r"""Implement the ``startswith`` operator. Produces a LIKE expression that tests against a match for the start of a string value: .. sourcecode:: sql column LIKE || '%' E.g.:: stmt = select(sometable).where(sometable.c.column.start
(
self,
other: Any,
escape: Optional[str] = None,
autoescape: bool = False,
)
| 1075 | isnot = is_not |
| 1076 | |
| 1077 | def startswith( |
| 1078 | self, |
| 1079 | other: Any, |
| 1080 | escape: Optional[str] = None, |
| 1081 | autoescape: bool = False, |
| 1082 | ) -> ColumnOperators: |
| 1083 | r"""Implement the ``startswith`` operator. |
| 1084 | |
| 1085 | Produces a LIKE expression that tests against a match for the start |
| 1086 | of a string value: |
| 1087 | |
| 1088 | .. sourcecode:: sql |
| 1089 | |
| 1090 | column LIKE <other> || '%' |
| 1091 | |
| 1092 | E.g.:: |
| 1093 | |
| 1094 | stmt = select(sometable).where(sometable.c.column.startswith("foobar")) |
| 1095 | |
| 1096 | Since the operator uses ``LIKE``, wildcard characters |
| 1097 | ``"%"`` and ``"_"`` that are present inside the <other> expression |
| 1098 | will behave like wildcards as well. For literal string |
| 1099 | values, the :paramref:`.ColumnOperators.startswith.autoescape` flag |
| 1100 | may be set to ``True`` to apply escaping to occurrences of these |
| 1101 | characters within the string value so that they match as themselves |
| 1102 | and not as wildcard characters. Alternatively, the |
| 1103 | :paramref:`.ColumnOperators.startswith.escape` parameter will establish |
| 1104 | a given character as an escape character which can be of use when |
| 1105 | the target expression is not a literal string. |
| 1106 | |
| 1107 | :param other: expression to be compared. This is usually a plain |
| 1108 | string value, but can also be an arbitrary SQL expression. LIKE |
| 1109 | wildcard characters ``%`` and ``_`` are not escaped by default unless |
| 1110 | the :paramref:`.ColumnOperators.startswith.autoescape` flag is |
| 1111 | set to True. |
| 1112 | |
| 1113 | :param autoescape: boolean; when True, establishes an escape character |
| 1114 | within the LIKE expression, then applies it to all occurrences of |
| 1115 | ``"%"``, ``"_"`` and the escape character itself within the |
| 1116 | comparison value, which is assumed to be a literal string and not a |
| 1117 | SQL expression. |
| 1118 | |
| 1119 | An expression such as:: |
| 1120 | |
| 1121 | somecolumn.startswith("foo%bar", autoescape=True) |
| 1122 | |
| 1123 | Will render as: |
| 1124 | |
| 1125 | .. sourcecode:: sql |
| 1126 | |
| 1127 | somecolumn LIKE :param || '%' ESCAPE '/' |
| 1128 | |
| 1129 | With the value of ``:param`` as ``"foo/%bar"``. |
| 1130 | |
| 1131 | :param escape: a character which when given will render with the |
| 1132 | ``ESCAPE`` keyword to establish that character as the escape |
| 1133 | character. This character can then be placed preceding occurrences |
| 1134 | of ``%`` and ``_`` to allow them to act as themselves and not |