Skip to content

Commit

Permalink
Merge pull request #727 from eschizoid/feature/refactor-driver
Browse files Browse the repository at this point in the history
Feature/refactor driver
  • Loading branch information
argenisleon authored Nov 10, 2019
2 parents d3d838b + 70930d6 commit 491dc78
Show file tree
Hide file tree
Showing 25 changed files with 2,311 additions and 385 deletions.
56 changes: 56 additions & 0 deletions optimus/io/abstract_driver.py
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
26 changes: 26 additions & 0 deletions optimus/io/cassandra.py
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
32 changes: 0 additions & 32 deletions optimus/io/driver.py

This file was deleted.

33 changes: 33 additions & 0 deletions optimus/io/driver_context.py
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)
52 changes: 52 additions & 0 deletions optimus/io/factory.py
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()])
Loading

0 comments on commit 491dc78

Please sign in to comment.