Gadfly Singleton (was Re: Gadfly Question

David invalid.address at 127.0.0.1
Sun Jun 25 12:30:03 EDT 2000


This seems to work nicely.  It creates a singleton instance and connection
to the database.  One can completely bugger it by supplying a different
dbase name/path, I suppose.
 
Feel free to fix/improve/extend this and re-post.  Thx!

======

import gadfly, os, os.path

class _DBase:
	instance = None
	connection = None
	def __init__(self, dbase="dbname", path="dbpath"):
		if not os.path.isdir(path):
			print "creating database directory: "+path
			os.mkdir(path)

		if not os.path.isfile(path+"/"+dbase+".gfd"):
			print "creating database file: "+dbase
			connection = gadfly.gadfly()
			connection.startup(dbase, path)

			cursor = connection.cursor()
			cursor.execute("CREATE TABLE spam (foo integer)")
			cursor.execute("DROP TABLE spam")

			connection.commit()

	def extendedFunctionality(self):
		print "Wowsa!"

def ConnectDB(dbase, path):
	if _DBase.instance == None:
		_DBase.instance = _DBase(dbase, path)

	_DBase.connection = gadfly.gadfly(dbase, path)

	return _DBase.instance


# stand-alone testing
if __name__ == "__main__":
	dbase = ConnectDB("testdb", "testdb")
	connection =  dbase.connection

	c = connection.cursor()
	c.execute("SELECT * FROM __table_names__")
	print c.pp() 

	print dbase.extendedFunctionality()
	



More information about the Python-list mailing list