Skip to content

Commit

Permalink
new(core) finalize insert command
Browse files Browse the repository at this point in the history
  • Loading branch information
RedmanPlus committed Jan 26, 2023
1 parent eaf6480 commit 83ac1c2
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/venv
*.pyc
*.egg-info/
main.py
main.py
test
7 changes: 5 additions & 2 deletions noio_db/core/abstract_syntax_tree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from noio_db.utils.arg_manipulation import reformat_dict
from noio_db.utils.arg_manipulation import list_to_insert_vals, reformat_dict
from noio_db.utils.consts import ARG_METHOD_NAMES, KWARG_METHOD_NAMES


Expand Down Expand Up @@ -37,10 +37,13 @@ def to_dict(self) -> dict:
result_dict[k] = v

for k, v in result_dict.items():
if (k == "where" or k == "having") and isinstance(v, dict):
if (k == "where" or k == "having" or k == "insert") and isinstance(v, dict):
newargs = reformat_dict(v)
result_dict[k] = newargs[0]

if k == "insert" and isinstance(v, list):
v[2] = list_to_insert_vals(*v[2])

return result_dict


Expand Down
7 changes: 3 additions & 4 deletions noio_db/core/db_drivers/base_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ def __init__(self, parent, settings: BaseSettings):
self._cursor = self._connection.cursor()

def execute_sync(self, *args, **kwargs) -> Union[Tuple[str], None]:

query_result = self._cursor.execute(*args, **kwargs)
if query_result is None:
result = query_result.fetchall()
if not result:
self._connection.commit()
return None

return query_result.fetchall()
return result

async def execute_async(self, *args, **kwargs):

Expand Down
14 changes: 10 additions & 4 deletions noio_db/core/sql_object_factories/insert_sql_object_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ def get_object(self, *args) -> BaseSQLObject:
field_mappings = ArgInBracesSQLObject(list_into_comma_sql_object(*args[1]))

values = []
if len(args[2:]) == 1:

for val_group in args[2:]:
values = ArgInBracesSQLObject(list_into_comma_sql_object(*args[2]))

val_mappings = ArgInBracesSQLObject(list_into_comma_sql_object(*val_group))
values.append(val_mappings)
else:
for val_group in args[2:]:

values = list_into_comma_sql_object(*values)
val_mappings = ArgInBracesSQLObject(
list_into_comma_sql_object(*val_group)
)
values.append(val_mappings)

values = list_into_comma_sql_object(*values)

return InsertSQLObject(destination, field_mappings, values)
5 changes: 0 additions & 5 deletions noio_db/models/fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
class IDField:
def __init__(self, parent):
self.__counter__ = 1
self.parent = parent
self.parent_id = self.__counter__
12 changes: 4 additions & 8 deletions noio_db/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ def __init__(self, *args, **kwargs):
self.is_called_async: bool = False
super().__init__(*args, **kwargs)

@ToAsync
# @ToAsync
def insert(self):

table_name = self.table_name
table_fields = list(self.__dict__.keys())
table_fields.insert(0, "id")
table_fields.pop(0)
table_values = list(self.__dict__.values())
table_values.insert(0, self.__count__)
table_values.pop(0)

# pylint: disable=W0212
ast = AST()
Expand All @@ -94,12 +94,8 @@ def insert(self):


class Model(BaseModel, SelectMixin, CreateModelMixin, InsertMixin, ObjectCounter):

__from_orm__: bool = False

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__count__ += 1
id: int = None

@property
def table_name(self):
Expand Down
46 changes: 36 additions & 10 deletions noio_db/query/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,45 @@ def __init__(self, model, ast: AST):

self.db_driver = get_current_settings(self)

self.aiter_count = 0

def _fill_query(self):

query = self.query_constructor.compile(self.query_ast.to_dict())
self.objects = self.db_driver(query)

model_annotations = self.model_class.__annotations__
model_field_names = list(model_annotations.keys())
model_field_names.insert(0, "id")

for i, result in enumerate(self.objects):
kwargs = zip_into_dict(model_field_names, result)
self.objects[i] = self.model_class(__from_orm__=True, **kwargs)

self.called = True

def __getitem__(self, item):

if not self.called:
query = self.query_constructor.compile(self.query_ast.to_dict())
self.objects = self.db_driver(query)
self._fill_query()

model_annotations = self.model_class.__annotations__
model_field_names = list(model_annotations.keys())
model_field_names.insert(0, "id")
return self.objects[item]

for i, result in enumerate(self.objects):
kwargs = zip_into_dict(model_field_names, result)
self.objects[i] = self.model_class(__from_orm__=True, **kwargs)
def __iter__(self):

self.called = True
return self.objects[item]
if not self.called:
self._fill_query()

return iter(self.objects)

def __anext__(self):
result = self.objects[self.aiter_count]
self.aiter_count += 1
return result

def __aiter__(self):

if not self.called:
self._fill_query()

return self
1 change: 1 addition & 0 deletions noio_db/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from noio_db.utils.arg_manipulation import (
list_into_comma_sql_object,
list_to_insert_vals,
pair_up_args,
zip_into_dict,
)
Expand Down
19 changes: 18 additions & 1 deletion noio_db/utils/arg_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,32 @@ def zip_into_dict(list_a: list, list_b: list) -> dict:


def list_into_comma_sql_object(*args) -> CommaSQLObject:

root = None
for i, arg in enumerate(args):
if arg is None:
break

if i == 0:
root = CommaSQLObject(arg, args[i + 1])
continue

if i == len(args) - 1:
break

root = CommaSQLObject(root, args[i + 1])

return root


def list_to_insert_vals(*args) -> list:
result = []

for one in args:

if isinstance(one, str):
result.append(f"'{one}'")
continue

result.append(one)

return result
14 changes: 7 additions & 7 deletions noio_db/utils/async_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ class ToAsync:
def __init__(self, func):
self.func = func

async def _run_async(self, *args, **kwargs):
async def _run_async(self, ref, *args, **kwargs):

return self.func(*args, **kwargs)
return self.func(ref, *args, **kwargs)

def _run_sync(self, *args, **kwargs):
def _run_sync(self, ref, *args, **kwargs):

return self.func(*args, **kwargs)
return self.func(ref, *args, **kwargs)

def __call__(self, *args, **kwargs):
def __call__(self, ref, *args, **kwargs):

curframe = currentframe()
outframe = getouterframes(curframe, 1)

if "await" in outframe[1][4][0]:
return self._run_async(*args, **kwargs)
return self._run_async(ref, *args, **kwargs)

return self._run_sync(*args, **kwargs)
return self._run_sync(ref, *args, **kwargs)

0 comments on commit 83ac1c2

Please sign in to comment.