def _search( self, fields: Dict[str, EntityID], model: Type[EntityT], ) -> List[EntityT]: """Get the entities whose attributes match one or several conditions. Particular implementation of the database adapter. Args: model: Entity class to obtain. fields: Dictionary with the {key}:{value} to search. Returns: entities: List of Entity object that matches the search criteria. """ table = self._table_model(model) query = Query.from_(table).select("*") for key, value in fields.items(): if key == "id_": key = "id" if isinstance(value, str): query = query.where( functions.Lower(getattr(table, key)).regexp(value.lower())) else: query = query.where(getattr(table, key) == value) return self._build_entities(model, query)
def _search( self, fields: Dict[str, EntityID], models: OptionalModelOrModels[Entity] = None, ) -> List[Entity]: """Get the entities whose attributes match one or several conditions. Args: models: Entity class or classes to obtain. fields: Dictionary with the {key}:{value} to search. Returns: entities: List of Entity object that matches the search criteria. Raises: EntityNotFoundError: If the entities are not found. """ entities: List[Entity] = [] models = self._build_models(models) for model in models: table = self._table_model(model) query = Query.from_(table).select("*") for key, value in fields.items(): if key == "id_": key = "id" if isinstance(value, str): query = query.where( functions.Lower(getattr(table, key)).regexp(value.lower())) else: query = query.where(getattr(table, key) == value) with suppress(OperationalError): entities += self._build_entities(model, query) if len(entities) == 0: raise self._model_not_found( models, f" that match the search filter {fields}") return entities
def test__lower__field(self): q = Q.from_(self.t).select(fn.Lower(self.t.foo)) self.assertEqual("SELECT LOWER(\"foo\") FROM \"abc\"", str(q))
def test__lower__str(self): q = Q.select(fn.Lower('ABC')) self.assertEqual("SELECT LOWER('ABC')", str(q))
def test__lower__field(self): q = Q.from_(self.t).select(fn.Lower(self.t.foo)) self.assertEqual('SELECT LOWER("foo") FROM "abc"', str(q))