|
@@ -5,21 +5,40 @@ from collections import defaultdict
|
|
|
|
|
|
|
|
|
class IqdConverter:
|
|
|
- commands = ["convert"]
|
|
|
+ commands = ["convert", "run_folder"]
|
|
|
target_schema_by_type = {"csv": "staging", "ims": "ims"}
|
|
|
- output_dir = "C:\\GlobalCube\\System\\LOCOSOFT\\SQL\\schema\\LOCOSOFT\\views_imr"
|
|
|
+ output_dir = "C:\\GlobalCube\\System\\OPTIMA\\SQL\\schema\\OPTIMA\\views_imr"
|
|
|
|
|
|
- def convert(self, iqd_file, target_type="csv"):
|
|
|
+ def convert(self, iqd_file, target_type="csv", force=True):
|
|
|
if not Path(iqd_file).exists():
|
|
|
print(f"File {iqd_file} does not exist!")
|
|
|
return
|
|
|
+ output_file = iqd_file[:-4] + ".sql"
|
|
|
+ if (
|
|
|
+ not force
|
|
|
+ and Path(output_file).exists()
|
|
|
+ and Path(output_file).stat().st_mtime >= Path(iqd_file).stat().st_mtime
|
|
|
+ ):
|
|
|
+ # no update needed
|
|
|
+ return
|
|
|
+
|
|
|
query = self.get_query_from_iqdfile(iqd_file)
|
|
|
query = self.cleanup_query(query)
|
|
|
|
|
|
- output_file = iqd_file[:-4] + ".sql"
|
|
|
with open(output_file, "w", encoding="latin-1") as fwh:
|
|
|
fwh.write(query)
|
|
|
|
|
|
+ create_view, view_file = self.get_create_view(iqd_file, target_type, query)
|
|
|
+
|
|
|
+ with open(view_file, "w", encoding="latin-1") as fwh:
|
|
|
+ fwh.write(create_view)
|
|
|
+
|
|
|
+ create_view_90 = self.get_create_view_90(query)
|
|
|
+
|
|
|
+ with open(view_file.replace("_imr.sql", "_90.sql"), "w", encoding="latin-1") as fwh:
|
|
|
+ fwh.write(create_view_90)
|
|
|
+
|
|
|
+ def get_create_view(self, iqd_file, target_type, query):
|
|
|
table = Path(iqd_file).stem.lower()
|
|
|
if target_type == "csv":
|
|
|
table += "_imr"
|
|
@@ -30,10 +49,32 @@ class IqdConverter:
|
|
|
+ f"CREATE VIEW {schema}.{table} AS\n\n{query}\n"
|
|
|
+ "GO\nSET QUOTED_IDENTIFIER OFF \nGO\nSET ANSI_NULLS OFF \nGO\n\nGO"
|
|
|
)
|
|
|
-
|
|
|
view_file = f"{self.output_dir}\\{schema}.{table}.sql"
|
|
|
- with open(view_file, "w", encoding="latin-1") as fwh:
|
|
|
- fwh.write(create_view)
|
|
|
+ return create_view, view_file
|
|
|
+
|
|
|
+ def get_create_view_90(self, query):
|
|
|
+ match = re.findall(r"(\"([^\"]+)\"\.)?\"([^\"]+)\"\ (T\d+)", query)
|
|
|
+ tables = dict([(t[3], t[2]) for t in match])
|
|
|
+
|
|
|
+ query_from_ori = re.search(r"from ([^\s].*)where", query, re.DOTALL).group(1)
|
|
|
+ query_from = query_from_ori.replace("(", "").replace(")", "")
|
|
|
+
|
|
|
+ query_where_ori = re.search(r"where (.*)", query, re.DOTALL).group(1)
|
|
|
+ query_where = re.sub(r"-- order by.*", "", query_where_ori)
|
|
|
+ # query_where = query_where_ori.replace("(", "").replace(")", "")
|
|
|
+
|
|
|
+ match = re.findall(r"T\d+\.\"[^\"]+\"", query)
|
|
|
+ columns = list(sorted(list(set(match))))
|
|
|
+ cols_alias = []
|
|
|
+ for col in columns:
|
|
|
+ t_name, col_name = re.search(r"(T\d+)\.\"([^\"]+)\"", col).group(1, 2)
|
|
|
+ if True or col_name in cols_alias:
|
|
|
+ table_name = tables.get(t_name, t_name)
|
|
|
+ cols_alias.append(f"{col_name}__{table_name}")
|
|
|
+ else:
|
|
|
+ cols_alias.append(col_name)
|
|
|
+ columns_combined = [f'{c} AS "{a}"' for c, a in zip(columns, cols_alias)]
|
|
|
+ return "SELECT " + ", ".join(columns_combined) + " FROM " + query_from + " WHERE " + query_where
|
|
|
|
|
|
def get_query_from_iqdfile(self, iqd_file):
|
|
|
if iqd_file[-4:].lower() == ".imr":
|
|
@@ -120,7 +161,7 @@ class IqdConverter:
|
|
|
query = re.sub(r"[^ ](order by .*)", r"\n-- \1", query)
|
|
|
return query
|
|
|
|
|
|
- def run_folder(self, base_dir: str):
|
|
|
+ def run_folder(self, base_dir):
|
|
|
files = sorted([(f.stat().st_mtime, f) for f in Path(base_dir).rglob("*.iqd")])
|
|
|
for timestamp, iqd in files:
|
|
|
self.convert(str(iqd))
|
|
@@ -128,5 +169,7 @@ class IqdConverter:
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
# IqdConverter().convert('C:\\GlobalCube_LOCOSOFT\\System\\LOCOSOFT\\IQD\\Serv_Teile\\offene_Auftraege_Ums_ben_AW.iqd')
|
|
|
- # IqdConverter().run_folder('C:\\Projekte\\DWH\\CARLO\\IQD')
|
|
|
- plac.Interpreter.call(IqdConverter)
|
|
|
+ iqdconv = IqdConverter()
|
|
|
+ iqdconv.output_dir = "C:\\GlobalCube_LOCOSOFT\\System\\LOCOSOFT\\SQL\\schema\\LOCOSOFT\\views_imr"
|
|
|
+ iqdconv.run_folder("C:\\GlobalCube_LOCOSOFT\\System\\LOCOSOFT\\IQD")
|
|
|
+ # plac.Interpreter.call(IqdConverter)
|