-
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.
Merge pull request #727 from eschizoid/feature/refactor-driver
Feature/refactor driver
- Loading branch information
Showing
25 changed files
with
2,311 additions
and
385 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from abc import abstractmethod, ABC | ||
from enum import Enum | ||
|
||
|
||
class AbstractDriver(ABC): | ||
"""Base class for all database implementations""" | ||
|
||
@abstractmethod | ||
def properties(self) -> Enum: | ||
""" | ||
Returns an enum containing the following database properties: | ||
* name | ||
* port | ||
* java_class | ||
* table_name | ||
:return: en enum of type ``DriverProperties`` | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def url(self, *args, **kwargs) -> str: | ||
""" | ||
Returns a string connection to the underlying database implementation. | ||
:param kwargs: connection properties | ||
:return: the connection string | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def table_names_query(self, *args, **kwargs) -> str: | ||
""" | ||
Returns the query for all table names in the database. | ||
:param kwargs: query parameters | ||
:return: a query for all the tables in a given database | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def table_name_query(self, *args, **kwargs) -> str: | ||
""" | ||
Returns the query for a table name in the database. | ||
:param kwargs: query parameters | ||
:return: a query for all the tables in a given database | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def count_query(self, *args, **kwargs) -> str: | ||
""" | ||
Returns the query for counting the rows in a given table. | ||
:param kwargs: query parameters | ||
:return: a query to count the number of rows in a table | ||
""" | ||
pass |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from enum import Enum | ||
|
||
from singleton_decorator import singleton | ||
|
||
from optimus.io.abstract_driver import AbstractDriver | ||
from optimus.io.properties import DriverProperties | ||
|
||
|
||
@singleton | ||
class CassandraDriver(AbstractDriver): | ||
"""Cassandra Database""" | ||
|
||
def properties(self) -> Enum: | ||
return DriverProperties.CASSANDRA | ||
|
||
def url(self, *args, **kwargs) -> str: | ||
return "" | ||
|
||
def table_names_query(self, *args, **kwargs) -> str: | ||
pass | ||
|
||
def table_name_query(self, *args, **kwargs) -> str: | ||
pass | ||
|
||
def count_query(self, *args, **kwargs) -> str: | ||
pass |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from enum import Enum | ||
|
||
from optimus.io.abstract_driver import AbstractDriver | ||
|
||
|
||
class DriverContext: | ||
"""Driver context holding a reference to the underlying driver implementation""" | ||
|
||
def __init__(self, driver: AbstractDriver) -> None: | ||
self._driver = driver | ||
|
||
@property | ||
def driver(self) -> AbstractDriver: | ||
return self._driver | ||
|
||
@driver.setter | ||
def driver(self, driver: AbstractDriver) -> None: | ||
self._driver = driver | ||
|
||
def properties(self) -> Enum: | ||
return self._driver.properties() | ||
|
||
def url(self, *args, **kwargs) -> str: | ||
return self._driver.url(*args, **kwargs) | ||
|
||
def table_names_query(self, *args, **kwargs) -> str: | ||
return self._driver.table_names_query(*args, **kwargs) | ||
|
||
def table_name_query(self, *args, **kwargs) -> str: | ||
return self._driver.table_name_query(*args, **kwargs) | ||
|
||
def count_query(self, *args, **kwargs) -> str: | ||
return self._driver.count_query(*args, **kwargs) |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from optimus.helpers.raiseit import RaiseIt | ||
from optimus.io.abstract_driver import AbstractDriver | ||
from optimus.io.cassandra import CassandraDriver | ||
from optimus.io.mysql import MySQLDriver | ||
from optimus.io.oracle import OracleDriver | ||
from optimus.io.postgresql import PostgreSQLDriver | ||
from optimus.io.presto import PrestoDriver | ||
from optimus.io.properties import DriverProperties | ||
from optimus.io.redshift import RedshiftDriver | ||
from optimus.io.sqlite import SQLiteDriver | ||
from optimus.io.sqlserver import SQLServerDriver | ||
|
||
|
||
class DriverFactory: | ||
"""Database driver factory. This database driver factory currently supports the following implementations: | ||
* Cassandra | ||
* MySQL | ||
* Oracle | ||
* Postgres | ||
* Presto | ||
* Redshift | ||
* SQLite | ||
* SQLServer | ||
""" | ||
|
||
@staticmethod | ||
def get(driver_type) -> AbstractDriver: | ||
""" | ||
Returns a driver implementation given a database name | ||
:param driver_type: name of the database | ||
:return: a database driver | ||
""" | ||
if driver_type == DriverProperties.CASSANDRA.value["name"]: | ||
return CassandraDriver() | ||
elif driver_type == DriverProperties.MYSQL.value["name"]: | ||
return MySQLDriver() | ||
elif driver_type == DriverProperties.ORACLE.value["name"]: | ||
return OracleDriver() | ||
elif driver_type == DriverProperties.POSTGRESQL.value["name"]: | ||
return PostgreSQLDriver() | ||
elif driver_type == DriverProperties.PRESTO.value["name"]: | ||
return PrestoDriver() | ||
elif driver_type == DriverProperties.REDSHIFT.value["name"]: | ||
return RedshiftDriver() | ||
elif driver_type == DriverProperties.SQLITE.value["name"]: | ||
return SQLiteDriver() | ||
elif driver_type == DriverProperties.SQLSERVER.value["name"]: | ||
return SQLServerDriver() | ||
else: | ||
RaiseIt.value_error(driver_type, [database["name"] for database in DriverProperties.list()]) |
Oops, something went wrong.