comparison backend/sqlalchemy/Database.py @ 119:04a730f9d07d backend

move all sqlalchemy related classes to the respective sub-package. use a backend to abstract from access to the data
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sun, 21 Aug 2011 03:55:16 +0200
parents Database.py@842727971796
children 5b131f82057d
comparison
equal deleted inserted replaced
118:0e73adb2dec4 119:04a730f9d07d
1
2 from sqlalchemy.engine import create_engine
3 from sqlalchemy.orm import sessionmaker
4 import Mapping
5 import sys
6 import util
7
8 # Keep the connection to the database only once. The feed updater and the GUI app will
9 # operate on a single engine/session but this comes in handy for interactive use
10 engine = None
11 SessionMaker = None
12
13 def createSession(databaseUrl=None):
14 if databaseUrl is None:
15 databaseUrl = _getDatabaseUrl()
16 initEngine(databaseUrl)
17 Mapping.createMapping(engine)
18 initSessionMaker()
19 return SessionMaker()
20
21 def _getDatabaseUrl():
22 if len(sys.argv) < 2:
23 print("Usage: %s <database url>" % (sys.argv[0]))
24 sys.exit(1)
25 return sys.argv[1]
26
27 def initEngine(databaseUrl):
28 global engine
29 if engine is None:
30 verbose = util.databaseLoggingEnabled()
31 engine = create_engine(databaseUrl, echo=verbose)
32
33 def initSessionMaker():
34 global SessionMaker
35 if SessionMaker is None:
36 SessionMaker = sessionmaker(bind=engine)