iqd_convert.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from pathlib import Path
  2. import re
  3. import plac
  4. class IqdConverter:
  5. commands = ['convert']
  6. target_schema_by_type = {
  7. 'csv': 'staging',
  8. 'ims': 'ims'
  9. }
  10. output_dir = 'C:\\GlobalCube_LOCOSOFT\\System\\LOCOSOFT\\SQL\\schema\\LOCOSOFT\\views'
  11. def convert(self, iqd_file, target_type='csv'):
  12. query = self.get_query_from_iqdfile(iqd_file)
  13. query = self.cleanup_query(query)
  14. output_file = iqd_file[:-4] + '.sql'
  15. with open(output_file, 'w', encoding='latin-1') as fwh:
  16. fwh.write(query)
  17. table = Path(iqd_file).stem
  18. schema = self.target_schema_by_type[target_type]
  19. create_view = "SET QUOTED_IDENTIFIER ON \nGO\nSET ANSI_NULLS ON \nGO\n" + \
  20. f"CREATE VIEW {schema}.{table} AS\n\n{query}\n" + \
  21. "GO\nSET QUOTED_IDENTIFIER OFF \nGO\nSET ANSI_NULLS OFF \nGO\n\nGO"
  22. view_file = f"{self.output_dir}\\{schema}.{table}.sql"
  23. with open(view_file, 'w', encoding='latin-1') as fwh:
  24. fwh.write(create_view)
  25. def get_query_from_iqdfile(self, iqd_file):
  26. if iqd_file[-4:].lower() == '.imr':
  27. iqd_file = iqd_file[:-4] + '.iqd'
  28. iqd = open(iqd_file, 'r', encoding='latin-1').read()
  29. res = re.findall(r'BEGIN SQL\n(.*)\n\nEND SQL', iqd, re.MULTILINE | re.DOTALL)
  30. query = ''
  31. if len(res) > 0:
  32. query = res[0]
  33. columns = re.findall(r'COLUMN,\d+,(.*)', iqd)
  34. col_keys = [f'c{i}' for i in range(len(columns), 0, -1)]
  35. col_names = [f'"{c}"' for c in reversed(columns)]
  36. for col_key, col_name in zip(col_keys, col_names):
  37. query = re.sub(col_key + r'([^\d])', col_name + r'\1', query)
  38. columns2 = re.findall(r'\s+(c\d+) as (".*")', query)
  39. for col in columns2:
  40. query = re.sub(col[0] + r' as ' + col[1], col[1], query)
  41. query = re.sub(col[0] + r'([^\d])', col[1] + r'\1', query)
  42. return query
  43. def cleanup_query(self, query):
  44. query = re.sub(r' from (\d+) for (\d+)\)', r', \1, \2)', query)
  45. query = query.replace(' || ', ' + ')
  46. query = query.replace('truncate(', 'rtrim(')
  47. query = query.replace('char_length(', 'len(')
  48. query = query.replace('ascii(', 'convert(varchar(50), ')
  49. query = query.replace('extract(DAY FROM ', 'day(')
  50. query = query.replace('extract(MONTH FROM ', 'month(')
  51. query = query.replace('extract(YEAR FROM ', 'year(')
  52. query = query.replace('cdatetime(', 'convert(datetime, ')
  53. query = query.replace('cast_float(', 'convert(float, ')
  54. query = query.replace('sy_right(', 'right(')
  55. query = query.replace('od_left(', 'left(')
  56. query = query.replace('length(', 'len(')
  57. query = query.replace('{hour}', 'hour')
  58. query = query.replace('{minute}', 'minute')
  59. query = query.replace('{weekday}', 'weekday')
  60. query = query.replace('@CURRENT_DATE', 'getdate()')
  61. query = query.replace('cdate(', '(')
  62. query = re.sub(r'intdiv\(([^\)]+)\,1\)', r'\1', query)
  63. query = re.sub(r'XCOUNT\(([^\)]+) for ', r'COUNT(\1) OVER (partition by ', query)
  64. query = re.sub(r'XSUM\(([^\)]+) for ', r'SUM(\1) OVER (partition by ', query)
  65. query = re.sub(r'XMIN\(([^\)]+) for ', r'MIN(\1) OVER (partition by ', query)
  66. query = re.sub(r'XMAX\(([^\)]+) for ', r'MAX(\1) OVER (partition by ', query)
  67. query = re.sub(r'QSS\.\"[^\"]+\\([^\\]+)\.ims\"', r'"ims"."\1"', query)
  68. query = re.sub(r"DATE '([\d-]+)'", r"convert(date, '\1')", query)
  69. query = re.sub(r"TIMESTAMP '([\d\s\-\:\.]+)'", r"convert(datetime, '\1')", query)
  70. query = re.sub(r'asciiz\(([^\,]*)\,\d+\)', r'convert(varchar(50), \1)', query)
  71. query = re.sub(r'asciiz\(([^\+]*)\)', r'convert(varchar(50), \1)', query)
  72. query = re.sub(r'convert\(varchar\(50\)\, ([^,]+)\,\d+\)', r'convert(varchar(50), \1)', query)
  73. query = query.replace('cdate((convert(float, ', 'convert(datetime, ((')
  74. query = re.sub(r'(order by .*)', r'-- \1', query)
  75. return query
  76. def run_folder(self, base_dir: str):
  77. files = sorted([(f.stat().st_mtime, f) for f in Path(base_dir).rglob('*.iqd')])
  78. for timestamp, iqd in files:
  79. self.convert(str(iqd))
  80. if __name__ == '__main__':
  81. # IqdConverter().convert('C:\\GlobalCube_LOCOSOFT\\System\\LOCOSOFT\\IQD\\Serv_Teile\\offene_Auftraege_Ums_ben_AW.iqd')
  82. # IqdConverter().run_folder('C:\\Projekte\\DWH\\CARLO\\IQD')
  83. plac.Interpreter.call(IqdConverter)