diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-03-09 17:40:06 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-03-09 17:40:06 -0500 |
| commit | a0de45185bf510fca9e237d9191e89391d118591 (patch) | |
| tree | 889eb10d656581848ae3bc978b44d90634348808 | |
| parent | c8a80e21301791fd4e1caf29ed8cadd40f617765 (diff) | |
| download | sqlalchemy-a0de45185bf510fca9e237d9191e89391d118591.tar.gz | |
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 Por Briem.
[ticket:2676]
| -rw-r--r-- | doc/build/changelog/changelog_07.rst | 9 | ||||
| -rw-r--r-- | doc/build/changelog/changelog_08.rst | 9 | ||||
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 12 | ||||
| -rw-r--r-- | test/dialect/test_postgresql.py | 8 |
4 files changed, 36 insertions, 2 deletions
diff --git a/doc/build/changelog/changelog_07.rst b/doc/build/changelog/changelog_07.rst index df919dc3d..a42ef3bb6 100644 --- a/doc/build/changelog/changelog_07.rst +++ b/doc/build/changelog/changelog_07.rst @@ -7,6 +7,15 @@ :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 :pullreq: 41 diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 3a93667b6..1a509089e 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -19,6 +19,15 @@ * :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 diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index a7a9e65ce..c59caff8d 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -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: diff --git a/test/dialect/test_postgresql.py b/test/dialect/test_postgresql.py index d9fb9830d..62e855356 100644 --- a/test/dialect/test_postgresql.py +++ b/test/dialect/test_postgresql.py @@ -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), |
