From 85a88df13ab8d217331cf98392544a888b4d7df3 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 17 Jul 2022 11:32:27 -0400 Subject: use concat() directly for contains, startswith, endswith Adjusted the SQL compilation for string containment functions ``.contains()``, ``.startswith()``, ``.endswith()`` to force the use of the string concatenation operator, rather than relying upon the overload of the addition operator, so that non-standard use of these operators with for example bytestrings still produces string concatenation operators. To accommodate this, needed to add a new _rconcat operator function, which is private, as well as a fallback in concat_op() that works similarly to Python builtin ops. Fixes: #8253 Change-Id: I2b7f56492f765742d88cb2a7834ded6a2892bd7e --- lib/sqlalchemy/sql/compiler.py | 12 ++++++------ lib/sqlalchemy/sql/operators.py | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index a11d83b11..87d031cc2 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -2691,37 +2691,37 @@ class SQLCompiler(Compiled): def visit_contains_op_binary(self, binary, operator, **kw): binary = binary._clone() percent = self._like_percent_literal - binary.right = percent.__add__(binary.right).__add__(percent) + binary.right = percent.concat(binary.right).concat(percent) return self.visit_like_op_binary(binary, operator, **kw) def visit_not_contains_op_binary(self, binary, operator, **kw): binary = binary._clone() percent = self._like_percent_literal - binary.right = percent.__add__(binary.right).__add__(percent) + binary.right = percent.concat(binary.right).concat(percent) return self.visit_not_like_op_binary(binary, operator, **kw) def visit_startswith_op_binary(self, binary, operator, **kw): binary = binary._clone() percent = self._like_percent_literal - binary.right = percent.__radd__(binary.right) + binary.right = percent._rconcat(binary.right) return self.visit_like_op_binary(binary, operator, **kw) def visit_not_startswith_op_binary(self, binary, operator, **kw): binary = binary._clone() percent = self._like_percent_literal - binary.right = percent.__radd__(binary.right) + binary.right = percent._rconcat(binary.right) return self.visit_not_like_op_binary(binary, operator, **kw) def visit_endswith_op_binary(self, binary, operator, **kw): binary = binary._clone() percent = self._like_percent_literal - binary.right = percent.__add__(binary.right) + binary.right = percent.concat(binary.right) return self.visit_like_op_binary(binary, operator, **kw) def visit_not_endswith_op_binary(self, binary, operator, **kw): binary = binary._clone() percent = self._like_percent_literal - binary.right = percent.__add__(binary.right) + binary.right = percent.concat(binary.right) return self.visit_not_like_op_binary(binary, operator, **kw) def visit_like_op_binary(self, binary, operator, **kw): diff --git a/lib/sqlalchemy/sql/operators.py b/lib/sqlalchemy/sql/operators.py index 2b888769a..44d63b398 100644 --- a/lib/sqlalchemy/sql/operators.py +++ b/lib/sqlalchemy/sql/operators.py @@ -615,6 +615,16 @@ class ColumnOperators(Operators): """ return self.operate(concat_op, other) + def _rconcat(self, other: Any) -> ColumnOperators: + """Implement an 'rconcat' operator. + + this is for internal use at the moment + + .. versionadded:: 1.4.40 + + """ + return self.reverse_operate(concat_op, other) + def like( self, other: Any, escape: Optional[str] = None ) -> ColumnOperators: @@ -1764,7 +1774,12 @@ def filter_op(a: Any, b: Any) -> Any: @_operator_fn def concat_op(a: Any, b: Any) -> Any: - return a.concat(b) + try: + concat = a.concat + except AttributeError: + return b._rconcat(a) + else: + return concat(b) @_operator_fn -- cgit v1.2.1