Package install :: Package MoSTBioDat :: Package DataBase :: Package ForgetSQL2 :: Module forgetsql2 :: Class Table
[hide private]
[frames] | no frames]

Class Table

source code

object --+    
         |    
  Database --+
             |
            Table

Representation of a table.

Instances of this class represent a row in the table, and can be
retrieved in several ways. In the examples below, the subclass Thing
is assumed.

- By calling the constructor with a primary key::

    # thing with primary key thing_id=1447
    thing = Thing(thing_id=1447)

- By iterating over the the class::

    # all things
    for thing in Thing:
        pass    

- By iterating over a filtered subset using SQL where::

    # All things with thing.value > 13
    for thing in Thing.where("value > $value", value=13):
        pass

- By following a foreign key from another table::

    # the thing with thing_id = person.thing_id
    thing = person.get_thing()

- By iterating over rows who have a foreign key to another table::
    
    # all things which have thing.box_id = box.box_id
    for thing in box.get_things():
        pass

Nested Classes [hide private]
  __metaclass__
Metaclass for Table allowing iteration.
Instance Methods [hide private]
 
__init__(self, _db_row=None, **primary)
Instanciate a new or existing database row.
source code
 
_load(self, _db_row=None, reload=False, **primary)
Load from database.
source code
 
__repr__(self)
repr(x)
source code
 
undo(self)
Undo changed attributes.
source code
 
save(self)
Save changes to database.
source code
 
_save_primary(self)
Store a copy of current primary keys.
source code
 
_get_foreign(self, _foreign)
Fetch foreign key as instance
source code
 
_set_foreign(self, value, _foreign)
Set foreign key by instance
source code
 
_get_children(self, _Child, _child_field, _my_field)
Yield all children as instances.
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __str__

Class Methods [hide private]
 
where(cls, where=None, **parameters)
Yield all instances limited by ``where`` clause.
source code
 
get(cls, where=None, **parameters)
Like where(), but returns first instance or None.
source code
 
_where_primary(cls)
Get WHERE part for primary key.
source code

Inherited from Database (private): _execute, _iter_cursor, _query, _query_one

Class Variables [hide private]

Inherited from Database (private): _db

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, _db_row=None, **primary)
(Constructor)

source code 

Instanciate a new or existing database row.

If no parameters are given, a new, blank instance is created, which will be INSERT-ed when save() is called.

Else, if keyword arguments are supplied, they must match the primary keys in _primary, and will be used for loading an existing row. In such a case, the instance can be UPDATE-d by calling save() - or restored to the database values by calling undo().

Mostly for internal usage, if _db_row is provided, it is assumed to be a dictionary of row values as returned from cls.query(), from which the object values will be loaded instead.

Overrides: object.__init__

where(cls, where=None, **parameters)
Class Method

source code 

Yield all instances limited by ``where`` clause.

Use $field in where clause and supply values in the optional dict parameter ``**parameters`` or as keyword arguments. Parameters will be properly escaped.

If you supply keyword parameters, but not a where-clause, a where clause "WHERE x=$x AND y=$y" will be generated from the keywords.

Example:

   # Assume generated class County
   for county in County.where("county_name=$name"
                              name="Oslo"):
       print county                            

   for county in County.where(county_name="Oslo"):
       print county                            

_where_primary(cls)
Class Method

source code 

Get WHERE part for primary key.

Note that the fields are prefixed with p__ to avoid mixup when used by UPDATE.

_load(self, _db_row=None, reload=False, **primary)

source code 

Load from database.

If _db_row is supplied, it is assumed to be a dict as returned by _query, and the instance will be loaded without any new database calls.

If reload is True, the saved primary keys in the attribute _primary_values will be used to reload all attributes.

Else, if keyword arguments are supplied, they must match the primary keys in _primary, and will be used for loading a unique row.

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

undo(self)

source code 

Undo changed attributes.

If this instance represent an existing row, the instance will be reloaded to the *current* database values.

If this is a new instance not yet inserted to the database, all database related attributes are removed.

save(self)

source code 

Save changes to database.

Return number of rows updated/inserted, normally 1. (This is database dependant, sqlite will often return 0)

_save_primary(self)

source code 

Store a copy of current primary keys.

The attribute _primary_values will be updated with the current primary key values. These will be used for reload-ing and updates in case the normal attributes are changed.

The attributes are saved as a dictionary with p__fieldname as keys, to match the WHERE clause generated by _where_primary().

_get_children(self, _Child, _child_field, _my_field)

source code 

Yield all children as instances.

A child is someone whose foreign keys point to us.