44
55AUTHOR: David J. Lambert
66
7- VERSION: 0.7.5
7+ VERSION: 0.7.6
88
9- DATE: Apr 20 , 2020
9+ DATE: Jul 9 , 2020
1010"""
11- from MyConstants import *
12- from MyFunctions import *
11+ from functions import print_stacktrace , is_file_in_path
12+ from constants import ACCESS , MYSQL , ORACLE , POSTGRESQL , SQLITE , SQLSERVER
13+ import constants as c
1314
1415
15- # noinspection PyUnresolvedReferences
1616class DBInstance (object ):
1717 """ Class containing database connection utilities and information
1818 about one database instance (referred to as "this database").
@@ -68,20 +68,20 @@ def __init__(self,
6868 self .instance : str = instance
6969
7070 # Check if db_type valid.
71- if self .db_type not in db_types :
71+ if self .db_type not in c . DB_TYPES :
7272 print ('Invalid database type "{}".' .format (self .db_type ))
7373 # Nothing to clean up.
7474 exit (1 )
7575
7676 # Import appropriate database library.
77- self .db_lib_name = lib_name_for_db [self .db_type ]
77+ self .db_lib_name = c . LIB_NAME_FOR_DB [self .db_type ]
7878 self .db_lib_obj = __import__ (self .db_lib_name )
7979
8080 # Appropriate database client executable.
81- self .db_client_exe = db_client_exes [self .db_type ]
81+ self .db_client_exe = c . DB_CLIENT_EXES [self .db_type ]
8282
8383 # Get database library version.
84- if self .db_lib_name in {psycopg2 , pymysql }:
84+ if self .db_lib_name in {c . PSYCOPG2 , c . PYMYSQL }:
8585 self .db_lib_version = self .db_lib_obj .__version__
8686 else :
8787 self .db_lib_version = self .db_lib_obj .version
@@ -95,22 +95,22 @@ def __init__(self,
9595 # paramstyle = 'pyformat': pymysql and psycopg2.
9696
9797 # Get the parameter style we're using.
98- self .paramstyle = paramstyle_for_lib [self .db_lib_name ]
99- if self .db_type == access :
100- self .paramstyle = nobindvars
98+ self .paramstyle = c . PARAMSTYLE_FOR_LIB [self .db_lib_name ]
99+ if self .db_type == ACCESS :
100+ self .paramstyle = c . NOBINDVARS
101101
102102 # Initialize bind_vars.
103- if self .paramstyle in {named , pyformat }:
103+ if self .paramstyle in {c . NAMED , c . PYFORMAT }:
104104 self .bind_vars = dict ()
105- elif self .paramstyle == qmark :
105+ elif self .paramstyle == c . QMARK :
106106 self .bind_vars = tuple ()
107107 else :
108108 self .bind_vars = None
109109
110110 # Connect to database instance.
111111 self .connection = None
112112 try :
113- if db_type in uses_connection_string :
113+ if db_type in c . USES_CONNECTION_STRING :
114114 z = self .get_db_connection_string ()
115115 self .connection = self .db_lib_obj .connect (z )
116116 else :
@@ -151,7 +151,7 @@ def close_connection(self, del_cursors: bool = False) -> None:
151151 else :
152152 print ('Dependent DBClients exist, will not close connection.' )
153153
154- if self .db_type in file_databases :
154+ if self .db_type in c . FILE_DATABASES :
155155 z = '\n {} from database at "{}".'
156156 z = z .format ('{}' , self .db_path )
157157 else :
@@ -209,7 +209,7 @@ def get_connection_status(self) -> str:
209209 Parameters:
210210 Returns:
211211 """
212- if self .db_type in file_databases :
212+ if self .db_type in c . FILE_DATABASES :
213213 z = 'Connection status for the database at "{}": {}connected.'
214214 z = z .format (self .db_path , '{}' )
215215 else :
@@ -270,21 +270,21 @@ def get_db_software_version(self) -> str:
270270 db_software_version (str): database software version.
271271 """
272272 sql = {
273- mysql : 'SELECT version()' ,
274- postgresql : 'SELECT version()' ,
275- oracle : "SELECT * FROM v$version WHERE banner LIKE 'Oracle%'" ,
276- sqlite : 'SELECT sqlite_version()' ,
277- sqlserver : 'SELECT @@VERSION' }.get (self .db_type , 'Nada' )
273+ MYSQL : 'SELECT version()' ,
274+ POSTGRESQL : 'SELECT version()' ,
275+ ORACLE : "SELECT * FROM v$version WHERE banner LIKE 'Oracle%'" ,
276+ SQLITE : 'SELECT sqlite_version()' ,
277+ SQLSERVER : 'SELECT @@VERSION' }.get (self .db_type , 'Nada' )
278278
279279 if sql [0 :6 ] == 'SELECT' :
280280 cursor = self .connection .cursor ()
281281 cursor .execute (sql )
282282 version = cursor .fetchone ()[0 ]
283283 cursor .close ()
284284 del cursor
285- elif self .db_type == access :
285+ elif self .db_type == ACCESS :
286286 version = "unavailable for MS Access through SQL"
287- elif self .db_lib_name != pyodbc :
287+ elif self .db_lib_name != c . PYODBC :
288288 # This is for future use.
289289 version = self .connection .version
290290 else :
@@ -300,32 +300,32 @@ def get_db_connection_string(self) -> str:
300300 db_software_version (str): database software version.
301301 """
302302 z = ''
303- if self .db_lib_name == cx_Oracle :
303+ if self .db_lib_name == c . CX_ORACLE :
304304 z = '{}/{}@{}:{}/{}'
305- elif self .db_lib_name == psycopg2 :
305+ elif self .db_lib_name == c . PSYCOPG2 :
306306 z = "user='{}' password='{}' host='{}' port='{}' dbname='{}'"
307- elif self .db_lib_name == pymysql :
307+ elif self .db_lib_name == c . PYMYSQL :
308308 z = ''
309- elif self .db_lib_name == pyodbc :
310- if self .db_type == access :
309+ elif self .db_lib_name == c . PYODBC :
310+ if self .db_type == ACCESS :
311311 z = ('DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};'
312312 'DBQ={};' )
313- elif self .db_type == sqlserver :
313+ elif self .db_type == c . SQLSERVER :
314314 z = ('DRIVER={{SQL Server}};'
315315 'UID={};PWD={};SERVER={};PORT={};DATABASE={}' )
316- elif self .db_lib_name == sqlite3 :
316+ elif self .db_lib_name == c . SQLITE3 :
317317 z = '{}'
318318 else :
319319 print ('Unknown db library "{}", aborting.' .format (self .db_lib_name ))
320320 self .close_connection ()
321321 exit (1 )
322322
323- if self .db_type in {oracle , postgresql , sqlserver }:
323+ if self .db_type in {ORACLE , POSTGRESQL , SQLSERVER }:
324324 z = z .format (self .username , self .password , self .hostname ,
325325 self .port_num , self .instance )
326- elif self .db_type in file_databases :
326+ elif self .db_type in c . FILE_DATABASES :
327327 z = z .format (self .db_path )
328- elif self .db_type == mysql :
328+ elif self .db_type == MYSQL :
329329 pass
330330
331331 return z
@@ -340,7 +340,7 @@ def print_all_connection_parameters(self) -> None:
340340 print ('The database type is "{}".' .format (self .db_type ))
341341 z = 'The database software version is {}.'
342342 print (z .format (self .db_software_version ))
343- if self .db_type in file_databases :
343+ if self .db_type in c . FILE_DATABASES :
344344 print ('The database path is "{}".' .format (self .db_path ))
345345 else :
346346 print ('The database username is "{}".' .format (self .username ))
@@ -361,26 +361,26 @@ def get_cmdline_list(self) -> list:
361361 args = (self .username , self .password , self .hostname ,
362362 self .port_num , self .instance )
363363
364- if self .db_type == access or self .db_client_exe == '' :
364+ if self .db_type == ACCESS or self .db_client_exe == '' :
365365 z = '{} DOES NOT HAVE A COMMAND LINE INTERFACE.'
366366 z = z .format (self .db_type ).upper ()
367367 cmd = ['Error' , z ]
368368 elif not is_file_in_path (self .os , self .db_client_exe ):
369369 z = 'Did not find {} in PATH.' .format (self .db_client_exe )
370370 cmd = ['Error' , z ]
371- elif self .db_type == mysql :
371+ elif self .db_type == MYSQL :
372372 conn_str = '--uri={}:{}@{}:{}/{}' .format (* args )
373373 cmd = [self .db_client_exe , conn_str ,
374374 '--table' , '--sql' , '--quiet-start' ]
375- elif self .db_type == oracle :
375+ elif self .db_type == ORACLE :
376376 conn_str = '{}/{}@{}:{}/{}' .format (* args )
377377 cmd = [self .db_client_exe , conn_str ]
378- elif self .db_type == postgresql :
378+ elif self .db_type == POSTGRESQL :
379379 conn_str = 'postgresql://{}:{}@{}:{}/{}' .format (* args )
380380 cmd = [self .db_client_exe , '-d' , conn_str ]
381- elif self .db_type == sqlite :
381+ elif self .db_type == SQLITE :
382382 cmd = [self .db_client_exe , self .db_path ]
383- elif self .db_type == sqlserver :
383+ elif self .db_type == SQLSERVER :
384384 host_port = '{},{}' .format (self .hostname , self .port_num )
385385 cmd = [self .db_client_exe , '-U' , self .username , '-P' , self .password ,
386386 '-S' , host_port , '-d' , self .instance ]
0 commit comments