|
8 | 8 | from odmtools.odmservices import SeriesService, EditService, ExportService |
9 | 9 | from odmtools.controller import EditTools |
10 | 10 | from odmtools.lib.Appdirs.appdirs import user_config_dir |
11 | | -from odmtools.odmdata import SessionFactory, change_schema, ODM#, refreshDB |
| 11 | +from odmtools.odmdata import SeriesService, dbconnection #ODM#, refreshDBSessionFactory, |
| 12 | + |
12 | 13 |
|
13 | 14 |
|
14 | 15 |
|
@@ -44,6 +45,7 @@ def __init__(self, debug=False, conn_dict = None): |
44 | 45 | line_dict['password'] = line[2] |
45 | 46 | line_dict['address'] = line[3] |
46 | 47 | line_dict['db'] = line[4] |
| 48 | + line_dict['version']= line[5] |
47 | 49 | self._conn_dicts.append(line_dict) |
48 | 50 | else: |
49 | 51 | self._conn_dicts.append(conn_dict) |
@@ -84,115 +86,30 @@ def add_connection(self, conn_dict): |
84 | 86 | # logger.error("Unable to save connection due to invalid connection to database") |
85 | 87 | # return False |
86 | 88 |
|
87 | | - @staticmethod |
88 | | - def _getSchema(engine): |
89 | | - from sqlalchemy.engine import reflection |
90 | | - |
91 | | - insp=reflection.Inspector.from_engine(engine) |
92 | | - |
93 | | - for name in insp.get_schema_names(): |
94 | | - if 'odm2'== name.lower(): |
95 | | - return name |
96 | | - else: |
97 | | - return insp.default_schema_name |
98 | | - |
99 | | - @classmethod |
100 | | - def _setSchema(self, engine): |
101 | | - |
102 | | - s = self._getSchema(engine) |
103 | | - change_schema(s) |
104 | | - |
105 | | - |
106 | | - @classmethod |
107 | | - def testEngine(self, connection_string): |
108 | | - |
109 | | - s = SessionFactory(connection_string, echo=False) |
110 | | - try: |
111 | | - # s.ms_test_Session().query(Variable1).limit(1).first() |
112 | | - s.test_Session().query(ODM.Variable.code).limit(1).first() |
113 | | - |
114 | | - |
115 | | - except Exception as e: |
116 | | - print "Connection was unsuccessful ", e.message |
117 | | - return False |
118 | | - return True |
119 | | - # def is_valid_connection(self): |
120 | | - # if self._current_conn_dict: |
121 | | - # conn_string = self._build_connection_string(self._current_conn_dict) |
122 | | - # logger.debug("Conn_string: %s" % conn_string) |
123 | | - # try: |
124 | | - # if self.testEngine(conn_string): |
125 | | - # return self._current_conn_dict |
126 | | - # |
127 | | - # return None |
128 | | - |
129 | | - def is_valid_connection(self): |
130 | | - if self._current_conn_dict: |
131 | | - conn_string = self._build_connection_string(self._current_conn_dict) |
132 | | - logger.debug("Conn_string: %s" % conn_string) |
133 | | - dbtype = float(self._current_conn_dict['version']) |
134 | | - #dbtype =1.1 |
135 | | - #refreshDB(dbtype) |
136 | | - |
137 | | - try: |
138 | | - if self.testEngine(conn_string): |
139 | | - return self._current_conn_dict |
140 | | - except Exception as e: |
141 | | - logger.fatal("The previous database for some reason isn't accessible, please enter a new connection %s" % e.message) |
142 | | - return None |
143 | | - |
144 | | - def test_connection(self, conn_dict): |
145 | | - try: |
146 | | - conn_string = self._build_connection_string(conn_dict) |
147 | | - |
148 | | - dbtype = float(conn_dict['version']) |
149 | | - #dbtype =1.1 |
150 | | - #refreshDB(dbtype) |
151 | | - if self.testEngine(conn_string):# and self.get_db_version(conn_string) == '1.1.1': |
152 | | - |
153 | | - return True |
154 | | - except SQLAlchemyError as e: |
155 | | - logger.error("SQLAlchemy Error: %s" % e.message) |
156 | | - raise e |
157 | | - except Exception as e: |
158 | | - logger.error("Error: %s" % e) |
159 | | - raise e |
160 | | - return False |
161 | 89 |
|
162 | 90 | def delete_connection(self, conn_dict): |
163 | 91 | self._conn_dicts[:] = [x for x in self._conn_dicts if x != conn_dict] |
164 | 92 |
|
165 | | - # Create and return services based on the currently active connection |
166 | | - # def get_db_version_dict(self, conn_dict): |
167 | | - # conn_string = self._build_connection_string(conn_dict) |
168 | | - # self.get_db_version(conn_string) |
169 | | - |
170 | | - # def get_db_version(self, conn_string): |
171 | | - # if isinstance(conn_string, dict): |
172 | | - # conn_string = self._build_connection_string(conn_string) |
173 | | - # service = SeriesService(conn_string) |
174 | | - # #if not self.version: |
175 | | - # try: |
176 | | - # self.version = service.get_db_version() |
177 | | - # except Exception as e: |
178 | | - # logger.error("Exception: %s" % e.message) |
179 | | - # return None |
180 | | - # return self.version |
181 | 93 |
|
182 | 94 | def get_series_service(self, conn_dict=None, conn_string=""): |
183 | | - version = 1.1 |
184 | | - if conn_dict: |
185 | | - conn_string = self._build_connection_string(conn_dict) |
186 | | - #self._current_conn_dict = conn_dict |
187 | | - |
188 | | - version = float(conn_dict['version']) |
189 | | - elif not conn_dict and not conn_string: |
190 | | - conn_string = self._build_connection_string(self._current_conn_dict) |
191 | | - version = float(self._current_conn_dict['version']) |
192 | | - |
193 | | - sf = SessionFactory(conn_string, self.debug, version = version) |
194 | | - ss= SeriesService(sf) |
195 | | - ss.refreshDB(sf.version) |
| 95 | + if not conn_dict and not conn_string: |
| 96 | + conn_dict = self._current_conn_dict |
| 97 | + conn = dbconnection.createConnection(conn_dict['engine'], conn_dict['address'], conn_dict['db'], conn_dict['user'], |
| 98 | + conn_dict['password'], conn_dict['version']) |
| 99 | + |
| 100 | + # version = 1.1 |
| 101 | + # if conn_dict: |
| 102 | + # conn_string = self._build_connection_string(conn_dict) |
| 103 | + # #self._current_conn_dict = conn_dict |
| 104 | + # |
| 105 | + # version = float(conn_dict['version']) |
| 106 | + # elif not conn_dict and not conn_string: |
| 107 | + # conn_string = self._build_connection_string(self._current_conn_dict) |
| 108 | + # version = float(self._current_conn_dict['version']) |
| 109 | + # |
| 110 | + # sf = SessionFactory(conn_string, self.debug, version = version) |
| 111 | + ss= SeriesService(conn) |
| 112 | + ss.refreshDB(conn.version) |
196 | 113 | return ss |
197 | 114 |
|
198 | 115 | # def get_cv_service(self): |
|
0 commit comments