SnakeDb ver 1.00


Author: Ming Hui Huang
Company: Script Foundry Inc.
Company URL: http://www.scriptfoundry.com/
Info URL: http://www.scriptfoundry.com/modules/snakedb/
Download URL: http://www.scriptfoundry.com/modules/dist/
Contact: ming@scriptfoundry.com

Introduction:

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.

System Requirements:

SnakeDb Classes:

Below are a brief description of every class in the SnakeDb module. Normally, you should not have to modify any of these classes.

Special Features:

There are few special features of SnakeDb. If you want to have a database table called "Person". You will need to define the following class:
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)

Installation:

  1. Download the distribution file at http://www.scriptfoundry.com/modules/dist/
  2. Uncompress the distribution file
  3. change into the directory created during the uncompression of distribution file
  4. execute the command:
    python setup.py install
    NOTE: Make sure you have the correct permissions before you run step 4. If the python interpreter was installed by ROOT, a regular user probably run into trouble.



Unix Installation Example:

Code Example:

Below is a script that shows how to create and use a Gadfly database.

#!/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()