]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add DDLCompiler.create_table_suffix()
authorMark Sandan <msandan@utexas.edu>
Tue, 24 May 2016 00:08:36 +0000 (17:08 -0700)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 16 Jun 2016 15:43:02 +0000 (11:43 -0400)
Allows custom dialects to add keywords after the
CREATE TABLE section.

Change-Id: I6fa66dfcf00ef95122f491a9115410df2746cf88

doc/build/changelog/changelog_11.rst
lib/sqlalchemy/sql/compiler.py
test/sql/test_compiler.py

index 534c3993c7cccf1b2208d59696b449009ce6b176..2b71c799d2cfe9c99190499bfe8e36b47b684ff0 100644 (file)
         for database load on the next object-wide unexpire, when the object
         were merged into the session with ``session.merge(obj, load=False)``.
 
+    .. change::
+        :tags: feature, sql
+        :pullreq: github:275
+
+        Added a hook in :meth:`.DDLCompiler.visit_create_table` called
+        :meth:`.DDLCompiler.create_table_suffix`, allowing custom dialects
+        to add keywords after the "CREATE TABLE" clause.  Pull request
+        courtesy Mark Sandan.
+
     .. change::
         :tags: feature, sql
         :pullreq: github:231
index 5e537dfdc3daec0faa3ec6c615d65081a8553019..fc50735969de7ac9675ede2bab8b904c39a30ae6 100644 (file)
@@ -2260,7 +2260,13 @@ class DDLCompiler(Compiled):
         text = "\nCREATE "
         if table._prefixes:
             text += " ".join(table._prefixes) + " "
-        text += "TABLE " + preparer.format_table(table) + " ("
+        text += "TABLE " + preparer.format_table(table) + " "
+
+        create_table_suffix = self.create_table_suffix(table)
+        if create_table_suffix:
+            text += create_table_suffix + " "
+
+        text += "("
 
         separator = "\n"
 
@@ -2465,6 +2471,9 @@ class DDLCompiler(Compiled):
             colspec += " NOT NULL"
         return colspec
 
+    def create_table_suffix(self, table):
+        return ''
+
     def post_create_table(self, table):
         return ''
 
index ca3468710f75538d5e72daf7953d4149a85133df..8b4e5053b5994fa4c3375a1a3d49dfec46db2337 100644 (file)
@@ -3081,6 +3081,22 @@ class DDLTest(fixtures.TestBase, AssertsCompiledSQL):
             "PRIMARY KEY (b, a))"
         )
 
+    def test_create_table_suffix(self):
+        class MyDialect(default.DefaultDialect):
+            class MyCompiler(compiler.DDLCompiler):
+                def create_table_suffix(self, table):
+                    return 'SOME SUFFIX'
+
+            ddl_compiler = MyCompiler
+
+        m = MetaData()
+        t1 = Table('t1', m, Column('q', Integer))
+        self.assert_compile(
+            schema.CreateTable(t1),
+            "CREATE TABLE t1 SOME SUFFIX (q INTEGER)",
+            dialect=MyDialect()
+        )
+
     def test_table_no_cols(self):
         m = MetaData()
         t1 = Table('t1', m)