Package orm2 :: Module datasource :: Class datasource_base
[hide private]
[frames] | no frames]

Class datasource_base

source code

sql.datasource --+
                 |
                datasource_base
Known Subclasses:
adapters.gadfly.datasource.datasource, adapters.mysql.datasource.datasource, adapters.pgsql.datasource.datasource

The DataSource encapsulates the functionality we need to talk to the database. Most notably are the insert, select and delete methods.

This class must be subclassed by the adapter.*.datasource.datasource classes.

It inherits from sql.datasource to provide default implmentations of the methods the sql module depends upon.

Instance Methods [hide private]
  __init__(self)
  _dbconn(self)
Return the dbconn for this ds
  query_one(self, query)
This method is ment for results that return exactly one row or item
  execute(self, command, modify=False)
Execute COMMAND on the database.
  commit(self)
Run commit on this ds's connection.
  rollback(self)
Undo the changes you made to the database since the last commit()
  cursor(self)
Return a newly created dbi cursor.
  update_cursor(self)
Return the cursor that this ds uses for any query that modifies the database (to keep the transaction together).
  select(self, dbclass, *clauses)
SELECT dbobjs from the database, according to clauses.
  run_select(self, dbclass, select)
Run a select statement on this datasource that is ment to return rows suitable to construct objects of dbclass from them.
  select_one(self, dbclass, *clauses)
This method is ment for queries of which you know that they will return exactly one dbobj.
  count(self, dbclass, *clauses)
All clauses except the WHERE clause will be ignored (including OFFSET and LIMIT!)
  join_select(self, dbclasses, *clauses)
  primary_key_where(self, dbclass, key)
Return a orm2.sql where clause that will yield the object of dbclass whoes primary key equals key
  select_by_primary_key(self, dbclass, key)
Select a single object of dbclass from its relation, identified by its primary key.
  select_for_update(self, dbclass, key)
This method works like select_by_primary_key above, except that it doesn't select anything but returns a dummy object (an empty dbobj) that will allow setting attributes, yielding proper UPDATE statements.
  insert(self, dbobj, dont_select=False)
  select_after_insert(self, dbobj)
This method will be run after each INSERT statement automaticaly generated by a ds to pick up default values and primary keys set by the backend.
  select_after_insert_where(self, dbobj)
  update(self, relation, column, sql_literal, where)
Updates are stored in a list and executed on calls to commit() or to flush_updates() to join updates to the same row into a single SQL command.
  flush_updates(self)
Execute the updates stored by the update() method (see above).
  close(self)
  delete_by_primary_key(self, dbclass, primary_key_value)

Inherited from sql.datasource: backend_encoding, escape_string, identifyer_quotes, string_quotes


Class Variables [hide private]
  escaped_chars = [('\\', '\\\\'), ("'", "\\'"), ('"', '\\"'), ('%', '...
  _format_funcs = {}

Method Details [hide private]

__init__(self)
(Constructor)

source code 
None

_dbconn(self)

source code 
Return the dbconn for this ds

query_one(self, query)

source code 

This method is ment for results that return exactly one row or item

It will:
  • return None if there is an empty result returned
  • if there are more than one result row, return the result as is (a tuple of tuples)
  • if there is only one row, but several columns, return the row as a tuple
  • if the only row has only one column, return the value of the column
Parameters:
  • query - A string containing an SQL query.

execute(self, command, modify=False)

source code 
Execute COMMAND on the database. If modify is True, the command is assumed to modify the database. All modifying commands will be executed on the same cursor.
Parameters:
  • command - A string containing an SQL command of any kind or an sql.statement instance.

commit(self)

source code 
Run commit on this ds's connection. You need to do this for any change you really want to happen!

rollback(self)

source code 
Undo the changes you made to the database since the last commit()

cursor(self)

source code 
Return a newly created dbi cursor.

update_cursor(self)

source code 
Return the cursor that this ds uses for any query that modifies the database (to keep the transaction together).

select(self, dbclass, *clauses)

source code 
SELECT dbobjs from the database, according to clauses.
Parameters:
  • dbclass - The dbclass of the objects to be selected.
  • clauses - A list of orm2.sql clauses instances (or equivalent Python object i.e. strings) that are added to the sql.select query. See orm2.sql.select for details

run_select(self, dbclass, select)

source code 
Run a select statement on this datasource that is ment to return rows suitable to construct objects of dbclass from them.
Parameters:
  • dbclass - The dbclass of the objects to be selected
  • select - sql.select instance representing the query

select_one(self, dbclass, *clauses)

source code 
This method is ment for queries of which you know that they will return exactly one dbobj. It will set a limit=1 clause. If the result is empty, it will return None, otherwise the selected dbobj.

count(self, dbclass, *clauses)

source code 
All clauses except the WHERE clause will be ignored (including OFFSET and LIMIT!)
Parameters:
  • dbclass - See select() above.
  • clauses - See select() above.
Returns:
An integer value indicating the number of objects of dbclass select() would return if run with these clauses.

join_select(self, dbclasses, *clauses)

source code 
None

primary_key_where(self, dbclass, key)

source code 
Return a orm2.sql where clause that will yield the object of dbclass whoes primary key equals key
Parameters:
  • dbclass - The dbclass of the object the where clause is supposed to be for.
  • key - Python value representing the primary key or a tuple of such Python values, if the primary key has multiple columns

select_by_primary_key(self, dbclass, key)

source code 
Select a single object of dbclass from its relation, identified by its primary key.
Parameters:
  • dbclass - Dbclass to be selected
  • key - Python value representing the primary key or a tuple of such Python values, if the primary key has multiple columns
Returns:
A single dbobj.
Raises:
  • IllegalPrimaryKey - hallo

select_for_update(self, dbclass, key)

source code 

This method works like select_by_primary_key above, except that it doesn't select anything but returns a dummy object (an empty dbobj) that will allow setting attributes, yielding proper UPDATE statements. Note that supplying a primary key that does not exist will go unnoticed: The UPDATE statements won't create an error, they just won't affect any rows.

This method is primarily ment for transaction based (i.e. www) applications.

insert(self, dbobj, dont_select=False)

source code 
Parameters:
  • dbobj - The dbobj to be inserted (must not be created by a select statement.
  • dont_select - Do not perform a SELECT query for those columns whoes values are provided by the backend, either through AUTO_INCREMENT mechanisms or default column values.

select_after_insert(self, dbobj)

source code 
This method will be run after each INSERT statement automaticaly generated by a ds to pick up default values and primary keys set by the backend. See insert().

select_after_insert_where(self, dbobj)

source code 
None

update(self, relation, column, sql_literal, where)

source code 
Updates are stored in a list and executed on calls to commit() or to flush_updates() to join updates to the same row into a single SQL command.
Parameters:
  • relation - The relation to be updated
  • column - sql.column Name of the column to be updated
  • sql_literal - sql literal of the value to be stored.
  • where - where clause that would select (or update in this case) the desired row from relation

flush_updates(self)

source code 
Execute the updates stored by the update() method (see above).

close(self)

source code 
None

delete_by_primary_key(self, dbclass, primary_key_value)

source code 
None

Class Variable Details [hide private]

escaped_chars

None
Value:
[('\\', '\\\\'), ("'", "\\'"), ('"', '\\"'), ('%', '%%')]              
      

_format_funcs

None
Value:
{}