1+ import re
12import typing
23
34from testgen .commands .queries .refresh_data_chars_query import CRefreshDataCharsSQL
45from testgen .commands .queries .rollup_scores_query import CRollupScoresSQL
56from testgen .common import date_service , read_template_sql_file , read_template_yaml_file
6- from testgen .common .database .database_service import replace_params
7+ from testgen .common .database .database_service import get_flavor_service , replace_params
78from testgen .common .read_file import replace_templated_functions
89
910
@@ -21,6 +22,7 @@ class CProfilingSQL:
2122 col_name = ""
2223 col_gen_type = ""
2324 col_type = ""
25+ db_data_type = ""
2426 col_ordinal_position = "0"
2527 col_is_decimal = ""
2628 col_top_freq_update = ""
@@ -98,6 +100,7 @@ def _get_params(self) -> dict:
98100 "COL_NAME_SANITIZED" : self .col_name .replace ("'" , "''" ),
99101 "COL_GEN_TYPE" : self .col_gen_type ,
100102 "COL_TYPE" : self .col_type or "" ,
103+ "DB_DATA_TYPE" : self .db_data_type or "" ,
101104 "COL_POS" : self .col_ordinal_position ,
102105 "TOP_FREQ" : self .col_top_freq_update ,
103106 "PROFILE_RUN_ID" : self .profile_run_id ,
@@ -118,6 +121,7 @@ def _get_params(self) -> dict:
118121 "CONTINGENCY_MAX_VALUES" : self .contingency_max_values ,
119122 "PROCESS_ID" : self .process_id ,
120123 "SQL_FLAVOR" : self .flavor ,
124+ "QUOTE" : get_flavor_service (self .flavor ).quote_character
121125 }
122126
123127 def _get_query (
@@ -130,6 +134,7 @@ def _get_query(
130134 params = {}
131135
132136 if query :
137+ query = self ._process_conditionals (query )
133138 if extra_params :
134139 params .update (extra_params )
135140 params .update (self ._get_params ())
@@ -139,6 +144,33 @@ def _get_query(
139144
140145 return query , params
141146
147+ def _process_conditionals (self , query : str ):
148+ re_pattern = re .compile (r"^--\s+TG-(IF|ELSE|ENDIF)(?:\s+(\w+))?\s*$" )
149+ condition = None
150+ updated_query = []
151+ for line in query .splitlines (True ):
152+ if re_match := re_pattern .match (line ):
153+ match re_match .group (1 ):
154+ case "IF" if condition is None and re_match .group (2 ) is not None :
155+ condition = bool (getattr (self , re_match .group (2 )))
156+ case "ELSE" if condition is not None :
157+ condition = not condition
158+ case "ENDIF" if condition is not None :
159+ condition = None
160+ case _:
161+ raise ValueError ("Template conditional misused" )
162+ elif condition is not False :
163+ updated_query .append (line )
164+
165+ if condition is not None :
166+ raise ValueError ("Template conditional misused" )
167+
168+ return "" .join (updated_query )
169+
170+ @property
171+ def do_sample_bool (self ):
172+ return self .parm_do_sample == "Y"
173+
142174 def GetSecondProfilingColumnsQuery (self ) -> tuple [str , dict ]:
143175 # Runs on App database
144176 return self ._get_query ("secondary_profiling_columns.sql" )
@@ -260,7 +292,12 @@ def GetProfilingQuery(self) -> tuple[str, dict]:
260292 else :
261293 strQ += dctSnippetTemplate ["strTemplate01_else" ]
262294
263- strQ += dctSnippetTemplate ["strTemplate02_all" ]
295+ strQ += dctSnippetTemplate ["strTemplate01_5" ]
296+
297+ if self .col_gen_type == "X" :
298+ strQ += dctSnippetTemplate ["strTemplate02_X" ]
299+ else :
300+ strQ += dctSnippetTemplate ["strTemplate02_else" ]
264301
265302 if self .col_gen_type in ["A" , "D" , "N" ]:
266303 strQ += dctSnippetTemplate ["strTemplate03_ADN" ]
0 commit comments