Skip to content

Commit

Permalink
supporting-other-dialects
Browse files Browse the repository at this point in the history
  • Loading branch information
CrispenGari committed Feb 2, 2024
1 parent 0dc3e55 commit 5a3b707
Show file tree
Hide file tree
Showing 26 changed files with 1,844 additions and 829 deletions.
26 changes: 25 additions & 1 deletion dataloom/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
from dataloom.db import Dataloom
from dataloom import exceptions
from dataloom.model import Model
from dataloom.model.column import (
PrimaryKeyColumn,
CreatedAtColumn,
ForeignKeyColumn,
UpdatedAtColumn,
Column,
)

dataloom = Dataloom()
Dataloom = Dataloom
# Columns
PrimaryKeyColumn = PrimaryKeyColumn
CreatedAtColumn = CreatedAtColumn
ForeignKeyColumn = ForeignKeyColumn
UpdatedAtColumn = UpdatedAtColumn
Column = Column


# exceptions
PkNotDefinedException = exceptions.PkNotDefinedException
TooManyPkException = exceptions.TooManyPkException
UnsupportedDialectException = exceptions.UnsupportedDialectException

# models
Model = Model
44 changes: 25 additions & 19 deletions dataloom/conn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from dataclasses import dataclass, field
from dataloom.exceptions import UnsupportedDialect
from dataloom.exceptions import UnsupportedDialectException
from sqlite3.dbapi2 import Connection
from os import PathLike
from typing_extensions import TypeAlias
from typing import Optional

from dataloom.constants import instances


StrOrBytesPath: TypeAlias = str | bytes | PathLike[str] | PathLike[bytes]

Expand All @@ -16,12 +20,12 @@ def connection_options[T](self) -> T:
@dataclass(kw_only=True)
class PostgresDialect(Dialect):
# "postgres://postgres:postgres@localhost:5432/db"
connection_string: str | None = field(default=None)
connection_string: Optional[str] = field(default=None)
database: str
user: str | None = field(default="postgres")
host: str | None = field(default="localhost")
port: int | None = field(default=5432)
password: str | None = field(default="postgres")
user: Optional[str] = field(default=instances["postgres"].get("user"))
host: Optional[str] = field(default=instances["postgres"].get("host"))
port: Optional[int] = field(default=instances["postgres"].get("port"))
password: Optional[str] = field(default=instances["postgres"].get("user"))

def connection_options[T](self) -> T:
return (
Expand All @@ -31,12 +35,12 @@ def connection_options[T](self) -> T:

@dataclass(kw_only=True)
class MySQLDialect(Dialect):
connection_string: str | None = field(default=None)
connection_string: Optional[str] = field(default=None)
database: str
user: str | None = field(default="root")
host: str | None = field(default="localhost")
port: int | None = field(default=3306)
password: str | None = field(default="root")
user: Optional[str] = field(default=instances["mysql"].get("user"))
host: Optional[str] = field(default=instances["mysql"].get("host"))
port: Optional[int] = field(default=instances["mysql"].get("port"))
password: Optional[str] = field(default=instances["mysql"].get("password"))

def connection_options[T](self) -> T:
return (
Expand All @@ -46,14 +50,14 @@ def connection_options[T](self) -> T:

@dataclass(kw_only=True)
class SQLiteDialect(Dialect):
database: StrOrBytesPath = field(default="users.db")
timeout: float | None = field(default=None)
detect_types: int | None = field(default=None)
isolation_level: str | None = field(default=None)
database: StrOrBytesPath = field(default=instances["sqlite"].get("database"))
timeout: Optional[float] = field(default=None)
detect_types: Optional[int] = field(default=None)
isolation_level: Optional[str] = field(default=None)
check_same_thread: bool = field(default=None)
factory: type[Connection] | None = field(default=None)
cached_statements: int | None = field(default=None)
uri: bool | None = field(default=None)
cached_statements: Optional[int] = field(default=None)
uri: Optional[bool] = field(default=None)

def connection_options[T](self) -> T:
return vars(self)
Expand All @@ -62,7 +66,9 @@ def connection_options[T](self) -> T:
@dataclass
class ConnectionOptionsFactory:
@staticmethod
def get_connection_options(dialect, **kwargs):
def get_connection_options(**kwargs):
dialect = kwargs.get("dialect")
kwargs = {k: v for k, v in kwargs.items() if k != "dialect"}
if dialect == "postgres":
return {
k: v
Expand All @@ -78,6 +84,6 @@ def get_connection_options(dialect, **kwargs):
if v is not None
}
else:
raise UnsupportedDialect(
raise UnsupportedDialectException(
"The dialect passed is not supported the supported dialects are: {'postgres', 'mysql', 'sqlite'}"
)
10 changes: 9 additions & 1 deletion dataloom/constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
"port": 5432,
"user": "postgres",
"password": "postgres",
"host": "localhost" or "127.0.0.1",
},
"mysql": {
"type": "mysql",
"port": 3306,
"user": "root",
"password": "root",
"host": "127.0.0.1" or "localhost",
}
},
"sqlite": {"database": "dataloom_instance.db", "type": "sqlite"},
}
Loading

0 comments on commit 5a3b707

Please sign in to comment.