diff options
author | Federico Caselli <cfederico87@gmail.com> | 2020-07-16 21:32:52 +0200 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-08-27 17:30:18 -0400 |
commit | b1b97ed1fcac777c4f42fdf84e05f8d59f63b679 (patch) | |
tree | 93b67e4ae3eff79d3e49bca71cddac40cf40b9bd /lib/sqlalchemy/sql/default_comparator.py | |
parent | fe772672b4fc00df0b66aca92e2092779a844a2d (diff) | |
download | sqlalchemy-b1b97ed1fcac777c4f42fdf84e05f8d59f63b679.tar.gz |
Add support for regular expression on supported backend.
Two operations have been defined:
* :meth:`~.ColumnOperators.regexp_match` implementing a regular
expression match like function.
* :meth:`~.ColumnOperators.regexp_replace` implementing a regular
expression string replace function.
Fixes: #1390
Change-Id: I44556846e4668ccf329023613bd26861d5c674e6
Diffstat (limited to 'lib/sqlalchemy/sql/default_comparator.py')
-rw-r--r-- | lib/sqlalchemy/sql/default_comparator.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/sqlalchemy/sql/default_comparator.py b/lib/sqlalchemy/sql/default_comparator.py index 6f1a25670..eec174e8b 100644 --- a/lib/sqlalchemy/sql/default_comparator.py +++ b/lib/sqlalchemy/sql/default_comparator.py @@ -252,6 +252,45 @@ def _collate_impl(expr, op, other, **kw): return collate(expr, other) +def _regexp_match_impl(expr, op, pattern, flags, **kw): + if flags is not None: + flags = coercions.expect( + roles.BinaryElementRole, + flags, + expr=expr, + operator=operators.regexp_replace_op, + ) + return _boolean_compare( + expr, + op, + pattern, + flags=flags, + negate=operators.not_regexp_match_op + if op is operators.regexp_match_op + else operators.regexp_match_op, + **kw + ) + + +def _regexp_replace_impl(expr, op, pattern, replacement, flags, **kw): + replacement = coercions.expect( + roles.BinaryElementRole, + replacement, + expr=expr, + operator=operators.regexp_replace_op, + ) + if flags is not None: + flags = coercions.expect( + roles.BinaryElementRole, + flags, + expr=expr, + operator=operators.regexp_replace_op, + ) + return _binary_operate( + expr, op, pattern, replacement=replacement, flags=flags, **kw + ) + + # a mapping of operators with the method they use, along with # their negated operator for comparison operators operator_lookup = { @@ -304,4 +343,7 @@ operator_lookup = { "lshift": (_unsupported_impl,), "rshift": (_unsupported_impl,), "contains": (_unsupported_impl,), + "regexp_match_op": (_regexp_match_impl,), + "not_regexp_match_op": (_regexp_match_impl,), + "regexp_replace_op": (_regexp_replace_impl,), } |