diff Database.py @ 34:5813e3c10f14

move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 05 May 2010 02:14:05 +0200
parents
children 22214d79ed41
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Database.py	Wed May 05 02:14:05 2010 +0200
@@ -0,0 +1,28 @@
+
+import Mapping
+import sqlalchemy
+import sqlalchemy.orm
+import util
+
+# Keep the connection to the database only once. The feed updater and the GUI app will
+# operate on a single engine/session but this comes in handy for interactive use
+engine = None
+SessionMaker = None
+
+def createSession():
+    databaseUrl = util.loadDatabaseUrl()
+    initEngine(databaseUrl)
+    Mapping.createMapping(engine)
+    initSessionMaker()
+    return SessionMaker()
+
+def initEngine(databaseUrl):
+    global engine
+    if engine is None:
+        engine = sqlalchemy.create_engine(databaseUrl, echo=False)
+
+def initSessionMaker():
+    global SessionMaker
+    if SessionMaker is None:
+        SessionMaker = sqlalchemy.orm.sessionmaker(bind=engine)
+    
\ No newline at end of file