The purpose of SnakeDb is to provide an easy database API. You don't have to write SQL statements in your scripts to create databases or execute a query. You can access the fields in the tables through a dictionary. The API also allows you to execute complex SQL statements. SnakeDb is a wrapper around other python database modules. SnakeDb ver 1.00 supports Gadfly and MySQL databases. You can easily extend it to use other python database modules.
Below are a brief description of every class in the SnakeDb module. Normally, you should not have to modify any of these classes.
class PersonTable(SnakeDbTable): tableName="person" def __init__(self): sqlFields={ #sqlfield: fieldtype 'personId':'varchar(100)', 'personObjId':'varchar(100)', 'creationTimeStamp':'timestamp(14)', 'firstName':'varchar(100)', 'lastName':'varchar(100)', 'phone':'varchar(100)', SnakeDbTable.__init__(self, self.tableName, sqlFields)
class Person(SnakeDbObj): def __init__(self, db, detailsDict={}): SnakeDbObj.__init__(self, db, PersonTable.tableName, detailsDict)For example, if xVar is an instance of Person, then you can use xVar.details to access the fields. xVar.details['firstName'] gets you the firstName.
db=PhoneBookDb() db.connectDb() x=Person(db) q=SnakeDbQuery(snakeDb=db,pageSize=5,pageNumber=2) q.setCriteria({'lastName':'Lee'}) results=x.find(q)
#!/usr/bin/env python from snakedb import * import sys from string import lower, join, split class PhoneBookDb(GadflySnakeDb): dbDir="/tmp" dbName="phonebook" def __init__(self): GadflySnakeDb.__init__(self,self.dbName, self.dbDir) def registerAllDbTables(self): self.registerDbTable(PersonTable()) self.registerDbTable(AddressTable()) class PersonTable(SnakeDbTable): tableName="person" def __init__(self): sqlFields={ #sqlfield: fieldtype 'personId':'varchar', 'personObjId':'varchar', 'updatedOnTimeStamp':'varchar', 'lastName':'varchar', 'firstName':'varchar', 'phone':'varchar' } SnakeDbTable.__init__(self, self.tableName, sqlFields) class Person(SnakeDbObj): def __init__(self, db, detailsDict={}): SnakeDbObj.__init__(self, db, PersonTable.tableName, detailsDict) class AddressTable(SnakeDbTable): tableName="address" def __init__(self): sqlFields={ #sqlfield: fieldtype 'addressId':'varchar', 'addressObjId':'varchar', 'updatedOnTimeStamp':'varchar', 'personId':'varchar', 'city':'varchar', 'state':'varchar' } SnakeDbTable.__init__(self, self.tableName, sqlFields) class Address(SnakeDbObj): def __init__(self, db, detailsDict={}): SnakeDbObj.__init__(self, db, AddressTable.tableName, detailsDict) db=PhoneBookDb() try: db.dropDb() except: pass db.createDb() print "New Phone Book database created!" personInfo={ 'firstName':'Ming', 'lastName':'Huang', 'phone':'212-228-0933' } p=Person(db, personInfo) p.create() addressInfo={ 'personId':p.details['personId'], 'city':'Brooklyn', 'state': 'NY' } a=Address(db, addressInfo) a.create() print "Added new entry: %s %s"%(p.details,a.details) print "Updating the address of Ming Huang" a2=Address(db) a2.load(a.details['addressId']) a2.details['city']='Brooklyn' a2.save() personInfo={ 'firstName':'Thanos', 'lastName':'Vassilakis', 'phone':'212-228-0933' } p=Person(db, personInfo) p.create() addressInfo={ 'personId':p.details['personId'], 'city':'New York', 'state': 'NY' } a=Address(db, addressInfo) a.create() print "Added new entry: %s %s"%(p.details,a.details) personInfo={ 'firstName':'John', 'lastName':'Lee', 'phone':'212-555-5555' } p=Person(db, personInfo) p.create() addressInfo={ 'personId':p.details['personId'], 'city':'Queens', 'state': 'NY' } a=Address(db, addressInfo) a.create() print "Added new entry: %s %s"%(p.details,a.details) personInfo={ 'firstName':'Sue', 'lastName':'Lee', 'phone':'212-555-6666' } p=Person(db, personInfo) p.create() addressInfo={ 'personId':p.details['personId'], 'city':'Queens', 'state': 'NY' } a=Address(db, addressInfo) a.create() print "Added new entry: %s %s"%(p.details,a.details) print "\n\nListing of all person with telephone: 212-228-0933" query=SnakeDbQuery(snakeDb=db,pageSize=5,pageNumber=1) query.setCriteria({'phone':'212-228-0933'}) print p.find(query) print "\n\nListing of all person with telephone: 212-228-0933 and last name 'Huang'" query=SnakeDbQuery(snakeDb=db,pageSize=5,pageNumber=1) query.setCriteria({'phone':'212-228-0933', 'lastName':'Huang'}) print p.find(query) print "\n\nDeleting all person who live in Brooklyn" #Testing the deletion of entries in two tables pgNumber=1 query=SnakeDbQuery(snakeDb=db,pageSize=5,pageNumber=pgNumber) query.setCriteria({'city':'Brooklyn'}) a3=Address(db) p3=Person(db) found=a3.find(query) while (found): for record in found: a3.remove(addressId=record['addressId']) p3.remove(personId=record['personId']) pgNumber=pgNumber+1 query.reset(pageSize=5,pageNumber=pgNumber) found=a3.find(query) print "\n\nDeleting all person with last name Lee" #Testing the deletion of entries in only one table #NOTE: In the following exmaple, the address entries for the deleted persons are not affected p4=Person(db) p4.remove(lastName='Lee') db.closeDb()