| |
- __builtin__.object
-
- CommonProperty
- SQLConnection
- exceptions.Exception(exceptions.BaseException)
-
- SQLError
class CommonProperty(__builtin__.object) |
|
Ref. Python CookBook 2d ed., by Alex Martelli, Anna Martelli
Ravenscroft and David Ascher (O'Reilly Media, 2005) 0-596-00797-3
Recipe 20.5 by Raymond Hettinger, page 752 |
|
Methods defined here:
- __delete__(self, obj)
- __get__(self, obj, objtype=None)
- __init__(self, realname, fget=<built-in function getattr>, fset=<built-in function setattr>, fdel=<built-in function delattr>, doc=None)
- __set__(self, obj, value)
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class SQLConnection(__builtin__.object) |
|
Base clase for handling connections with the database |
|
Methods defined here:
- __init__(self, timeout_=5)
- Returns connection and cursor objects
- commit(self)
- Commit the transaction
- delete(self, cls, deletedCriteria=None, commit=True)
- Deletes tuples from a table that match certain criteria.
Parameters:
cls: calling object. The class' object needs a __table__ and a
__pk__ attributes stating the table's name and the primary
key's column(s)' name(s). In case it's a composity a tuple
should be sent.
deletedCriteria: if its a single value, will match the __pk__
attribute for the class. If its a dict it will containe
the the column=filter criteria that will be concatenated
with "and".
Additional tips:
1- If you need to specify a more complex criteria
(e.g. with "or") use the word 'SQL' as key and
write the exact 'where' filter you need. SQL will
be append to the rest of the filters with an
'and' unless it's the only one filter.
2- If you want to use a "like" expression the
value will be scannned to find a % token.
3- To pass a "is null" criteria, use None as the
key's value.
4- To pass a "is not null" criteria, use '!None'
as the key's value.
5- If "filter" is a tuple or list it will be use
to construct an "in (tuple)" filter.
6- If deletedCriteria is None all table will be
deleted.
commit: indicates if the statement should be committed or not.
Default Yes
- disconnects(self)
- Closes the database connection
- find(self, cls, selectColumns=None, filterCriteria=None, returnAsInstance=False)
- Find the results in a table given by cls.__table__ and return
a list of dicts with the object's attributes that fullfill
filterCriteria.
Parameters:
cls: object to query
selectColumns: the columns the query must return. If it's a
tuple it will return these columns as if.
If it's a dictionary it will use the key for the
column and the value as the column's alias.
If None it will execute a "select * ..."
filterCriteria: a dict containing the the column=filter criteria
that will be concatenated with "and".
Additional tips:
1- If you need to specify a more complex criteria
(e.g. with "or") use the word 'SQL' as key and
write the exact 'where' filter you need. SQL will
be append to the rest of the filters with an
'and' unless it's the only one filter.
2- If you want to use a "like" expression the
value will be scannned to find a % token.
3- To pass a "is null" criteria, use None as the
key's value.
4- To pass a "is not null" criteria, use '!None'
as the key's value.
5- If "filter" is a tuple or list it will be use
to construct an "in (tuple)" filter.
6- If filterCriteria is None all table will be
retrieved.
returnAsInstance: in certain cases user will want to retrieve a
list of live objects: if this parameter is True
will call the cls.__init__ for every tuple
returned by the database. Default to False.
- get(self, cls, pk_id, **kwargs)
- Retrieves information from a table using the primary key. It
should always retrieve just one row (duh!).
If no information is found a SQLError will be raised.
Parameters:
cls: calling object. The class' object needs a __table__ and a
__pk__ attributes stating the table's name and the primary
key's column(s)' name(s). In case it's a composity a tuple
should be sent.
pk_id: the primary key's value we'll use to query.
kwargs: a "column name":"alias" to use for the query. If no
value is provided it will execute a "select * from ...".
Otherwise, just the columns listed will be retrieved.
Example:
kwargs = 'tas_from'='from', 'tas_max'='max'
will be turned into
select tas_from as from, tas_max as max from ...
- insert(self, cls, values, constraint_action, commit=True)
- Inserts a new value in the database. Returns the primary key value
in case the queried class defines one.
Parameters:
cls: calling object. The class' object needs a __table__ and a
__pk__ attributes stating the table's name and the primary
key's column(s)' name(s). In case it's a composity a tuple
should be sent.
values: a "column name":"inserted value" dict to use for the
query.
Example:
values = {'tas_from':u'/tmp',
'tas_add_date'='2008-03-20 12:34:56.0'}
will be turned into
insert into __table__ (tas_from, tas_add_date)
values (u'/tmp', '2008-03-20 12:34:56.0')
if "values" is a list of dicts (following the up supra rules)
it will perform a bulk execute (à la executemany)
constraint_action: what action the system must apply in case
of contraint violation. The possible values
are the ones supported by the SQL engine:
ABORT, REPLACE, IGNORE, ROLLBACK, FAIL.
commit: indicates if the statement should be committed or not.
Default Yes
- rollback(self)
- Rollback the transaction
- update(self, cls, updatedValue, updatedCriteria, commit=True)
- Updates a table.
In case updatedValue and updatedCriteria are lists (of dicts)
will execute a bulk update (à la executemany). If they're dicts
will execute just one update (see respective parameter docs for
more on this).
In case it's a bulk update both updatedCriteria and updatedValue
must be a list of dicts.
Parameters:
cls: calling object. The class' object needs a __table__ and a
__pk__ attributes stating the table's name and the primary
key's column(s)' name(s). In case it's a composity a tuple
should be sent.
updatedValue: a "column name":"update value" dict to use for the
query.
Example:
values = {'tas_from'=u'/tmp',
'tas_add_date'='2008-03-20 12:34:56.0'}
will be turned into
update __table__ set tas_from=u'/tmp',
tas_add_date='2008-03-20 12:34:56.0'
where ...
updatedCriteria: if its a single value, will match the __pk__
attribute for the class. If its a dict it will containe
the the column=filter criteria that will be concatenated
with "and".
Additional tips:
1- If you need to specify a more complex criteria
(e.g. with "or") use the word 'SQL' as key and
write the exact 'where' filter you need. SQL will
be append to the rest of the filters with an
'and' unless it's the only one filter.
2- If you want to use a "like" expression the
value will be scannned to find a % token.
3- To pass a "is null" criteria, use None as the
key's value.
4- To pass a "is not null" criteria, use '!None'
as the key's value.
5- If "filter" is a tuple or list it will be use
to construct an "in (tuple)" filter.
6- If updatedCriteria is None all table will be
updated.
commit: indicates if the statement should be committed or not.
Default Yes
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
|