-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modify client to use mssql, mariadb and postgresql
- Loading branch information
1 parent
f731849
commit 56ffa89
Showing
3 changed files
with
37 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,36 @@ | ||
import pymssql | ||
import sqlalchemy | ||
import logging | ||
|
||
|
||
class DatabaseClient: | ||
def __init__(self, database, username, password, host): | ||
self.database = database | ||
self.username = username | ||
self.password = password | ||
self.host = host | ||
def __init__(self, db_type, database, username, password, host, port=None): | ||
if db_type.lower() == 'mssql': | ||
driver = 'mssql+pymssql' | ||
elif db_type.lower() == 'mariadb': | ||
driver = 'mariadb+mariadbconnector' | ||
elif db_type.lower() == 'postgresql': | ||
driver = 'postgresql+psycopg2' | ||
else: | ||
raise ValueError(f"Invalid database type {type}") | ||
|
||
self.logger = logging.getLogger(__name__) | ||
|
||
self.connection = None | ||
self.cursor = None | ||
if port: | ||
host = host + f':{port}' | ||
|
||
self.engine = sqlalchemy.create_engine(f'{driver}://{username}:{password}@{host}/{database}') | ||
|
||
def get_connection(self): | ||
try: | ||
if not self.connection: | ||
self.connection = pymssql.connect(host=self.host, user=self.username, password=self.password, database=self.database) | ||
return self.connection | ||
if self.engine: | ||
return self.engine.connect() | ||
self.logger.error("DatabaseClient not initialized properly. Engine is None. Check error from init.") | ||
except Exception as e: | ||
self.logger.error(f"Error connecting to database: {e}") | ||
|
||
def get_cursor(self): | ||
try: | ||
if not self.cursor: | ||
self.cursor = self.get_connection().cursor() | ||
return self.cursor | ||
except Exception as e: | ||
self.logger.error(f"Error getting cursor: {e}") | ||
|
||
def execute_sql(self, sql): | ||
try: | ||
cur = self.get_cursor() | ||
cur.execute(sql) | ||
return cur.fetchall() | ||
with self.get_connection() as conn: | ||
return conn.execute(sqlalchemy.text(sql)) | ||
except Exception as e: | ||
self.logger.error(f"Error executing SQL: {e}") | ||
self.logger.error(f"Error executing SQL: {e}") |