From 7198abb13e464b32639d03dc5987457ba64281c4 Mon Sep 17 00:00:00 2001 From: Derek Harland Date: Mon, 14 Jan 2013 17:00:37 +1300 Subject: Add mssql_clustered option for mssql dialect --- lib/sqlalchemy/dialects/mssql/base.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index d7c29654a..74d1a1320 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -932,6 +932,26 @@ class MSDDLCompiler(compiler.DDLCompiler): return colspec + def visit_create_index(self, create, include_schema=False): + index = create.element + preparer = self.preparer + text = "CREATE " + if index.unique: + text += "UNIQUE " + + # handle clustering option + if index.kwargs.get("mssql_clustered"): + text += "CLUSTERED " + + 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)) + return text + def visit_drop_index(self, drop): return "\nDROP INDEX %s.%s" % ( self.preparer.quote_identifier(drop.element.table.name), -- cgit v1.2.1 From 38f765007253729f384cb2583a569194f3ee3435 Mon Sep 17 00:00:00 2001 From: Derek Harland Date: Mon, 14 Jan 2013 17:02:20 +1300 Subject: Add mssql_ordering option for mssql dialect --- lib/sqlalchemy/dialects/mssql/base.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index 74d1a1320..aacc58ba8 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): -- cgit v1.2.1 From f5a5a6bf43faf53569f21cf05d4a28a4ba1f5229 Mon Sep 17 00:00:00 2001 From: Derek Harland Date: Mon, 14 Jan 2013 17:03:38 +1300 Subject: Add mssql_include option for mssql dialect --- lib/sqlalchemy/dialects/mssql/base.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index aacc58ba8..29f32f3f1 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -957,6 +957,16 @@ class MSDDLCompiler(compiler.DDLCompiler): preparer.format_table(index.table), ', '.join([preparer.quote(c.name, c.quote) + (" " + o if o else "") for c, o in zip(index.columns, ordering)])) + + # handle other included columns + if index.kwargs.get("mssql_include"): + inclusions = [index.table.c[col] if isinstance(col, basestring) else col + for col in index.kwargs["mssql_include"]] + + text += " INCLUDE (%s)" \ + % ', '.join([preparer.quote(c.name, c.quote) + for c in inclusions]) + return text def visit_drop_index(self, drop): -- cgit v1.2.1