view backend/sqlalchemy/Database.py @ 134:40163eac0d27

rename feedupdate-main to feedupdate, adjust the launch script
author Dirk Olmes <dirk@xanthippe.ping.de>
date Tue, 23 Aug 2011 03:49:29 +0200
parents 04a730f9d07d
children 5b131f82057d
line wrap: on
line source


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)