sql_refactoring.py 545 B

123456789101112131415161718
  1. from pathlib import Path
  2. import sqlglot
  3. from sqlglot.optimizer import optimize
  4. def main():
  5. output_path = Path("sandbox/sql/converted")
  6. for file in Path("sandbox/sql/raw").glob("*.sql"):
  7. with file.open("r", encoding="latin-1") as frh:
  8. res = optimize(sqlglot.parse_one(frh.read(), read="tsql"))
  9. output_file = output_path / file.name
  10. with output_file.open("w", encoding="latin-1") as fwh:
  11. fwh.write(res.sql(dialect="tsql", pretty=True))
  12. if __name__ == "__main__":
  13. main()