diff --git a/asyncdb/models/base.py b/asyncdb/models/base.py index 81f8527f..270c7049 100644 --- a/asyncdb/models/base.py +++ b/asyncdb/models/base.py @@ -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): """ @@ -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 """ @@ -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 @@ -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. @@ -801,18 +801,18 @@ 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): @@ -820,17 +820,17 @@ async def remove(cls, conditions: dict = {}, **kwargs): 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] @@ -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 @@ -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): @@ -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 @@ -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. @@ -942,22 +944,22 @@ 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, ): """ @@ -965,7 +967,7 @@ async def makeModel( 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 = [] @@ -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 diff --git a/asyncdb/providers/sql.py b/asyncdb/providers/sql.py index c2bf50a8..3eadd92c 100644 --- a/asyncdb/providers/sql.py +++ b/asyncdb/providers/sql.py @@ -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: @@ -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. """ @@ -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) @@ -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}') @@ -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): @@ -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. """ @@ -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) @@ -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: @@ -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): @@ -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) @@ -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): @@ -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 @@ -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): diff --git a/asyncdb/version.py b/asyncdb/version.py index b0ac27b0..1527d78c 100644 --- a/asyncdb/version.py +++ b/asyncdb/version.py @@ -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'