db.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from pathlib import Path
  2. import typer
  3. import config
  4. import database
  5. app = typer.Typer()
  6. cfg = config.Config()
  7. @app.command()
  8. def create(config_file: str):
  9. config_file = cfg.system_dir + f"\\SQL\\config\\{config_file}.json"
  10. database.create(config_file)
  11. @app.command()
  12. def compare(config_file: str):
  13. config_file = cfg.system_dir + f"\\SQL\\config\\{config_file}.json"
  14. database.compare(config_file)
  15. @app.command()
  16. def run(config_file: str, increment: str = "1", max: int = 5):
  17. """
  18. Executes a database operation using a specified configuration file.
  19. Args:
  20. config_file (str): The name of the configuration file (without extension) to use, located in the SQL config directory.
  21. increment (str, optional): Determines whether to increment; treated as True if "1", otherwise False. Defaults to "1".
  22. max (int, optional): The maximum number of operations to perform. Defaults to 5.
  23. Returns:
  24. None
  25. """
  26. config_file = cfg.system_dir + f"\\SQL\\config\\{config_file}.json"
  27. database.run(config_file, increment == "1", max)
  28. @app.command()
  29. def run_folder(folder: str):
  30. folder_list = [
  31. Path(folder),
  32. Path(cfg.system_dir + "\\SQL\\exec\\" + folder),
  33. Path(cfg.system_dir + "\\Export\\SQL\\" + folder),
  34. ]
  35. for f in folder_list:
  36. if f.exists() and f.is_dir():
  37. folder = str(f.resolve())
  38. break
  39. print(folder)
  40. for sql_file in Path(folder).glob("*.sql"):
  41. print(f"call sqlexec2.bat {sql_file}")
  42. @app.command()
  43. def schema():
  44. database.schema()
  45. @app.command()
  46. def bcp_log():
  47. logs_dir = cfg.system_dir + "\\SQL\\logs"
  48. if not Path(logs_dir).exists():
  49. logs_dir = cfg.system_dir + "\\SQL\\temp"
  50. output_file = cfg.log_dir + "\\bcp.csv.log"
  51. database.bcp_log(logs_dir, output_file)
  52. if __name__ == "__main__":
  53. app()