#       m   
#      u    rowfactory.py - Sat Sep 07 19:03 CEST 2013
#  SQLite   exposes columns as row attributes
#    d      part of sqmediumlite
#   e       copyright (C): nobody
#  m        

"""
Custom version of the Row class from the standard Python
sqlite3 module (Pysqlite):
    http://docs.python.org/library/sqlite3.html#sqlite3.Connection.row_factory
A seperate version is included to to be used with APSW:
    http://apidoc.apsw.googlecode.com/hg/execution.html#row-tracer
Differences with the standard Row class are:
- columns can be accessed as attributes (PL/SQL style)
- can be passed as bind variables (dict) to new queries
- table alias is eventually  stripped: tab.colname -> colname
- column names must be given in strictly lower case
- does NOT support get column by position.
"""

class Row (dict):
    " row_factory for standard Python sqlite3 (Pysqlite) "
    def __init__ (self, cur, values):
        dict.__init__ (self, zip (
                (t[0] [t[0].find ('.') + 1:].lower ()
                    for t in cur.description),
                values
                ))
    def __getattr__ (self, k):
        try:
            return dict.__getitem__ (self, k)
        except KeyError as e:
            raise AttributeError (str (e))
    def __setattr__ (self, k, v):
        dict.__setitem__ (self, k, v)

class apswRow (dict):
    " argument for setrowtrace in APSW "
    def __init__ (self, cur, values):
        dict.__init__ (self, zip (
                (t[0] [t[0].find ('.') + 1:].lower ()
                    for t in cur.getdescription ()),
                values
                ))
    def __getattr__ (self, k):
        try:
            return dict.__getitem__ (self, k)
        except KeyError as e:
            raise AttributeError (str (e))
    def __setattr__ (self, k, v):
        dict.__setitem__ (self, k, v)