]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Added support for Postgresql's traditional SUBSTRING
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 9 Mar 2013 22:41:32 +0000 (17:41 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 9 Mar 2013 22:41:32 +0000 (17:41 -0500)
function syntax, renders as "SUBSTRING(x FROM y FOR z)"
when regular ``func.substring()`` is used.
Courtesy Gunnlaugur Por Briem.
[ticket:2676]

doc/build/changelog/changelog_07.rst
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/test_postgresql.py

index aeb7251db50a27c563b9cc484505c3bba0acf83b..0e7809d183c37345b2917d0b5b0992081018564c 100644 (file)
@@ -6,6 +6,15 @@
 .. changelog::
     :version: 0.7.11
 
+    .. change::
+        :tags: feature, postgresql
+        :tickets: 2676
+
+      Added support for Postgresql's traditional SUBSTRING
+      function syntax, renders as "SUBSTRING(x FROM y FOR z)"
+      when regular ``func.substring()`` is used.
+      Courtesy Gunnlaugur Þór Briem.
+
     .. change::
         :tags: bug, tests
         :tickets: 2669
index 384b7616eadf18f7cc198c9c3b61eea7d8d91fe5..9743b5d1aa904c54533fbcbb3c86f68f97d79096 100644 (file)
@@ -722,6 +722,15 @@ class PGCompiler(compiler.SQLCompiler):
         return "EXTRACT(%s FROM %s)" % (
             field, self.process(expr))
 
+    def visit_substring_func(self, func, **kw):
+        s = self.process(func.clauses.clauses[0], **kw)
+        start = self.process(func.clauses.clauses[1], **kw)
+        if len(func.clauses.clauses) > 2:
+            length = self.process(func.clauses.clauses[2], **kw)
+            return "SUBSTRING(%s FROM %s FOR %s)" % (s, start, length)
+        else:
+            return "SUBSTRING(%s FROM %s)" % (s, start)
+
 class PGDDLCompiler(compiler.DDLCompiler):
     def get_column_specification(self, column, **kwargs):
         colspec = self.preparer.format_column(column)
@@ -734,8 +743,7 @@ class PGDDLCompiler(compiler.DDLCompiler):
                 (
                     isinstance(column.default, schema.Sequence) and
                     column.default.optional
-                )
-            ):
+                )):
             if isinstance(impl_type, sqltypes.BigInteger):
                 colspec += " BIGSERIAL"
             else:
index d62a93111fe9efa6f3654fac456bc36d5cdf1f0b..4cbf99a98baaa56c94aa0115fec842e6e7fd5ac5 100644 (file)
@@ -272,6 +272,14 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
         self.assert_compile(x,
             '''SELECT pg_table.col1, pg_table."variadic" FROM pg_table''')
 
+    def test_substring(self):
+        self.assert_compile(func.substring('abc', 1, 2),
+                            'SUBSTRING(%(substring_1)s FROM %(substring_2)s '
+                            'FOR %(substring_3)s)')
+        self.assert_compile(func.substring('abc', 1),
+                            'SUBSTRING(%(substring_1)s FROM %(substring_2)s)')
+
+
 
 class FloatCoercionTest(fixtures.TablesTest, AssertsExecutionResults):
     __only_on__ = 'postgresql'