aboutsummaryrefslogtreecommitdiff
path: root/dodai/config/db/sa.py
diff options
context:
space:
mode:
Diffstat (limited to 'dodai/config/db/sa.py')
-rw-r--r--dodai/config/db/sa.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/dodai/config/db/sa.py b/dodai/config/db/sa.py
new file mode 100644
index 0000000..06adf9c
--- /dev/null
+++ b/dodai/config/db/sa.py
@@ -0,0 +1,64 @@
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
18from dodai.config.db import BaseConfigDb
19from dodai.db import Db
20
21class Sa(BaseConfigDb):
22
23
24 def load(self, obj):
25 from sqlalchemy.orm import sessionmaker
26 self._clean(obj)
27 db = Db()
28 db.engine = self._build_engine(obj)
29 Session = sessionmaker(bind=db.engine)
30 db.session = Session()
31 db.name = obj.name
32 db.protocol = obj.protocol
33 if hasattr(obj, 'filename') and obj.filename:
34 db.filename = obj.filename
35 else:
36 db.hostname = obj.hostname
37 if hasattr(obj, 'port') and obj.port:
38 db.port = obj.port
39 db.database = obj.database
40 return db
41
42 def _build_connection_string(self, obj):
43 out = []
44 out.append('{db.protocol}')
45 if hasattr(obj, 'protocol_extra') and obj.protocol_extra:
46 out.append('+{db.protocol_extra}')
47 out.append('://')
48 if hasattr(obj, 'filename') and obj.filename:
49 out.append('{db.filename}')
50 else:
51 out.append('{db.username}:{db.password}@')
52 out.append('{db.hostname}')
53 if hasattr(obj, 'port') and obj.port:
54 out.append(':{db.port}')
55 out.append('/{db.database}')
56 out = ''.join(out)
57 out = out.format(db=obj)
58 return out
59
60 def _build_engine(self, obj):
61 from sqlalchemy import create_engine
62 connection_string = self._build_connection_string(obj)
63 db_obj = create_engine(connection_string)
64 return db_obj