14
14
# KIND, either express or implied. See the License for the
15
15
# specific language governing permissions and limitations
16
16
# under the License.
17
+ import logging
18
+ from typing import Optional , Union
19
+
20
+ from sqlalchemy .engine .reflection import Inspector
21
+
17
22
from superset .constants import TimeGrain
18
23
from superset .db_engine_specs .base import BaseEngineSpec , LimitMethod
19
24
25
+ logger = logging .getLogger (__name__ )
26
+
20
27
21
28
class Db2EngineSpec (BaseEngineSpec ):
22
29
engine = "db2"
@@ -26,6 +33,8 @@ class Db2EngineSpec(BaseEngineSpec):
26
33
force_column_alias_quotes = True
27
34
max_column_name_length = 30
28
35
36
+ supports_dynamic_schema = True
37
+
29
38
_time_grain_expressions = {
30
39
None : "{col}" ,
31
40
TimeGrain .SECOND : "CAST({col} as TIMESTAMP) - MICROSECOND({col}) MICROSECONDS" ,
@@ -52,3 +61,49 @@ class Db2EngineSpec(BaseEngineSpec):
52
61
@classmethod
53
62
def epoch_to_dttm (cls ) -> str :
54
63
return "(TIMESTAMP('1970-01-01', '00:00:00') + {col} SECONDS)"
64
+
65
+ @classmethod
66
+ def get_table_comment (
67
+ cls , inspector : Inspector , table_name : str , schema : Union [str , None ]
68
+ ) -> Optional [str ]:
69
+ """
70
+ Get comment of table from a given schema
71
+
72
+ Ibm Db2 return comments as tuples, so we need to get the first element
73
+
74
+ :param inspector: SqlAlchemy Inspector instance
75
+ :param table_name: Table name
76
+ :param schema: Schema name. If omitted, uses default schema for database
77
+ :return: comment of table
78
+ """
79
+ comment = None
80
+ try :
81
+ table_comment = inspector .get_table_comment (table_name , schema )
82
+ comment = table_comment .get ("text" )
83
+ return comment [0 ]
84
+ except IndexError :
85
+ return comment
86
+ except Exception as ex : # pylint: disable=broad-except
87
+ logger .error ("Unexpected error while fetching table comment" , exc_info = True )
88
+ logger .exception (ex )
89
+ return comment
90
+
91
+ @classmethod
92
+ def get_prequeries (
93
+ cls ,
94
+ catalog : Union [str , None ] = None ,
95
+ schema : Union [str , None ] = None ,
96
+ ) -> list [str ]:
97
+ """
98
+ Set the search path to the specified schema.
99
+
100
+ This is important for two reasons: in SQL Lab it will allow queries to run in
101
+ the schema selected in the dropdown, resolving unqualified table names to the
102
+ expected schema.
103
+
104
+ But more importantly, in SQL Lab this is used to check if the user has access to
105
+ any tables with unqualified names. If the schema is not set by SQL Lab it could
106
+ be anything, and we would have to block users from running any queries
107
+ referencing tables without an explicit schema.
108
+ """
109
+ return [f'set current_schema "{ schema } "' ] if schema else []
0 commit comments