Skip to content
This repository was archived by the owner on May 5, 2022. It is now read-only.

Commit be859cd

Browse files
committed
feat: implement get_table_comment
1 parent 8e97d23 commit be859cd

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

sqlalchemy_trino/dialect.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def get_view_definition(self, connection: Connection, view_name: str, schema: st
170170
query = f'SHOW CREATE VIEW {full_view}'
171171
try:
172172
res = connection.execute(sql.text(query))
173-
return res.first()[0]
173+
return res.scalar()
174174
except error.TrinoQueryError as e:
175175
if e.error_name in (
176176
error.TABLE_NOT_FOUND,
@@ -204,8 +204,20 @@ def get_check_constraints(self, connection: Connection,
204204
return []
205205

206206
def get_table_comment(self, connection: Connection,
207-
table_name: str, schema: str = None, **kw) -> str:
208-
pass
207+
table_name: str, schema: str = None, **kw) -> Dict[str, Any]:
208+
properties_table = self._get_full_table(f"{table_name}$properties", schema)
209+
query = f'SELECT "comment" FROM {properties_table}'
210+
try:
211+
res = connection.execute(sql.text(query))
212+
return dict(text=res.scalar())
213+
except error.TrinoQueryError as e:
214+
if e.error_name in (
215+
error.NOT_FOUND,
216+
error.COLUMN_NOT_FOUND,
217+
error.TABLE_NOT_FOUND,
218+
):
219+
return dict(text=None)
220+
raise
209221

210222
def has_schema(self, connection: Connection, schema: str) -> bool:
211223
query = f"SHOW SCHEMAS LIKE '{schema}'"

sqlalchemy_trino/error.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
)
44

55
# ref: https://github.com/trinodb/trino/blob/master/core/trino-spi/src/main/java/io/trino/spi/StandardErrorCode.java
6+
NOT_FOUND = 'NOT_FOUND'
7+
COLUMN_NOT_FOUND = 'COLUMN_NOT_FOUND'
68
TABLE_NOT_FOUND = 'TABLE_NOT_FOUND'
79
SCHEMA_NOT_FOUND = 'SCHEMA_NOT_FOUND'
810
CATALOG_NOT_FOUND = 'CATALOG_NOT_FOUND'

0 commit comments

Comments
 (0)