From: Michael Trier Date: Sat, 5 Jul 2008 03:31:30 +0000 (+0000) Subject: Added prefixes option to that accepts a list of string to insert after CREATE in... X-Git-Tag: rel_0_5beta2~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9d7c6901cb92da6cb846bd6c90364cb5d3e934f4;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Added prefixes option to that accepts a list of string to insert after CREATE in the CREATE TABLE statement. Closes #1075. --- diff --git a/CHANGES b/CHANGES index 50a988c070..191a648e21 100644 --- a/CHANGES +++ b/CHANGES @@ -33,6 +33,11 @@ CHANGES - Session.bind gets used as a default even when table/mapper specific binds are defined. +- schema + - Added prefixes option to `Table` that accepts a list of + strings to insert after CREATE in the CREATE TABLE statement. + [ticket:1075] + - sqlite - Modified SQLite's representation of "microseconds" to match the output of str(somedatetime), i.e. in that the diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index e1e5e079ce..fff93df721 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -176,6 +176,10 @@ class Table(SchemaItem, expression.TableClause): been defined elsewhere in the application, else an exception is raised. + prefixes + A list of strings to insert after CREATE in the CREATE TABLE + statement. They will be separated by spaces. + useexisting Defaults to False: indicates that if this Table was already defined elsewhere in the application, disregard the rest of the @@ -224,6 +228,8 @@ class Table(SchemaItem, expression.TableClause): if kwargs.get('info'): self._info = kwargs.pop('info') + self._prefixes = kwargs.pop('prefixes', []) + self.__extra_kwargs(**kwargs) # load column definitions from the database if 'autoload' is defined diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index c8324d7539..51ad1dfb09 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -788,7 +788,7 @@ class SchemaGenerator(DDLBase): if column.default is not None: self.traverse_single(column.default) - self.append("\nCREATE TABLE " + self.preparer.format_table(table) + " (") + self.append("\nCREATE " + " ".join(table._prefixes) + " TABLE " + self.preparer.format_table(table) + " (") separator = "\n" diff --git a/test/engine/metadata.py b/test/engine/metadata.py index ec088d7b2f..db08fdb526 100644 --- a/test/engine/metadata.py +++ b/test/engine/metadata.py @@ -1,6 +1,6 @@ import testenv; testenv.configure_for_tests() import pickle -from sqlalchemy import MetaData +from sqlalchemy import MetaData, create_engine from testlib.sa import Table, Column, Integer, String, UniqueConstraint, \ CheckConstraint, ForeignKey import testlib.sa as tsa @@ -116,5 +116,34 @@ class MetaDataTest(TestBase, ComparesTables): 'fake_table', MetaData(testing.db), autoload=True) + +class TableOptionsTest(TestBase): + def mock_engine(self): + buffer = [] + def executor(sql, *a, **kw): + buffer.append(sql) + engine = create_engine(testing.db.name + '://', + strategy='mock', executor=executor) + assert not hasattr(engine, 'mock') + engine.mock = buffer + return engine + + def setUp(self): + self.engine = self.mock_engine() + self.metadata = MetaData(self.engine) + + def test_prefixes(self): + table1 = Table("temporary_table_1", self.metadata, + Column("col1", Integer), + prefixes = ["TEMPORARY"]) + table1.create() + assert [str(x) for x in self.engine.mock if 'CREATE TEMPORARY TABLE' in str(x)] + del self.engine.mock[:] + table2 = Table("temporary_table_2", self.metadata, + Column("col1", Integer), + prefixes = ["VIRTUAL"]) + table2.create() + assert [str(x) for x in self.engine.mock if 'CREATE VIRTUAL TABLE' in str(x)] + if __name__ == '__main__': testenv.main()