From: Mark Sandan Date: Tue, 24 May 2016 00:08:36 +0000 (-0700) Subject: Add DDLCompiler.create_table_suffix() X-Git-Tag: rel_1_1_0b1~1^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d7b8b475f8a5c0ddf955157f89db3d44d0dc0d9a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add DDLCompiler.create_table_suffix() Allows custom dialects to add keywords after the CREATE TABLE section. Change-Id: I6fa66dfcf00ef95122f491a9115410df2746cf88 --- diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 534c3993c7..2b71c799d2 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -264,6 +264,15 @@ 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 diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 5e537dfdc3..fc50735969 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -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 '' diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index ca3468710f..8b4e5053b5 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -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)