Package gmisclib :: Module sqlbase
[frames] | no frames]

Module sqlbase

source code

This is a module for using a SQLlite database as a collection of python objects.

For each SQL table, you derive a class from DBx. Each instance of that class corresponds to one row of the table.

Classes
  DBMetaClass
This is used by all the SQL classes, and initializes the class.
  SQLError
Any errors raised by this module.
  NoSuchTable
  ColumnMismatchError
  DB
Base class for all persistent objects in a sqlite database.
  DBx
This class is the main interface.
Functions
 
multiselect(cll, db, where, *args)
This is used for SQL select operations on a join of several tables.
source code
One of the classes in list_of_classes.
get_version(db_connection, list_of_classes)
This finds which of several variants of a table actually exists in the database.
source code
 
multiclass_get_by_id(possible_classes, db, idx)
This reads in a row from the database when you don't know exactly what information is available The idea is that there is a one of several possible types of objects stored in the database, and you will be happy to take whichever one is available.
source code
 
test() source code
Variables
  DEBUG = 0
  __package__ = 'gmisclib'

Imports: re, sqlite3, weakref, connect, die


Function Details

multiselect(cll, db, where, *args)

source code 

This is used for SQL select operations on a join of several tables.

Parameters:
  • cll (a sequence of (class, str) where class is derived from DBx) - This a sequence of (class, name) pairs, where class matches a table in the database, and name is used to refer to that table in the SQL select statement.
  • where (str) - This is the body of the select statement. Everything after select * from tablename.
  • args - All the arguments for the select statement.
  • db (sqlite3.Connection)

Note: As an example, if you have a table X in the database and a class x, and x.SQL_name=="X", and each row in X has an id number, and also the ID number of the next item, then you can find pairs of adjacent items with this call:

       multiselect(((x, "x1"), (x, "x2")), db, "x1.next = x2.id")

get_version(db_connection, list_of_classes)

source code 

This finds which of several variants of a table actually exists in the database. The intent is to let you upgrade from one version of table format to another.

Parameters:
  • db_connection - A sqlite3.Connection to a database.
  • list_of_classes (sequence of some class derived from sqlbase.DBx. These are the classes themselves, not class instances.) - Possible classes to try. Each will be checked for consistency with the database.
Returns: One of the classes in list_of_classes.
The first class in the list that is consistent with the structure of the database.

multiclass_get_by_id(possible_classes, db, idx)

source code 

This reads in a row from the database when you don't know exactly what information is available The idea is that there is a one of several possible types of objects stored in the database, and you will be happy to take whichever one is available. This makes most sense when they are different versions of the same class. You access the objects by their ID number.

Parameters:
  • possible_classes (a sequence of classes, where each class is derived from DBx) - possible classes to match a table in the database. Classes must be derived from DBx, must have a name that matches a table in the database, must have the right number of columns, and the raw data must be convertible to an object of that class.
  • idx (int) - the id of a row in the database.
  • db (a sqlite3.Connection to a database.)

Note: Ideally, there should be a 1:1 relationship between the names of tables in the database and the SQL_name attributes of classes. So, when you have a new version of a class, you really should have a new name for the corresponding table in the database. However, if you wish to have several classes that have the same SQL_name attribute, a class can raise ColumnMismatchError if the class and the data row are incompatible.