Mercurial > hg > Feedworm
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/sqlalchemy/Database.py Sun Aug 21 03:55:16 2011 +0200 @@ -0,0 +1,36 @@ + +from sqlalchemy.engine import create_engine +from sqlalchemy.orm import sessionmaker +import Mapping +import sys +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=None): + if databaseUrl is None: + databaseUrl = _getDatabaseUrl() + initEngine(databaseUrl) + Mapping.createMapping(engine) + initSessionMaker() + return SessionMaker() + +def _getDatabaseUrl(): + if len(sys.argv) < 2: + print("Usage: %s <database url>" % (sys.argv[0])) + sys.exit(1) + return sys.argv[1] + +def initEngine(databaseUrl): + global engine + if engine is None: + verbose = util.databaseLoggingEnabled() + engine = create_engine(databaseUrl, echo=verbose) + +def initSessionMaker(): + global SessionMaker + if SessionMaker is None: + SessionMaker = sessionmaker(bind=engine)