MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / icontains

Method icontains

lib/sqlalchemy/sql/operators.py:1518–1597  ·  view source on GitHub ↗

r"""Implement the ``icontains`` operator, e.g. case insensitive version of :meth:`.ColumnOperators.contains`. Produces a LIKE expression that tests against an insensitive match for the middle of a string value: .. sourcecode:: sql lower(column) LIKE '%'

(self, other: Any, **kw: Any)

Source from the content-addressed store, hash-verified

1516 return self.operate(contains_op, other, **kw)
1517
1518 def icontains(self, other: Any, **kw: Any) -> ColumnOperators:
1519 r"""Implement the ``icontains`` operator, e.g. case insensitive
1520 version of :meth:`.ColumnOperators.contains`.
1521
1522 Produces a LIKE expression that tests against an insensitive match
1523 for the middle of a string value:
1524
1525 .. sourcecode:: sql
1526
1527 lower(column) LIKE '%' || lower(<other>) || '%'
1528
1529 E.g.::
1530
1531 stmt = select(sometable).where(sometable.c.column.icontains("foobar"))
1532
1533 Since the operator uses ``LIKE``, wildcard characters
1534 ``"%"`` and ``"_"`` that are present inside the <other> expression
1535 will behave like wildcards as well. For literal string
1536 values, the :paramref:`.ColumnOperators.icontains.autoescape` flag
1537 may be set to ``True`` to apply escaping to occurrences of these
1538 characters within the string value so that they match as themselves
1539 and not as wildcard characters. Alternatively, the
1540 :paramref:`.ColumnOperators.icontains.escape` parameter will establish
1541 a given character as an escape character which can be of use when
1542 the target expression is not a literal string.
1543
1544 :param other: expression to be compared. This is usually a plain
1545 string value, but can also be an arbitrary SQL expression. LIKE
1546 wildcard characters ``%`` and ``_`` are not escaped by default unless
1547 the :paramref:`.ColumnOperators.icontains.autoescape` flag is
1548 set to True.
1549
1550 :param autoescape: boolean; when True, establishes an escape character
1551 within the LIKE expression, then applies it to all occurrences of
1552 ``"%"``, ``"_"`` and the escape character itself within the
1553 comparison value, which is assumed to be a literal string and not a
1554 SQL expression.
1555
1556 An expression such as::
1557
1558 somecolumn.icontains("foo%bar", autoescape=True)
1559
1560 Will render as:
1561
1562 .. sourcecode:: sql
1563
1564 lower(somecolumn) LIKE '%' || lower(:param) || '%' ESCAPE '/'
1565
1566 With the value of ``:param`` as ``"foo/%bar"``.
1567
1568 :param escape: a character which when given will render with the
1569 ``ESCAPE`` keyword to establish that character as the escape
1570 character. This character can then be placed preceding occurrences
1571 of ``%`` and ``_`` to allow them to act as themselves and not
1572 wildcard characters.
1573
1574 An expression such as::
1575

Calls 1

operateMethod · 0.95