From: Derek Harland Date: Mon, 14 Jan 2013 04:02:20 +0000 (+1300) Subject: Add mssql_ordering option for mssql dialect X-Git-Tag: rel_0_8_0~29^2~1^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=38f765007253729f384cb2583a569194f3ee3435;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add mssql_ordering option for mssql dialect --- diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index 74d1a13203..aacc58ba80 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -943,13 +943,20 @@ class MSDDLCompiler(compiler.DDLCompiler): if index.kwargs.get("mssql_clustered"): text += "CLUSTERED " + # extend the custom ordering to the right length + ordering = index.kwargs.get("mssql_ordering", []) + if len(ordering) > len(index.columns): + raise ValueError("Column ordering length is incompatible with index columns") + elif len(ordering) < len(index.columns): + ordering.extend([""]*(len(index.columns) - len(ordering))) + text += "INDEX %s ON %s (%s)" \ % ( self._prepared_index_name(index, include_schema=include_schema), - preparer.format_table(index.table), - ', '.join(preparer.quote(c.name, c.quote) - for c in index.columns)) + preparer.format_table(index.table), + ', '.join([preparer.quote(c.name, c.quote) + (" " + o if o else "") + for c, o in zip(index.columns, ordering)])) return text def visit_drop_index(self, drop): diff --git a/test/dialect/test_mssql.py b/test/dialect/test_mssql.py index 346022c315..4c02bfd156 100644 --- a/test/dialect/test_mssql.py +++ b/test/dialect/test_mssql.py @@ -515,6 +515,15 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): "CREATE CLUSTERED INDEX foo ON test (id)" ) + def test_index_ordering(self): + metadata = MetaData() + tbl = Table('test', metadata, + Column('x', Integer), Column('y', Integer), Column('z', Integer)) + idx = Index("foo", tbl.c.x, "y", mssql_ordering=['DESC']) + self.assert_compile(schema.CreateIndex(idx), + "CREATE INDEX foo ON test (x DESC, y)" + ) + class SchemaAliasingTest(fixtures.TestBase, AssertsCompiledSQL): """SQL server cannot reference schema-qualified tables in a SELECT statement, they must be aliased.