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/compiler.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/compiler.py')
-rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 7b917e661..ec1a57935 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -2109,6 +2109,24 @@ class SQLCompiler(Compiled): **kw ) + def visit_regexp_match_op_binary(self, binary, operator, **kw): + raise exc.CompileError( + "%s dialect does not support regular expressions" + % self.dialect.name + ) + + def visit_not_regexp_match_op_binary(self, binary, operator, **kw): + raise exc.CompileError( + "%s dialect does not support regular expressions" + % self.dialect.name + ) + + def visit_regexp_replace_op_binary(self, binary, operator, **kw): + raise exc.CompileError( + "%s dialect does not support regular expression replacements" + % self.dialect.name + ) + def visit_bindparam( self, bindparam, @@ -3671,6 +3689,20 @@ class StrSQLCompiler(SQLCompiler): def get_from_hint_text(self, table, text): return "[%s]" % text + def visit_regexp_match_op_binary(self, binary, operator, **kw): + return self._generate_generic_binary(binary, " <regexp> ", **kw) + + def visit_not_regexp_match_op_binary(self, binary, operator, **kw): + return self._generate_generic_binary(binary, " <not regexp> ", **kw) + + def visit_regexp_replace_op_binary(self, binary, operator, **kw): + replacement = binary.modifiers["replacement"] + return "<regexp replace>(%s, %s, %s)" % ( + binary.left._compiler_dispatch(self, **kw), + binary.right._compiler_dispatch(self, **kw), + replacement._compiler_dispatch(self, **kw), + ) + class DDLCompiler(Compiled): @util.memoized_property |