Skip to content

Commit

Permalink
string literal types and one-one-relations queries
Browse files Browse the repository at this point in the history
  • Loading branch information
CrispenGari committed Feb 5, 2024
1 parent 2d774d5 commit 9953cd1
Show file tree
Hide file tree
Showing 12 changed files with 3,621 additions and 23,879 deletions.
26,907 changes: 3,073 additions & 23,834 deletions dataloom.sql

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dataloom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
UnsupportedDialectException,
UnsupportedTypeException,
)
from dataloom.types import Order, Include
from dataloom.model import Model
from dataloom.model import (
PrimaryKeyColumn,
Expand All @@ -20,6 +21,8 @@
)

__all__ = [
Order,
Include,
MySQLConfig,
PgConfig,
InvalidColumnValuesException,
Expand Down
33 changes: 24 additions & 9 deletions dataloom/columns/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
from dataloom.types import POSTGRES_SQL_TYPES, MYSQL_SQL_TYPES, SQLITE3_SQL_TYPES
from dataloom.types import (
POSTGRES_SQL_TYPES,
MYSQL_SQL_TYPES,
SQLITE3_SQL_TYPES,
MYSQL_SQL_TYPES_LITERAL,
POSTGRES_SQL_TYPES_LITERAL,
SQLITE3_SQL_TYPES_LITERAL,
CASCADE_LITERAL,
DIALECT_LITERAL,
)
from dataclasses import dataclass
from dataloom.exceptions import UnsupportedTypeException, UnsupportedDialectException

Expand Down Expand Up @@ -34,18 +43,20 @@ class ForeignKeyColumn:
def __init__(
self,
table,
type: str | None = None,
type: MYSQL_SQL_TYPES_LITERAL
| POSTGRES_SQL_TYPES_LITERAL
| SQLITE3_SQL_TYPES_LITERAL,
required: bool = True,
onDelete: str = "NO ACTION",
onUpdate: str = "NO ACTION",
onDelete: CASCADE_LITERAL = "NO ACTION",
onUpdate: CASCADE_LITERAL = "NO ACTION",
):
self.table = table
self.required = required
self.onDelete = onDelete
self.onUpdate = onUpdate
self.type = type

def sql_type(self, dialect: str):
def sql_type(self, dialect: DIALECT_LITERAL):
if dialect == "postgres":
if self.type in POSTGRES_SQL_TYPES:
return (
Expand Down Expand Up @@ -96,7 +107,9 @@ def sql_type(self, dialect: str):
class PrimaryKeyColumn:
def __init__(
self,
type: str,
type: MYSQL_SQL_TYPES_LITERAL
| POSTGRES_SQL_TYPES_LITERAL
| SQLITE3_SQL_TYPES_LITERAL,
length: int | None = None,
auto_increment: bool = False,
nullable: bool = False,
Expand Down Expand Up @@ -132,7 +145,7 @@ def unique_constraint(self):
def nullable_constraint(self):
return "NOT NULL" if not self.nullable else "NULL"

def sql_type(self, dialect: str):
def sql_type(self, dialect: DIALECT_LITERAL):
if dialect == "postgres":
if self.type in POSTGRES_SQL_TYPES:
if self.auto_increment:
Expand Down Expand Up @@ -179,7 +192,9 @@ def sql_type(self, dialect: str):
class Column:
def __init__(
self,
type,
type: MYSQL_SQL_TYPES_LITERAL
| POSTGRES_SQL_TYPES_LITERAL
| SQLITE3_SQL_TYPES_LITERAL,
nullable: bool = True,
unique: bool = False,
length: int | None = None,
Expand Down Expand Up @@ -220,7 +235,7 @@ def default_constraint(self):
else ""
)

def sql_type(self, dialect: str):
def sql_type(self, dialect: DIALECT_LITERAL):
if dialect == "postgres":
if self.type in POSTGRES_SQL_TYPES:
return (
Expand Down
55 changes: 46 additions & 9 deletions dataloom/loom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
from dataloom.model import Model
from dataloom.statements import GetStatement
from dataloom.conn import ConnectionOptionsFactory
from dataloom.utils import logger_function
from dataloom.utils import logger_function, get_child_table_columns
from typing import Optional
from dataloom.types import Order, Include
from dataloom.types import DIALECT_LITERAL


class Dataloom:
def __init__(
self,
database: str,
dialect: str,
dialect: DIALECT_LITERAL,
user: Optional[str] = None,
host: Optional[str] = None,
port: Optional[int] = None,
Expand Down Expand Up @@ -294,13 +296,15 @@ def find_many(
return_dict: bool = True,
limit: Optional[int] = None,
offset: Optional[int] = None,
order: Optional[list[Order]] = [],
) -> list:
sql, params, fields = instance._get_select_where_stm(
dialect=self.dialect,
args=filters,
select=select,
limit=limit,
offset=offset,
order=order,
)
data = list()
rows = self._execute_sql(sql, fetchall=True, args=params)
Expand All @@ -313,13 +317,19 @@ def find_all(
self,
instance: Model,
select: list[str] = [],
include: list[Model] = [],
include: list[Include] = [],
return_dict: bool = True,
limit: Optional[int] = None,
offset: Optional[int] = None,
order: Optional[list[Order]] = [],
) -> list:
sql, params, fields = instance._get_select_where_stm(
dialect=self.dialect, select=select, limit=limit, offset=offset
dialect=self.dialect,
select=select,
limit=limit,
offset=offset,
order=order,
include=include,
)
data = list()
rows = self._execute_sql(sql, fetchall=True)
Expand All @@ -328,30 +338,57 @@ def find_all(
data.append(json if return_dict else instance(**json))
return data

def __map_relationships(
self,
instance: Model,
row: tuple,
parent_fields: list,
include: list[Include] = [],
return_dict: bool = True,
):
# how are relations are mapped?
json = dict(zip(parent_fields, row[: len(parent_fields)]))
result = json if return_dict else instance(**json)
row = row[len(parent_fields) :]
for _include in include:
alias, selected = [v for v in get_child_table_columns(_include).items()][0]
child_json = dict(zip(selected, row[: len(selected)]))
row = row[len(selected) :]
if return_dict:
result[alias] = child_json
else:
result[alias] = _include.model(**child_json)
return result

def find_by_pk(
self,
instance: Model,
pk,
select: list[str] = [],
include: list[Model] = [],
include: list[Include] = [],
return_dict: bool = True,
):
# what is the name of the primary key column? well we will find out
sql, fields = instance._get_select_by_pk_stm(
dialect=self.dialect, select=select
dialect=self.dialect, select=select, include=include
)
row = self._execute_sql(sql, args=(pk,), fetchone=True)
if row is None:
return None
json = dict(zip(fields, row))
return json if return_dict else instance(**json)
return self.__map_relationships(
instance=instance,
row=row,
parent_fields=fields,
include=include,
return_dict=return_dict,
)

def find_one(
self,
instance: Model,
filters: dict = {},
select: list[str] = [],
include: list[Model] = [],
include: list[Include] = [],
return_dict: bool = True,
offset: Optional[int] = None,
):
Expand Down
Loading

0 comments on commit 9953cd1

Please sign in to comment.