annotate backend/sqlalchemy/Database.py @ 222:fe6a2dad6e29

ignore hg reverted files
author Dirk Olmes <dirk@xanthippe.ping.de>
date Thu, 22 May 2014 05:11:35 +0200
parents bb3c851b18b1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
217
bb3c851b18b1 add source file endcoding header
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 137
diff changeset
1 # -*- coding: utf-8 -*-
119
04a730f9d07d move all sqlalchemy related classes to the respective sub-package. use a backend to abstract from access to the data
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 70
diff changeset
2 from sqlalchemy.engine import create_engine
04a730f9d07d move all sqlalchemy related classes to the respective sub-package. use a backend to abstract from access to the data
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 70
diff changeset
3 from sqlalchemy.orm import sessionmaker
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
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
4 import Mapping
137
5b131f82057d allow choosing the backend via commandline option
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 119
diff changeset
5 import argparse
57
254d5b89a6ca make sqlalchemy logging configurable through the --databaseLogging commandline parameter
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 37
diff changeset
6 import util
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
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
7
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
8 # Keep the connection to the database only once. The feed updater and the GUI app will
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
9 # operate on a single engine/session but this comes in handy for interactive use
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
10 engine = None
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
11 SessionMaker = None
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
12
70
842727971796 have the DB URL as parameter when creating a session and fall back to commandline arguments if no DB URL was passed in
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 57
diff changeset
13 def createSession(databaseUrl=None):
842727971796 have the DB URL as parameter when creating a session and fall back to commandline arguments if no DB URL was passed in
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 57
diff changeset
14 if databaseUrl is None:
842727971796 have the DB URL as parameter when creating a session and fall back to commandline arguments if no DB URL was passed in
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 57
diff changeset
15 databaseUrl = _getDatabaseUrl()
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
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
16 initEngine(databaseUrl)
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
17 Mapping.createMapping(engine)
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
18 initSessionMaker()
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
19 return SessionMaker()
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
20
37
22214d79ed41 database URL must be given as commandline argument now, no need for creating complicated config files. Add a menu entry for opening the selected article in browser.
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 34
diff changeset
21 def _getDatabaseUrl():
137
5b131f82057d allow choosing the backend via commandline option
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 119
diff changeset
22 parser = argparse.ArgumentParser()
5b131f82057d allow choosing the backend via commandline option
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 119
diff changeset
23 parser.add_argument("--dburl", nargs="?", required=True, help="Database URL for the sqlalchemy backend")
5b131f82057d allow choosing the backend via commandline option
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 119
diff changeset
24 args = parser.parse_known_args()
5b131f82057d allow choosing the backend via commandline option
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 119
diff changeset
25 return args[0].dburl
37
22214d79ed41 database URL must be given as commandline argument now, no need for creating complicated config files. Add a menu entry for opening the selected article in browser.
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 34
diff changeset
26
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
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
27 def initEngine(databaseUrl):
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
28 global engine
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
29 if engine is None:
57
254d5b89a6ca make sqlalchemy logging configurable through the --databaseLogging commandline parameter
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 37
diff changeset
30 verbose = util.databaseLoggingEnabled()
119
04a730f9d07d move all sqlalchemy related classes to the respective sub-package. use a backend to abstract from access to the data
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 70
diff changeset
31 engine = create_engine(databaseUrl, echo=verbose)
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
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
32
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
33 def initSessionMaker():
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
34 global SessionMaker
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
35 if SessionMaker is None:
119
04a730f9d07d move all sqlalchemy related classes to the respective sub-package. use a backend to abstract from access to the data
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 70
diff changeset
36 SessionMaker = sessionmaker(bind=engine)