]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add mssql_ordering option for mssql dialect
authorDerek Harland <derek.harland@finq.co.nz>
Mon, 14 Jan 2013 04:02:20 +0000 (17:02 +1300)
committerDerek Harland <derek.harland@finq.co.nz>
Mon, 14 Jan 2013 04:02:20 +0000 (17:02 +1300)
lib/sqlalchemy/dialects/mssql/base.py
test/dialect/test_mssql.py

index 74d1a132039f7a06701840f1c85d3e658be57f91..aacc58ba8010f72a4f0b5101bf78da0a909e1798 100644 (file)
@@ -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):
index 346022c315c08f3d4ec0716a276af122995dd155..4c02bfd15656c53afbf9f7fe8533f9fa1ec1db91 100644 (file)
@@ -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.