|
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,83 @@ 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_table_exp( |
| 463 | + self, table: exp.Table, table_comment: str, table_kind: str = "TABLE" |
| 464 | + ) -> exp.Comment | str: |
| 465 | + template = dedent(""" |
| 466 | + DECLARE @comment sql_variant = {comment}; |
| 467 | + DECLARE @property_name VARCHAR(128) = 'MS_Description'; |
| 468 | + DECLARE @schema_name VARCHAR(128) = {schema_name}; |
| 469 | + DECLARE @object_name VARCHAR(128) = {object_name}; |
| 470 | + DECLARE @object_kind VARCHAR(128) = '{object_kind}'; |
| 471 | + DECLARE @existing sql_variant; |
| 472 | +
|
| 473 | + SELECT TOP 1 @existing = CAST(VALUE AS NVARCHAR) FROM fn_listextendedproperty(@property_name, 'schema', @schema_name, @object_kind, @object_name, DEFAULT, DEFAULT); |
| 474 | +
|
| 475 | + IF @comment IS NULL |
| 476 | + BEGIN |
| 477 | + IF @existing IS NOT NULL |
| 478 | + EXEC sp_dropextendedproperty @property_name, 'schema', @schema_name, @object_kind, @object_name; |
| 479 | + END |
| 480 | + ELSE |
| 481 | + BEGIN |
| 482 | + IF @existing IS NULL |
| 483 | + EXEC sp_addextendedproperty @property_name,@comment, 'schema', @schema_name, @object_kind, @object_name; |
| 484 | + ELSE IF @existing != @comment |
| 485 | + EXEC sp_updateextendedproperty @property_name, @comment, 'schema', @schema_name, @object_kind, @object_name; |
| 486 | + END |
| 487 | + """) |
| 488 | + tsql_text = template.format( |
| 489 | + comment=exp.Literal.string(table_comment or "NULL").sql( |
| 490 | + dialect=self.dialect, identify=False |
| 491 | + ), |
| 492 | + schema_name=exp.Literal.string(table.db or "dbo").sql( |
| 493 | + dialect=self.dialect, identify=False |
| 494 | + ), |
| 495 | + object_name=exp.Literal.string(table.name).sql(dialect=self.dialect, identify=False), |
| 496 | + object_kind=table_kind, |
| 497 | + ) |
| 498 | + return tsql_text |
| 499 | + |
| 500 | + def _build_create_comment_column_exp( |
| 501 | + self, table: exp.Table, column_name: str, column_comment: str, table_kind: str = "TABLE" |
| 502 | + ) -> exp.Comment | str: |
| 503 | + template = dedent(""" |
| 504 | + DECLARE @comment sql_variant = {comment}; |
| 505 | + DECLARE @property_name VARCHAR(128) = 'MS_Description'; |
| 506 | + DECLARE @schema_name VARCHAR(128) = {schema_name}; |
| 507 | + DECLARE @object_name VARCHAR(128) = {object_name}; |
| 508 | + DECLARE @object_kind VARCHAR(128) = '{object_kind}'; |
| 509 | + DECLARE @column_name VARCHAR(128) = {column_name}; |
| 510 | + DECLARE @existing sql_variant; |
| 511 | +
|
| 512 | + SELECT TOP 1 @existing = CAST(VALUE AS NVARCHAR) FROM fn_listextendedproperty(@property_name, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name); |
| 513 | +
|
| 514 | + IF @comment IS NULL |
| 515 | + BEGIN |
| 516 | + IF @existing IS NOT NULL |
| 517 | + EXEC sp_dropextendedproperty @property_name, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name; |
| 518 | + END |
| 519 | + ELSE |
| 520 | + BEGIN |
| 521 | + IF @existing IS NULL |
| 522 | + EXEC sp_addextendedproperty @property_name,@comment, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name; |
| 523 | + ELSE IF @existing != @comment |
| 524 | + EXEC sp_updateextendedproperty @property_name, @comment, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name; |
| 525 | + END |
| 526 | + """) |
| 527 | + |
| 528 | + tsql_text = template.format( |
| 529 | + comment=exp.Literal.string(column_comment or "NULL").sql( |
| 530 | + dialect=self.dialect, identify=False |
| 531 | + ), |
| 532 | + schema_name=exp.Literal.string(table.db or "dbo").sql( |
| 533 | + dialect=self.dialect, identify=False |
| 534 | + ), |
| 535 | + object_name=exp.Literal.string(table.name).sql(dialect=self.dialect, identify=False), |
| 536 | + object_kind=table_kind, |
| 537 | + column_name=exp.Literal.string(column_name).sql(dialect=self.dialect, identify=False), |
| 538 | + ) |
| 539 | + |
| 540 | + return tsql_text |
0 commit comments