瀏覽代碼

Zeilenumbrüche

gc-server3 11 月之前
父節點
當前提交
91cbc16afb
共有 1 個文件被更改,包括 25 次插入44 次删除
  1. 25 44
      database/db_create.py

+ 25 - 44
database/db_create.py

@@ -65,7 +65,17 @@ class database_inspect:
             )
         if self.dsn.driver == "mysql":
             return f"mysql+pymysql://{self.dsn.user}:{self.dsn.password}@{self.dsn.server}/{self.dsn.database}?charset=utf8mb4"
-        return f"DSN={self.dsn.server};UID={self.dsn.user};PWD={self.dsn.password}"
+        return ";".join(
+            [
+                "Driver={PostgreSQL Unicode}",
+                f"Server={self.dsn.server}",
+                f"Port=5432",
+                f"Database={self.dsn.database}",
+                f"Uid={self.dsn.user}",
+                f"Pwd={self.dsn.password}",
+            ]
+        )
+        # f"DSN={self.dsn.server};UID={self.dsn.user};PWD={self.dsn.password}"
 
     def conn_ini(self):
         return "\r\n".join(
@@ -93,11 +103,7 @@ class database_inspect:
     def get_prefix(self):
         if (len(self.tables)) == 0:
             self.get_tables()
-        source_tables_prefix = dict(
-            enumerate(
-                sorted(list(set([t.split("$")[0] for t in self.tables if "$" in t]))), 1
-            )
-        )
+        source_tables_prefix = dict(enumerate(sorted(list(set([t.split("$")[0] for t in self.tables if "$" in t]))), 1))
         if len(source_tables_prefix) == 0:
             q = self.cursor.execute("select name FROM sys.databases")
             source_tables_prefix = [x[0] for x in q.fetchall()]
@@ -115,8 +121,7 @@ class database_inspect:
 
     def get_columns_is_typeof_str(self, table):
         source_insp_cols = [
-            col.data_type in [pyodbc.SQL_CHAR, pyodbc.SQL_VARCHAR]
-            for col in self.cursor.columns(table=table)
+            col.data_type in [pyodbc.SQL_CHAR, pyodbc.SQL_VARCHAR] for col in self.cursor.columns(table=table)
         ]
         if len(source_insp_cols) == 0:
             q = self.cursor.execute(
@@ -139,19 +144,13 @@ def load_config(config_file: str):
     base_dir = Path(config_file).resolve().parent
     cfg_import["name"] = Path(config_file).stem
     if cfg_import["stage_dir"][:2] == "..":
-        cfg_import["stage_dir"] = str(
-            base_dir.joinpath(cfg_import["stage_dir"]).resolve()
-        )
+        cfg_import["stage_dir"] = str(base_dir.joinpath(cfg_import["stage_dir"]).resolve())
     if cfg_import["batch_dir"][:2] == "..":
-        cfg_import["batch_dir"] = str(
-            base_dir.joinpath(cfg_import["batch_dir"]).resolve()
-        )
+        cfg_import["batch_dir"] = str(base_dir.joinpath(cfg_import["batch_dir"]).resolve())
     if "logs_dir" not in cfg_import:
         cfg_import["logs_dir"] = "..\\logs"
     if cfg_import["batch_dir"][:2] == "..":
-        cfg_import["batch_dir"] = str(
-            base_dir.joinpath(cfg_import["logs_dir"]).resolve()
-        )
+        cfg_import["batch_dir"] = str(base_dir.joinpath(cfg_import["logs_dir"]).resolve())
     return DbCreateConfig(**cfg_import)
 
 
@@ -175,31 +174,23 @@ def create(config_file="dbtools/OPTIMA.json"):  #
     target_tables = target_db.get_tables()
 
     for _, current_table in config.iterrows():
-        with open(
-            f"{cfg.batch_dir}/{current_table['target']}.bat", "w", encoding="cp850"
-        ) as f:
+        with open(f"{cfg.batch_dir}/{current_table['target']}.bat", "w", encoding="cp850") as f:
             f.write("@echo off \n")
             f.write("rem ==" + current_table["target"] + "==\n")
 
             if not current_table["target"] in target_tables:
-                f.write(
-                    f"echo Ziel-Tabelle '{current_table['target']}' existiert nicht!\n"
-                )
+                f.write(f"echo Ziel-Tabelle '{current_table['target']}' existiert nicht!\n")
                 print(f"Ziel-Tabelle '{current_table['target']}' existiert nicht!")
                 continue
 
-            f.write(
-                f"del {cfg.stage_dir}\\{current_table['target']}*.* /Q /F >nul 2>nul \n"
-            )
+            f.write(f"del {cfg.stage_dir}\\{current_table['target']}*.* /Q /F >nul 2>nul \n")
             f.write(
                 f"sqlcmd.exe {target_db.bcp_conn_params()} -p "
                 + f"-Q \"TRUNCATE TABLE [{cfg.target_dsn['schema']}].[{current_table['target']}]\" \n"
             )
 
             target_columns_list = target_db.get_columns(current_table["target"])
-            target_column_types = target_db.get_columns_is_typeof_str(
-                current_table["target"]
-            )
+            target_column_types = target_db.get_columns_is_typeof_str(current_table["target"])
 
             if "CLIENT_DB" in target_columns_list:
                 target_columns_list.remove("CLIENT_DB")
@@ -211,9 +202,7 @@ def create(config_file="dbtools/OPTIMA.json"):  #
                 if source_table not in source_tables:
                     source_table2 = source_db.convert_table(source_table)
                     if source_table2 not in source_tables:
-                        f.write(
-                            f"echo Quell-Tabelle '{source_table}' existiert nicht!\n"
-                        )
+                        f.write(f"echo Quell-Tabelle '{source_table}' existiert nicht!\n")
                         print(f"Quell-Tabelle '{source_table}' existiert nicht!")
                         continue
 
@@ -227,18 +216,14 @@ def create(config_file="dbtools/OPTIMA.json"):  #
                 diff2 = target_columns.difference(source_columns)
                 if "Client_DB" not in diff2:
                     f.write("echo Spalte 'Client_DB' fehlt!\n")
-                    print(
-                        f"Ziel-Tabelle '{current_table['target']}' Spalte 'Client_DB' fehlt!"
-                    )
+                    print(f"Ziel-Tabelle '{current_table['target']}' Spalte 'Client_DB' fehlt!")
                     continue
                 diff2.remove("Client_DB")
                 if len(diff2) > 0:
                     f.write("rem Nur in Ziel:   " + ";".join(diff2) + "\n")
 
                 if not pd.isnull(current_table["query"]):
-                    select_query = current_table["query"].format(
-                        prefix, cfg.filter[0], cfg.filter[1]
-                    )
+                    select_query = current_table["query"].format(prefix, cfg.filter[0], cfg.filter[1])
                 elif "." in source_table or cfg.source_dsn["schema"] == "":
                     if source_table[0] != "[":
                         source_table = f"[{source_table}]"
@@ -247,9 +232,7 @@ def create(config_file="dbtools/OPTIMA.json"):  #
                     select_query = f"SELECT T1.* FROM [{cfg.source_dsn['schema']}].[{source_table}] T1 "
 
                 if not pd.isnull(current_table["filter"]):
-                    select_query += " WHERE " + current_table["filter"].format(
-                        "", cfg.filter[0], cfg.filter[1]
-                    )
+                    select_query += " WHERE " + current_table["filter"].format("", cfg.filter[0], cfg.filter[1])
                 # select_columns = "T1.[" + "], T1.[".join(intersect) + "],"
                 select_columns = ""
                 for col, col_type in zip(target_columns_list, target_column_types):
@@ -266,9 +249,7 @@ def create(config_file="dbtools/OPTIMA.json"):  #
                 select_query = select_query.replace("T1.*", select_columns[:-2])
                 select_query = select_query.replace("%", "%%")  # batch-Problem
 
-                stage_csv = (
-                    f"{cfg.stage_dir}\\{current_table['target']}_{client_db}.csv"
-                )
+                stage_csv = f"{cfg.stage_dir}\\{current_table['target']}_{client_db}.csv"
                 # insert_query = f"LOAD DATA INFILE '{stage_csv}' INTO TABLE {current_table['target']} FIELDS TERMINATED BY ','
                 # ENCLOSED BY '\"' LINES TERMINATED BY '\n';"