Skip to content

Commit

Permalink
refactor: enhance database module design
Browse files Browse the repository at this point in the history
  • Loading branch information
alimghmi committed Oct 29, 2023
1 parent 1c6356a commit bbc98c4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 26 deletions.
2 changes: 2 additions & 0 deletions database/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .connection.mssql import MSSQLDatabaseConnection
from .inserter.pandas import PandasSQLDataInserter
Empty file added database/connection/__init__.py
Empty file.
11 changes: 0 additions & 11 deletions database/base.py → database/connection/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pandas as pd
from sqlalchemy import create_engine


Expand All @@ -18,13 +17,3 @@ def disconnect(self):
if self.engine:
self.engine.dispose()
self.engine = None


class DataInserter:
"""Abstracts the data insertion to a database."""

def __init__(self, db_connection: DatabaseConnection):
self.db_connection = db_connection

def insert(self, df: pd.DataFrame, table_name: str):
raise NotImplementedError("Subclasses must implement this method.")
14 changes: 14 additions & 0 deletions database/connection/mssql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from .base import DatabaseConnection


class MSSQLDatabaseConnection(DatabaseConnection):
"""Concrete implementation for MSSQL database connection."""

def __init__(
self, server: str, database: str, username: str, password: str
) -> None:
cnx_string = (
f"mssql+pyodbc://{username}:{password}@"
f"{server}/{database}?driver=ODBC+Driver+17+for+SQL+Server"
)
super().__init__(cnx_string)
Empty file added database/inserter/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions database/inserter/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pandas as pd

from ..connection.base import DatabaseConnection


class DataInserter:
"""Abstracts the data insertion to a database."""

def __init__(self, db_connection: DatabaseConnection):
self.db_connection = db_connection

def insert(self, df: pd.DataFrame, table_name: str):
raise NotImplementedError("Subclasses must implement this method.")
16 changes: 2 additions & 14 deletions database/mssql.py → database/inserter/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,8 @@

from config import logger

from .base import DatabaseConnection, DataInserter


class MSSQLDatabaseConnection(DatabaseConnection):
"""Concrete implementation for MSSQL database connection."""

def __init__(
self, server: str, database: str, username: str, password: str
) -> None:
cnx_string = (
f"mssql+pyodbc://{username}:{password}@"
f"{server}/{database}?driver=ODBC+Driver+17+for+SQL+Server"
)
super().__init__(cnx_string)
from ..connection.base import DatabaseConnection
from .base import DataInserter


class PandasSQLDataInserter(DataInserter):
Expand Down
2 changes: 1 addition & 1 deletion utils/db_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from config import settings
from database.mssql import MSSQLDatabaseConnection, PandasSQLDataInserter
from database import MSSQLDatabaseConnection, PandasSQLDataInserter


def create_inserter_objects(*args, **kwargs) -> PandasSQLDataInserter:
Expand Down

0 comments on commit bbc98c4

Please sign in to comment.