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

Module forgetsql2

source code

ForgetSQL - SQL to object database wrapper.

NOTE: This version 2 of forgetSQL is NOT backwards compatible with version 0.5.1. This is the reason it is called forgetsql2.py - so that it can co-exist with the old version.

You should only need to use the generate() function to generate the classes mapping to your tables, which will be subclasses of the Table class.

Example usage:

   import MySQLdb, forgetsql2
   # Connect to MySQLdb using keyword parameters
   db = forgetsql2.generate(MySQLdb, {db='fish'})

   # Iterate through generated class from the table "postal"
   for postal in db.Postal:
       # Print normal fields
       print postal.postal_no, postal.postal_name, postal.municipal_id
       # Follow the foreign key municipal_id to retrieve the entry
       # from the Municipal class
       municipal = postal.get_municipal()    
       print municipal.municipal_name
       
   # Retrieve by primary key
   rogaland = db.County(county_id=11)    
   # Iterate over municipals that have foreign keys to rogaland
   for municipal in rogaland.get_municipals():
       print municipal.municipal_name
Classes [hide private]
  error
Generic table error
  NotFoundError
Could not find
  PrimaryKeyError
Missing/wrong primary keys
  UnsupportedDBError
Unsupported db module
  DBConnect
Database connection.
  Database
Base class for objects that uses the database.
  metaclass_table
Metaclass for Table allowing iteration.
  Table
Representation of a table.
  TableBuilder
Build Table subclasses by investigating database.
Functions [hide private]
 
_sql_bool(value)
Convert SQL bool value to Python True/False
source code
 
generate(db_module, connect_info, globals=None, log=None)
Generate forgetSQL classes and return as a module object.
source code
Variables [hide private]
  _col_pat = re.compile(r'\$([a-zA-Z][a-zA-Z0-9_-]*)')
Function Details [hide private]

generate(db_module, connect_info, globals=None, log=None)

source code 

Generate forgetSQL classes and return as a module object.

The db_module can be MySQLdb or sqlite2. This parameter must be the actual module object, imported by the caller.

The connect_info is provided to db_module.connect() and can be

  • a dictionary (sent as keyword arguments)
  • a tuple/list (sent as positional arguments)
  • a string (sent as 1st argument)

If the optional parameter ''globals'' is provided, instead of generating a new module, generated classes will be inserted into the namespace dictionary, usually as provided by globals().

Example:

   # simple usage
   import MySQLdb, forgetsql2
   db = forgetsql2.generate(MySQLdb, {db='fish'})
   for postal in db.Postal:
       print postal.postal_no, postal.postal_name
   
   # export symbols to this module
   import MySQLdb, forgetsql2
   forgetsql2.generate(MySQLdb, {db='fish'}, globals())
   for postal in Postal:
       print postal.postal_no, postal.postal_name

The second variant can be used in a seperate module for bigger projects, for instance database.py. Other modules can then do:

   import database
   for postal in database.Postal:
       print postal.postal_no