@@ -45,7 +45,7 @@ def __init__(self, debug=False, conn_dict = None):
4545 line_dict ['password' ] = line [2 ]
4646 line_dict ['address' ] = line [3 ]
4747 line_dict ['db' ] = line [4 ]
48- line_dict ['version' ]= line [5 ]
48+ line_dict ['version' ]= float ( line [5 ]) if len ( line ) > 5 else 1.1
4949 self ._conn_dicts .append (line_dict )
5050 else :
5151 self ._conn_dicts .append (conn_dict )
@@ -86,17 +86,39 @@ def add_connection(self, conn_dict):
8686 # logger.error("Unable to save connection due to invalid connection to database")
8787 # return False
8888
89+ def is_valid_connection (self ):
90+
91+ if self .get_current_conn_dict ():
92+ #conn_string = self._build_connection_string(self._current_conn_dict)
93+ #logger.debug("Conn_string: %s" % conn_string)
94+ conn_dict = self .get_current_conn_dict ()
95+ try :
96+ if dbconnection .isValidConnection (dbconnection .buildConnectionString (conn_dict ['engine' ], conn_dict ['address' ], conn_dict ['db' ], conn_dict ['user' ],
97+ conn_dict ['password' ]), dbtype = conn_dict ['version' ]):
98+ return self .get_current_conn_dict ()
99+ except Exception as e :
100+ logger .fatal (
101+ "The previous database for some reason isn't accessible, please enter a new connection %s" % e .message )
102+ return None
103+ return None
104+
89105
90106 def delete_connection (self , conn_dict ):
91107 self ._conn_dicts [:] = [x for x in self ._conn_dicts if x != conn_dict ]
92108
93109
94110 def get_series_service (self , conn_dict = None , conn_string = "" ):
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' ],
111+ if not conn_dict :
112+ conn_dict = self .get_current_conn_dict ()
113+
114+ if conn_string :
115+ #todo how to get version from a connection string
116+ conn = dbconnection .createConnectionFromString (conn_string , float (self .get_current_conn_dict ()["version" ]))
117+ else :
118+ conn = dbconnection .createConnection (conn_dict ['engine' ], conn_dict ['address' ], conn_dict ['db' ], conn_dict ['user' ],
98119 conn_dict ['password' ], conn_dict ['version' ])
99120
121+
100122 # version = 1.1
101123 # if conn_dict:
102124 # conn_string = self._build_connection_string(conn_dict)
@@ -117,7 +139,6 @@ def get_series_service(self, conn_dict=None, conn_string=""):
117139 # return CVService(SessionFactory(conn_string, self.debug))
118140
119141 def get_edit_service (self , series_id , connection ):
120-
121142 return EditService (series_id , connection = connection , debug = self .debug )
122143
123144
@@ -152,58 +173,6 @@ def _get_file(self, mode):
152173
153174 return config_file
154175
155- def _build_connection_string (self , conn_dict ):
156-
157- self ._connection_format = "%s+%s://%s:%s@%s/%s"
158-
159- if conn_dict ['engine' ] == 'mssql' and sys .platform != 'win32' :
160- driver = "pyodbc"
161- quoted = urllib .quote_plus ('DRIVER={FreeTDS};DSN=%s;UID=%s;PWD=%s;' % (conn_dict ['address' ], conn_dict ['user' ],
162- conn_dict ['password' ]))
163- # quoted = urllib.quote_plus('DRIVER={FreeTDS};DSN=%s;UID=%s;PWD=%s;DATABASE=%s' %
164- # (conn_dict['address'], conn_dict['user'], conn_dict['password'],conn_dict['db'],
165- # ))
166- conn_string = 'mssql+pyodbc:///?odbc_connect={}' .format (quoted )
167-
168- elif conn_dict ['engine' ]== 'sqlite' :
169- connformat = "%s:///%s"
170- conn_string = connformat % (conn_dict ['engine' ], conn_dict ['address' ])
171- else :
172- if conn_dict ['engine' ] == 'mssql' :
173- driver = "pyodbc"
174- conn = "%s+%s://%s:%s@%s/%s?driver=SQL+Server"
175- if "sqlncli11.dll" in os .listdir ("C:\\ Windows\\ System32" ):
176- conn = "%s+%s://%s:%s@%s/%s?driver=SQL+Server+Native+Client+11.0"
177- self ._connection_format = conn
178- conn_string = self ._connection_format % (
179- conn_dict ['engine' ], driver , conn_dict ['user' ], conn_dict ['password' ], conn_dict ['address' ],
180- conn_dict ['db' ])
181- elif conn_dict ['engine' ] == 'mysql' :
182- driver = "pymysql"
183- conn_string = self .constringBuilder (conn_dict , driver )
184- elif conn_dict ['engine' ] == 'postgresql' :
185- driver = "psycopg2"
186- conn_string = self .constringBuilder (conn_dict , driver )
187- else :
188- driver = "None"
189- conn_string = self .constringBuilder (conn_dict , driver )
190-
191-
192- # print "******", conn_string
193- return conn_string
194-
195-
196-
197- def constringBuilder (self , conn_dict , driver ):
198- if conn_dict ['password' ] is None or not conn_dict ['password' ]:
199- conn_string = self ._connection_format_nopassword % (
200- conn_dict ['engine' ], driver , conn_dict ['user' ], conn_dict ['address' ],
201- conn_dict ['db' ])
202- else :
203- conn_string = self ._connection_format % (
204- conn_dict ['engine' ], driver , conn_dict ['user' ], conn_dict ['password' ], conn_dict ['address' ],
205- conn_dict ['db' ])
206- return conn_string
207176
208177 def _save_connections (self ):
209178 f = self ._get_file ('w' )
0 commit comments