Beispiel #1
0
    def where(self, *where: Combinable, **kwargs):
        for i in where:
            self.where_delegate.where(i)

        for key, value in kwargs.items():
            column = self._meta.get_column_by_name(key)
            self.where_delegate.where(Where(column=column, value=value, operator=Equal))

        return self
Beispiel #2
0
 def ilike(self, value: str) -> Where:
     if "%" not in value:
         raise ValueError("% is required for ilike operators")
     if self._meta.engine_type == "postgres":
         operator: t.Type[ComparisonOperator] = ILike
     else:
         colored_warning(
             "SQLite doesn't support ILIKE currently, falling back to LIKE."
         )
         operator = Like
     return Where(column=self, value=value, operator=operator)
Beispiel #3
0
    def ilike(self, value: str) -> Where:
        """
        Only Postgres supports ILIKE. It's used for case insensitive matching.

        For SQLite, it's just proxied to a LIKE query instead.

        """
        if self._meta.engine_type == "postgres":
            operator: t.Type[ComparisonOperator] = ILike
        else:
            colored_warning(
                "SQLite doesn't support ILIKE, falling back to LIKE.")
            operator = Like
        return Where(column=self, value=value, operator=operator)
Beispiel #4
0
    def like(self, value: str) -> Where:
        """
        Both SQLite and Postgres support LIKE, but they mean different things.

        In Postgres, LIKE is case sensitive (i.e. 'foo' equals 'foo', but
        'foo' doesn't equal 'Foo').

        In SQLite, LIKE is case insensitive for ASCII characters
        (i.e. 'foo' equals 'Foo'). But not for non-ASCII characters. To learn
        more, see the docs:

        https://sqlite.org/lang_expr.html#the_like_glob_regexp_and_match_operators

        """
        return Where(column=self, value=value, operator=Like)
Beispiel #5
0
    def all(self, value: t.Any) -> Where:
        """
        Check if all of the items in the array match the given value.

        .. code-block:: python

            >>> Ticket.select().where(Ticket.seat_numbers.all(510)).run_sync()

        """
        engine_type = self._meta.table._meta.db.engine_type

        if engine_type == "postgres":
            return Where(column=self, value=value, operator=ArrayAll)
        elif engine_type == "sqlite":
            raise ValueError("Unsupported by SQLite")
        else:
            raise ValueError("Unrecognised engine type")
Beispiel #6
0
 def __ne__(self, value) -> Where:  # type: ignore
     return Where(column=self, value=value, operator=NotEqual)
Beispiel #7
0
 def __eq__(self, value) -> Where:  # type: ignore
     if value is None:
         return Where(column=self, operator=IsNull)
     else:
         return Where(column=self, value=value, operator=Equal)
Beispiel #8
0
 def __ge__(self, value) -> Where:
     return Where(column=self, value=value, operator=GreaterEqualThan)
Beispiel #9
0
 def __le__(self, value) -> Where:
     return Where(column=self, value=value, operator=LessEqualThan)
Beispiel #10
0
 def not_like(self, value: str) -> Where:
     if "%" not in value:
         raise ValueError("% is required for like operators")
     return Where(column=self, value=value, operator=NotLike)
Beispiel #11
0
 def not_in(self, values: t.List[t.Any]) -> Where:
     if len(values) == 0:
         raise ValueError(
             "The `values` list argument must contain at least one value.")
     return Where(column=self, values=values, operator=NotIn)
Beispiel #12
0
 def is_not_null(self) -> Where:
     """
     Can be used instead of `MyTable.column == None`, because some linters
     don't like a comparison to None.
     """
     return Where(column=self, operator=IsNotNull)
Beispiel #13
0
 def __ne__(self, value) -> Where:  # type: ignore[override]
     if value is None:
         return Where(column=self, operator=IsNotNull)
     else:
         return Where(column=self, value=value, operator=NotEqual)
Beispiel #14
0
 def not_like(self, value: str) -> Where:
     return Where(column=self, value=value, operator=NotLike)