Skip to content

Commit

Permalink
fixing using word conditions making clash of parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
phenobarbital committed Jul 13, 2022
1 parent c84cb83 commit d0c2d4e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
56 changes: 29 additions & 27 deletions asyncdb/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ async def fetch(self, **kwargs):
raise Exception(
"Error on get {}: {}".format(self.Meta.name, err))

get = fetch
# get = fetch

async def select(self, **kwargs):
"""
Expand Down Expand Up @@ -749,7 +749,7 @@ async def select(self, **kwargs):
raise Exception(
"Error on filter {}: {}".format(self.Meta.name, err))

async def all(self, **kwargs):
async def fetch_all(self, **kwargs):
"""
Need to return all rows as a ***collection*** of nested DataClasses
"""
Expand All @@ -764,8 +764,7 @@ async def all(self, **kwargs):
return [self.__class__(**dict(r)) for r in result]
else:
raise NoDataFound(
"No Data on {} with condition {}".format(
self.Meta.name, kwargs)
f"No Data on {self.Meta.name} with condition {kwargs}"
)
except NoDataFound:
raise
Expand All @@ -774,7 +773,8 @@ async def all(self, **kwargs):
except Exception as err:
logging.debug(traceback.format_exc())
raise Exception(
"Error on filter {}: {}".format(self.Meta.name, err))
f"Error on filter {self.Meta.name}: {err}"
)

"""
Class-based methods for Dataclasses.
Expand All @@ -801,36 +801,36 @@ async def create(cls, records):
except Exception as err:
logging.debug(traceback.format_exc())
raise Exception(
"Error Updating Table {}: {}".format(cls.Meta.name, err)
)
f"Error Updating Table {cls.Meta.name}: {err}"
) from err

@classmethod
async def remove(cls, conditions: dict = {}, **kwargs):
async def remove(cls, _filter: Dict = None, **kwargs):
if not cls.Meta.connection:
cls.get_connection(cls)
async with await cls.Meta.connection.connection() as conn:
result = []
try:
result = await cls.Meta.connection.mdl_delete(
model=cls, conditions=conditions, **kwargs
model=cls, _filter=_filter, **kwargs
)
return result
except (StatementError, ProviderError):
raise
except Exception as err:
logging.debug(traceback.format_exc())
raise Exception(
"Error Deleting Table {}: {}".format(cls.Meta.name, err)
f"Error Deleting Table {cls.Meta.name}: {err}"
)

@classmethod
async def update(cls, conditions: dict = {}, **kwargs):
async def update(cls, _filter: Dict = None, **kwargs):
if not cls.Meta.connection:
cls.get_connection(cls)
async with await cls.Meta.connection.connection() as conn:
try:
result = await cls.Meta.connection.mdl_update(
model=cls, conditions=conditions, **kwargs
model=cls, _filter=_filter, **kwargs
)
if result:
return [cls(**dict(r)) for r in result]
Expand All @@ -841,7 +841,7 @@ async def update(cls, conditions: dict = {}, **kwargs):
except Exception as err:
print(traceback.format_exc())
raise Exception(
"Error Updating Table {}: {}".format(cls.Meta.name, err)
f"Error Updating Table {cls.Meta.name}: {err}"
)

@classmethod
Expand All @@ -868,7 +868,8 @@ async def filter(cls, **kwargs):
except Exception as err:
logging.debug(traceback.format_exc())
raise Exception(
"Error on filter {}: {}".format(cls.Meta.name, err))
f"Error on filter {cls.Meta.name}: {err}"
)

@classmethod
async def get(cls, **kwargs):
Expand All @@ -890,13 +891,15 @@ async def get(cls, **kwargs):
message=f"Data not found over {cls.Meta.name!s}")
except AttributeError as err:
raise Exception(
"Error on get {}: {}".format(cls.Meta.name, err))
f"Error on get {cls.Meta.name}: {err}"
)
except (StatementError, ProviderError):
raise
except Exception as err:
print(traceback.format_exc())
raise Exception(
"Error on get {}: {}".format(cls.Meta.name, err))
f"Error on get {cls.Meta.name}: {err}"
)

# get all data
@classmethod
Expand All @@ -912,8 +915,7 @@ async def all(cls, **kwargs):
except Exception as err:
print(traceback.format_exc())
raise Exception(
"Error on query_all over table {}: {}".format(
cls.Meta.name, err)
f"Error on query_all over table {cls.Meta.name}: {err}"
)
"""
Class-based methods for dataclass creation, model creation.
Expand Down Expand Up @@ -942,30 +944,30 @@ def model(cls, dialect: str = "json") -> Any:
return result

@classmethod
def make_model(cls, name: str, schema: str = "public", fields: list = []):
def make_model(cls, name: str, schema: str = "public", fields: List = None):
parent = inspect.getmro(cls)
cls = make_dataclass(name, fields, bases=(parent[0],))
obj = make_dataclass(name, fields, bases=(parent[0],))
m = Meta()
m.name = name
m.schema = schema
m.app_label = schema
cls.Meta = m
return cls
obj.Meta = m
return obj

@classmethod
async def makeModel(
cls,
name: str,
schema: str = "public",
fields: list = [],
fields: List = None,
db: "ConnectionBackend" = None,
):
"""
Make Model.
Making a model from field tuples, a JSON schema or a Table.
"""
tablename = "{}.{}".format(schema, name)
tablename = f"{schema}.{name}"
if not fields: # we need to look in to it.
colinfo = await db.column_info(tablename)
fields = []
Expand All @@ -983,15 +985,15 @@ async def makeModel(
dtype = str
fields.append((column["name"], dtype, col))
parent = inspect.getmro(cls)
cls = make_dataclass(name, fields, bases=(parent[0],))
obj = make_dataclass(name, fields, bases=(parent[0],))
m = Meta()
m.name = name
m.schema = schema
m.app_label = schema
m.connection = db
m.frozen = False
cls.Meta = m
return cls
obj.Meta = m
return obj

Meta = Meta
Meta = Meta
29 changes: 14 additions & 15 deletions asyncdb/providers/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def _where(self, fields: Dict, **where):
Re-think functionality for parsing where conditions.
"""
result = ""
if not fields:
fields = {}
if not where:
return result
elif type(where) == str:
Expand Down Expand Up @@ -148,7 +150,7 @@ def _where(self, fields: Dict, **where):
else:
return result

