comparison 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
comparison
equal deleted inserted replaced
33:f371d02fa09d 34:5813e3c10f14
1
2 import Mapping
3 import sqlalchemy
4 import sqlalchemy.orm
5 import util
6
7 # Keep the connection to the database only once. The feed updater and the GUI app will
8 # operate on a single engine/session but this comes in handy for interactive use
9 engine = None
10 SessionMaker = None
11
12 def createSession():
13 databaseUrl = util.loadDatabaseUrl()
14 initEngine(databaseUrl)
15 Mapping.createMapping(engine)
16 initSessionMaker()
17 return SessionMaker()
18
19 def initEngine(databaseUrl):
20 global engine
21 if engine is None:
22 engine = sqlalchemy.create_engine(databaseUrl, echo=False)
23
24 def initSessionMaker():
25 global SessionMaker
26 if SessionMaker is None:
27 SessionMaker = sqlalchemy.orm.sessionmaker(bind=engine)
28