|
1 | 1 | import re |
2 | | -from sqlalchemy.sql import compiler |
| 2 | +from sqlalchemy.sql import compiler, sqltypes |
3 | 3 | import logging |
4 | 4 |
|
5 | 5 | logger = logging.getLogger(__name__) |
@@ -39,17 +39,40 @@ def visit_identity_column(self, identity, **kw): |
39 | 39 | ) |
40 | 40 | return text |
41 | 41 |
|
| 42 | + def visit_set_column_comment(self, create, **kw): |
| 43 | + return "ALTER TABLE %s ALTER COLUMN %s COMMENT %s" % ( |
| 44 | + self.preparer.format_table(create.element.table), |
| 45 | + self.preparer.format_column(create.element), |
| 46 | + self.sql_compiler.render_literal_value( |
| 47 | + create.element.comment, sqltypes.String() |
| 48 | + ), |
| 49 | + ) |
| 50 | + |
| 51 | + def visit_drop_column_comment(self, create, **kw): |
| 52 | + return "ALTER TABLE %s ALTER COLUMN %s COMMENT ''" % ( |
| 53 | + self.preparer.format_table(create.element.table), |
| 54 | + self.preparer.format_column(create.element), |
| 55 | + ) |
| 56 | + |
42 | 57 | def get_column_specification(self, column, **kwargs): |
43 | | - """Currently we override this method only to emit a log message if a user attempts to set |
44 | | - autoincrement=True on a column. See comments in test_suite.py. We may implement implicit |
45 | | - IDENTITY using this feature in the future, similar to the Microsoft SQL Server dialect. |
| 58 | + """ |
| 59 | + Emit a log message if a user attempts to set autoincrement=True on a column. |
| 60 | + See comments in test_suite.py. We may implement implicit IDENTITY using this |
| 61 | + feature in the future, similar to the Microsoft SQL Server dialect. |
46 | 62 | """ |
47 | 63 | if column is column.table._autoincrement_column or column.autoincrement is True: |
48 | 64 | logger.warn( |
49 | 65 | "Databricks dialect ignores SQLAlchemy's autoincrement semantics. Use explicit Identity() instead." |
50 | 66 | ) |
51 | 67 |
|
52 | | - return super().get_column_specification(column, **kwargs) |
| 68 | + colspec = super().get_column_specification(column, **kwargs) |
| 69 | + if column.comment is not None: |
| 70 | + literal = self.sql_compiler.render_literal_value( |
| 71 | + column.comment, sqltypes.STRINGTYPE |
| 72 | + ) |
| 73 | + colspec += " COMMENT " + literal |
| 74 | + |
| 75 | + return colspec |
53 | 76 |
|
54 | 77 |
|
55 | 78 | class DatabricksStatementCompiler(compiler.SQLCompiler): |
|
0 commit comments