]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Render table hints in generic SQL
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 27 May 2020 14:18:33 +0000 (10:18 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 27 May 2020 14:22:33 +0000 (10:22 -0400)
Added :meth:`.Select.with_hint` output to the generic SQL string that is
produced when calling ``str()`` on a statement.  Previously, this clause
would be omitted under the assumption that it was dialect specific.
The hint text is presented within brackets to indicate the rendering
of such hints varies among backends.

Fixes: #5353
References: #4667
Change-Id: I01d97d6baa993e495519036ec7ecd5ae62856c16
(cherry picked from commit 7dc411dc63faf59b4e28fa0dea805887821d0d99)

doc/build/changelog/unreleased_13/5353.rst [new file with mode: 0644]
lib/sqlalchemy/sql/compiler.py
test/sql/test_compiler.py

diff --git a/doc/build/changelog/unreleased_13/5353.rst b/doc/build/changelog/unreleased_13/5353.rst
new file mode 100644 (file)
index 0000000..39a5ba4
--- /dev/null
@@ -0,0 +1,10 @@
+.. change::
+    :tags: bug, sql
+    :tickets: 5353
+
+    Added :meth:`.Select.with_hint` output to the generic SQL string that is
+    produced when calling ``str()`` on a statement.  Previously, this clause
+    would be omitted under the assumption that it was dialect specific.
+    The hint text is presented within brackets to indicate the rendering
+    of such hints varies among backends.
+
index 47f12f049303174847fcf7402744b611cb428234..473e8861714103be097dd01d473ac3fe5b198a9a 100644 (file)
@@ -2828,6 +2828,9 @@ class StrSQLCompiler(SQLCompiler):
             for t in extra_froms
         )
 
+    def get_from_hint_text(self, table, text):
+        return "[%s]" % text
+
 
 class DDLCompiler(Compiled):
     @util.memoized_property
index 00f7a976369fc91810f964f9d5faa72d66cc96ca..f79bbed5f42203f389547da0dd7e8263ba985224 100644 (file)
@@ -3393,6 +3393,41 @@ class StringifySpecialTest(fixtures.TestBase):
             "'%s'" % value,
         )
 
+    def test_with_hint_table(self):
+        stmt = (
+            select([table1])
+            .select_from(
+                table1.join(table2, table1.c.myid == table2.c.otherid)
+            )
+            .with_hint(table1, "use some_hint")
+        )
+
+        # note that some dialects instead use the "with_select_hint"
+        # hook to put the 'hint' up front
+        eq_ignore_whitespace(
+            str(stmt),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable [use some_hint] "
+            "JOIN myothertable ON mytable.myid = myothertable.otherid",
+        )
+
+    def test_with_hint_statement(self):
+        stmt = (
+            select([table1])
+            .select_from(
+                table1.join(table2, table1.c.myid == table2.c.otherid)
+            )
+            .with_statement_hint("use some_hint")
+        )
+
+        eq_ignore_whitespace(
+            str(stmt),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable "
+            "JOIN myothertable ON mytable.myid = myothertable.otherid "
+            "use some_hint",
+        )
+
 
 class KwargPropagationTest(fixtures.TestBase):
     @classmethod