|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from textwrap import dedent |
5 | 6 | import typing as t |
6 | 7 | import logging |
7 | 8 |
|
@@ -53,8 +54,8 @@ class MSSQLEngineAdapter( |
53 | 54 | SUPPORTS_TUPLE_IN = False |
54 | 55 | SUPPORTS_MATERIALIZED_VIEWS = False |
55 | 56 | CURRENT_CATALOG_EXPRESSION = exp.func("db_name") |
56 | | - COMMENT_CREATION_TABLE = CommentCreationTable.UNSUPPORTED |
57 | | - COMMENT_CREATION_VIEW = CommentCreationView.UNSUPPORTED |
| 57 | + COMMENT_CREATION_TABLE = CommentCreationTable.COMMENT_COMMAND_ONLY |
| 58 | + COMMENT_CREATION_VIEW = CommentCreationView.COMMENT_COMMAND_ONLY |
58 | 59 | SUPPORTS_REPLACE_TABLE = False |
59 | 60 | MAX_IDENTIFIER_LENGTH = 128 |
60 | 61 | SUPPORTS_QUERY_EXECUTION_TRACKING = True |
@@ -457,3 +458,64 @@ def delete_from(self, table_name: TableName, where: t.Union[str, exp.Expr]) -> N |
457 | 458 | ) |
458 | 459 |
|
459 | 460 | return super().delete_from(table_name, where) |
| 461 | + |
| 462 | + def _build_create_comment_column_exp( |
| 463 | + self, table: exp.Table, column_name: str, column_comment: str, table_kind: str = "TABLE" |
| 464 | + ) -> exp.Comment | str: |
| 465 | + tsql_text = dedent(f""" |
| 466 | + SET NOCOUNT ON; |
| 467 | +
|
| 468 | + DECLARE @comment sql_variant = {exp.Literal.string(column_comment).sql(dialect=self.dialect) if column_comment is not None else "NULL"}; |
| 469 | + DECLARE @property_name VARCHAR(128) = 'MS_Description'; |
| 470 | + DECLARE @schema_name VARCHAR(128) = '{table.db if table.db else "dbo"}'; |
| 471 | + DECLARE @object_name VARCHAR(128) = '{table.name}'; |
| 472 | + DECLARE @object_kind VARCHAR(128) = '{table_kind}'; |
| 473 | + DECLARE @column_name VARCHAR(128) = '{column_name}'; |
| 474 | + DECLARE @existing sql_variant; |
| 475 | +
|
| 476 | + SELECT TOP 1 @existing = CAST(VALUE AS NVARCHAR) FROM fn_listextendedproperty(@property_name, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name); |
| 477 | +
|
| 478 | + IF @comment IS NULL |
| 479 | + BEGIN |
| 480 | + IF @existing IS NOT NULL |
| 481 | + EXEC sp_dropextendedproperty @property_name, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name; |
| 482 | + END |
| 483 | + ELSE |
| 484 | + BEGIN |
| 485 | + IF @existing IS NULL |
| 486 | + EXEC sp_addextendedproperty @property_name,@comment, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name; |
| 487 | + ELSE IF @existing != @comment |
| 488 | + EXEC sp_updateextendedproperty @property_name, @comment, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name; |
| 489 | + END |
| 490 | + """) |
| 491 | + return tsql_text |
| 492 | + |
| 493 | + def _build_create_comment_table_exp( |
| 494 | + self, table: exp.Table, table_comment: str, table_kind: str |
| 495 | + ) -> exp.Comment | str: |
| 496 | + tsql_text = dedent(f""" |
| 497 | + SET NOCOUNT ON; |
| 498 | +
|
| 499 | + DECLARE @comment sql_variant = {exp.Literal.string(table_comment).sql(dialect=self.dialect) if table_comment is not None else "NULL"}; |
| 500 | + DECLARE @property_name VARCHAR(128) = 'MS_Description'; |
| 501 | + DECLARE @schema_name VARCHAR(128) = '{table.db if table.db else "dbo"}'; |
| 502 | + DECLARE @object_name VARCHAR(128) = '{table.name}'; |
| 503 | + DECLARE @object_kind VARCHAR(128) = '{table_kind}'; |
| 504 | + DECLARE @existing sql_variant; |
| 505 | +
|
| 506 | + SELECT TOP 1 @existing = CAST(VALUE AS NVARCHAR) FROM fn_listextendedproperty(@property_name, 'schema', @schema_name, @object_kind, @object_name, DEFAULT, DEFAULT); |
| 507 | +
|
| 508 | + IF @comment IS NULL |
| 509 | + BEGIN |
| 510 | + IF @existing IS NOT NULL |
| 511 | + EXEC sp_dropextendedproperty @property_name, 'schema', @schema_name, @object_kind, @object_name; |
| 512 | + END |
| 513 | + ELSE |
| 514 | + BEGIN |
| 515 | + IF @existing IS NULL |
| 516 | + EXEC sp_addextendedproperty @property_name,@comment, 'schema', @schema_name, @object_kind, @object_name; |
| 517 | + ELSE IF @existing != @comment |
| 518 | + EXEC sp_updateextendedproperty @property_name, @comment, 'schema', @schema_name, @object_kind, @object_name; |
| 519 | + END |
| 520 | + """) |
| 521 | + return tsql_text |
0 commit comments