]> 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:40:06 +0000 (17:40 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 9 Mar 2013 22:40:06 +0000 (17:40 -0500)
function syntax, renders as "SUBSTRING(x FROM y FOR z)"
when regular ``func.substring()`` is used.
Also in 0.7.11.  Courtesy Gunnlaugur Por Briem.
[ticket:2676]

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

index df919dc3d9b51c62c91417766ff98577f3502810..a42ef3bb6c2585173b7b1e0966316b764cc61606 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 3a93667b6c91aec158f3db3f01ca3767b9389218..1a509089e31eef08bb5b76190a1d741219e6a735 100644 (file)
 
       * :ref:`correlation_context_specific`
 
+    .. 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.
+      Also in 0.7.11.  Courtesy Gunnlaugur Þór Briem.
+
     .. change::
         :tags: feature, orm
         :tickets: 2675
index a7a9e65ce1dbb911fc04b091ef2013f8a975c1f4..c59caff8d25cc5c5707d39225c23ed080b9feba9 100644 (file)
@@ -1030,6 +1030,15 @@ class PGCompiler(compiler.SQLCompiler):
             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)
@@ -1042,8 +1051,7 @@ class PGDDLCompiler(compiler.DDLCompiler):
                 (
                     isinstance(column.default, schema.Sequence) and
                     column.default.optional
-                )
-            ):
+                )):
             if isinstance(impl_type, sqltypes.BigInteger):
                 colspec += " BIGSERIAL"
             else:
index d9fb9830dfc7e1d5e3f9448afac6a7759c12570f..62e85535695262d39ccd10c9a1d1355facee3118 100644 (file)
@@ -180,6 +180,14 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
                             'USING hash (data)',
                             dialect=postgresql.dialect())
 
+    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)')
+
+
 
     def test_extract(self):
         t = table('t', column('col1', DateTime), column('col2', Date),