[Checkins] SVN: Product.ZSQLAlchemy/trunk/ZSQLAlchemy.py added some
basic functionality in order to experiment a bit
Andreas Jung
andreas at andreas-jung.com
Thu Feb 1 06:21:10 EST 2007
Log message for revision 72289:
added some basic functionality in order to experiment a bit
Changed:
U Product.ZSQLAlchemy/trunk/ZSQLAlchemy.py
-=-
Modified: Product.ZSQLAlchemy/trunk/ZSQLAlchemy.py
===================================================================
--- Product.ZSQLAlchemy/trunk/ZSQLAlchemy.py 2007-02-01 11:00:35 UTC (rev 72288)
+++ Product.ZSQLAlchemy/trunk/ZSQLAlchemy.py 2007-02-01 11:21:10 UTC (rev 72289)
@@ -11,21 +11,84 @@
from OFS.PropertyManager import PropertyManager
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
+
+import sqlalchemy
+import psycopg2 as psycopg
+
+psycopg = sqlalchemy.pool.manage(psycopg)
+
+
+class SessionProxy(object):
+
+ security = ClassSecurityInfo()
+
+ def __init__(self, engine):
+ self._engine = engine
+
+
+ security.declarePublic('getEngine')
+ def getEngine(self):
+ """ return the engine """
+ return self._engine
+
+
+ security.declarePublic('getMetaData')
+ def getMetaData(self):
+ """ return a MetaData instance """
+ return sqlalchemy.BoundMetaData(self._engine)
+
+
+InitializeClass(SessionProxy)
+
+
+
class ZSQLAlchemy(SimpleItem, PropertyManager):
meta_type = 'ZSQLAlchemy'
- connection_url = ''
+ hostname = ''
+ username = ''
+ password = ''
+ database = ''
manage_options = SimpleItem.manage_options + \
PropertyManager.manage_options
- _properties=({'id':'connection_url',
- 'type':'string',
- 'mode':'wrd'},)
+ _properties=(
+ {'id':'hostname', 'type':'string', 'mode':'wrd'},
+ {'id':'username', 'type':'string', 'mode':'wrd'},
+ {'id':'password', 'type':'string', 'mode':'wrd'},
+ {'id':'database', 'type':'string', 'mode':'wrd'},
+ )
security = ClassSecurityInfo()
+ def _getConnection(self):
+ """ connection factory """
+ db = psycopg.connect(database=self.database,
+ user=self.username,
+ password=self.password,
+ host=self.hostname)
+ return db
+
+
+ def _createEngine(self):
+ """ create an engine """
+
+ p = sqlalchemy.pool.QueuePool(self._getConnection, max_overflow=10, pool_size=10, use_threadlocal=True)
+ engine = sqlalchemy.create_engine('postgres://', pool=p)
+ return engine
+
+
+ security.declarePublic('getSession')
+ def getSession(self):
+ """ return a session proxy """
+
+ engine = self._createEngine()
+ proxy = SessionProxy(engine)
+ return proxy
+
+
InitializeClass(ZSQLAlchemy)
More information about the Checkins
mailing list