async def mdl_update(self, model: "Model", conditions: dict, **kwargs):
async def mdl_update(self, model: "Model", _filter: Dict, **kwargs):
"""
Updating some records and returned.
"""
Expand All @@ -159,7 +161,7 @@ async def mdl_update(self, model: "Model", conditions: dict, **kwargs):
source = []
cols = []
fields = model.columns(model)
for name, field in fields.items():
for _, field in fields.items():
column = field.name
datatype = field.type
cols.append(column)
Expand All @@ -170,7 +172,7 @@ async def mdl_update(self, model: "Model", conditions: dict, **kwargs):
column, Entity.escapeLiteral(value, datatype))
)
set_fields = ", ".join(source)
condition = self._where(fields, **conditions)
condition = self._where(fields, **_filter)
columns = ", ".join(cols)
sql = f"UPDATE {table} SET {set_fields} {condition}"
logging.debug(f'UPDATE SQL: {sql}')
Expand All @@ -182,8 +184,7 @@ async def mdl_update(self, model: "Model", conditions: dict, **kwargs):
except Exception as err:
print(traceback.format_exc())
raise ProviderError(
message="Error on Insert over table {}: {}".format(
model.Meta.name, err)
f"Error on Insert over table {model.Meta.name}: {err}"
)

async def mdl_create(self, model: "Model", rows: list):
Expand Down Expand Up @@ -258,7 +259,7 @@ async def mdl_create(self, model: "Model", rows: list):
raise ProviderError(message="Error Bulk Insert {}: {}".format(table, err))
return results

async def mdl_delete(self, model: "Model", conditions: Dict, **kwargs):
async def mdl_delete(self, model: "Model", _filter: Dict, **kwargs):
"""
Deleting some records and returned.
"""
Expand All @@ -269,7 +270,7 @@ async def mdl_delete(self, model: "Model", conditions: Dict, **kwargs):
source = []
cols = []
fields = model.columns(model)
for name, field in fields.items():
for _, field in fields.items():
column = field.name
datatype = field.type
cols.append(column)
Expand All @@ -279,7 +280,7 @@ async def mdl_delete(self, model: "Model", conditions: Dict, **kwargs):
"{} = {}".format(
column, Entity.escapeLiteral(value, datatype))
)
condition = self._where(fields, **conditions)
condition = self._where(fields, **_filter)
sql = f"DELETE FROM {table} {condition}"
logging.debug(sql)
try:
Expand All @@ -289,7 +290,7 @@ async def mdl_delete(self, model: "Model", conditions: Dict, **kwargs):
except Exception as err:
print(traceback.format_exc())
raise ProviderError(
message="Error on Deleting table {}: {}".format(model.Meta.name, err)
f"Error on Deleting table {model.Meta.name}: {err}"
)

async def mdl_filter(self, model: "Model", **kwargs):
Expand All @@ -303,7 +304,7 @@ async def mdl_filter(self, model: "Model", **kwargs):
table = f"{model.Meta.schema}.{model.Meta.name}"
except Exception:
table = model.__name__
for name, field in fields.items():
for _, field in fields.items():
column = field.name
datatype = field.type
cols.append(column)
Expand All @@ -322,8 +323,7 @@ async def mdl_filter(self, model: "Model", **kwargs):
except Exception as err:
logging.debug(traceback.format_exc())
raise ProviderError(
message="Error FILTER: over table {}: {}".format(
model.Meta.name, err)
f"Error FILTER: over table {model.Meta.name}: {err}"
)

async def mdl_all(self, model: "Model", **kwargs):
Expand Down Expand Up @@ -360,7 +360,7 @@ async def mdl_get(self, model: "Model", **kwargs):
pk = {}
cols = []
fields = model.columns(model)
for name, field in fields.items():
for _, field in fields.items():
val = getattr(model, field.name)
column = field.name
datatype = field.type
Expand Down Expand Up @@ -462,8 +462,7 @@ async def model_select(self, model: "Model", fields: Dict = None, **kwargs):
except Exception as err:
logging.debug(traceback.format_exc())
raise ProviderError(
message="Error on SELECT over {}: {}".format(
model.Meta.name, err)
f"Error on SELECT over {model.Meta.name}: {err}"
)

async def model_all(self, model: "Model", fields: Dict = None):
Expand Down
2 changes: 1 addition & 1 deletion asyncdb/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__title__ = 'asyncdb'
__description__ = ('Library for Asynchronous data source connections '
'Collection of asyncio drivers.')
__version__ = '2.0.31'
__version__ = '2.0.32'
__author__ = 'Jesus Lara'
__author_email__ = 'jesuslarag@gmail.com'
__license__ = 'BSD'

0 comments on commit d0c2d4e

Please sign in to comment.