aboutsummaryrefslogtreecommitdiff
path: root/lib/dodai/config/databases/sa.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dodai/config/databases/sa.py')
-rw-r--r--lib/dodai/config/databases/sa.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/dodai/config/databases/sa.py b/lib/dodai/config/databases/sa.py
new file mode 100644
index 0000000..eaf35ea
--- /dev/null
+++ b/lib/dodai/config/databases/sa.py
@@ -0,0 +1,90 @@
1# Copyright (C) 2010 Leonard Thomas
2#
3# This file is part of Dodai.
4#
5# Dodai is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# Dodai is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Dodai. If not, see <http://www.gnu.org/licenses/>.
17
18class Sa(object):
19 """Callable object that will wire up sqlalchemy engine and
20 session objects
21
22 Attributes:
23 create_engine: The sqlalchemy create_engine object
24 session_maker: The sqlalchemy session_maker object
25 result_object: The object that will be populated with
26 all the connection information along with
27 the sqlalchemy objects
28
29 """
30 def __init__(self, create_engine, session_maker, result_object):
31 self._create_engine = create_engine
32 self._session_maker = session_maker
33 self._result_object = result_object
34
35 def __call__(self, section, name):
36 db = self._result_object()
37 db.name = name
38 db.protocol = section['protocol']
39
40 if section.has_key('protocol_extra') and section['protocol_extra']:
41 db.protocol_extra = section['protocol_extra']
42
43 if section.has_key('filename') and section['filename']:
44 db.filename = section['filename']
45
46 if section.has_key('port') and section['port']:
47 db.port = int(section['port'])
48
49 if section.has_key('database') and section['database']:
50 db.database = section['database']
51
52 if section.has_key('schema') and section['schema']:
53 db.schema = section['schema']
54
55 if section.has_key('username') and section['username']:
56 db.username = section['username']
57
58 if section.has_key('hostname') and section['hostname']:
59 db.hostname = section['hostname']
60
61 db.engine = self._build_engine(section)
62 db.session = self._build_session(db.engine)
63 return db
64
65 def _build_session(self, engine):
66 session = self._session_maker(bind=engine)
67 return session()
68
69 def _build_connection_string(self, section):
70 out = []
71 out.append('{section[protocol]}')
72 if section.has_key('protocol_extra') and section['protocol_extra']:
73 out.append('+{section[protocol_extra]}')
74 out.append('://')
75 if section.has_key('filename') and section['filename']:
76 out.append('/{section[filename]}')
77 else:
78 out.append('{section[username]}:{section[password]}@')
79 out.append('{section[hostname]}')
80 if section.has_key('port') and section['port']:
81 out.append(':{section[port]}')
82 out.append('/{section[database]}')
83 out = ''.join(out)
84 out = out.format(section=section)
85 return out
86
87 def _build_engine(self, section):
88 connection_string = self._build_connection_string(section)
89 db_obj = self._create_engine(connection_string)
90 return db_obj