From: Mike Bayer Date: Sat, 9 Mar 2013 22:41:32 +0000 (-0500) Subject: Added support for Postgresql's traditional SUBSTRING X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c74547164d290193fd4798601c2b7b770969a9d0;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git 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 Por Briem. [ticket:2676] --- diff --git a/doc/build/changelog/changelog_07.rst b/doc/build/changelog/changelog_07.rst index aeb7251db5..0e7809d183 100644 --- a/doc/build/changelog/changelog_07.rst +++ b/doc/build/changelog/changelog_07.rst @@ -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 diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 384b7616ea..9743b5d1aa 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -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: diff --git a/test/dialect/test_postgresql.py b/test/dialect/test_postgresql.py index d62a93111f..4cbf99a98b 100644 --- a/test/dialect/test_postgresql.py +++ b/test/dialect/test_postgresql.py @@ -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'