From 8318a98a60dabf0919f4998e4df32db804ffd3bb Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 10 Nov 2018 21:10:51 -0500 Subject: [PATCH] Add Sequence to StrSQLCompiler Added :class:`.Sequence` to the "string SQL" system that will render a meaningful string expression (``""``) when stringifying without a dialect a statement that includes a "sequence nextvalue" expression, rather than raising a compilation error. Fixes: #4144 Change-Id: Ia910f0e22008a7cde7597365954ede324101cf4d --- doc/build/changelog/unreleased_13/4144.rst | 10 ++++++++++ lib/sqlalchemy/sql/compiler.py | 3 +++ test/sql/test_compiler.py | 12 ++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 doc/build/changelog/unreleased_13/4144.rst diff --git a/doc/build/changelog/unreleased_13/4144.rst b/doc/build/changelog/unreleased_13/4144.rst new file mode 100644 index 0000000000..08fcb61028 --- /dev/null +++ b/doc/build/changelog/unreleased_13/4144.rst @@ -0,0 +1,10 @@ +.. change:: + :tags: feature, sql + :tickets: 4144 + + Added :class:`.Sequence` to the "string SQL" system that will render a + meaningful string expression (``""``) + when stringifying without a dialect a statement that includes a "sequence + nextvalue" expression, rather than raising a compilation error. + + diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 27ee4afc68..87a9f859bb 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -2407,6 +2407,9 @@ class StrSQLCompiler(SQLCompiler): def visit_json_path_getitem_op_binary(self, binary, operator, **kw): return self.visit_getitem_binary(binary, operator, **kw) + def visit_sequence(self, seq, **kw): + return "" % self.preparer.format_sequence(seq) + def returning_clause(self, stmt, returning_cols): columns = [ self._label_select_column(None, c, True, False, {}) diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index 993008c072..22b46cc46b 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -2898,6 +2898,18 @@ class StringifySpecialTest(fixtures.TestBase): "SELECT anon_1.myid FROM anon_1" ) + def test_next_sequence_value(self): + # using descriptive text that is intentionally not compatible + # with any particular backend, since all backends have different + # syntax + + seq = Sequence("my_sequence") + + eq_ignore_whitespace( + str(seq.next_value()), + "" + ) + def test_returning(self): stmt = table1.insert().returning(table1.c.myid) -- 2.47.2