commit 2332a6d8c867e02b831aa923658a4c4677d602e1 Author: Holger Sielaff Date: Thu Jun 26 15:41:03 2025 +0200 Initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b438785 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.idea +__pychache__ +*.swp +~* +*.pyc diff --git a/sql_oneline.py b/sql_oneline.py new file mode 100644 index 0000000..47d2d6a --- /dev/null +++ b/sql_oneline.py @@ -0,0 +1,37 @@ +import sys +import re +import argparse +import os + +ap = argparse.ArgumentParser() +ap.add_argument('-r', '--regex', required=False, default=r'^(\s|\t)*(INSERT|DELETE|UPDATE|SELECT)(\s|\t)') +ap.add_argument('-e', '--ext', required=False, default='oneline') +ap.add_argument('-l', '--fromlog', required=False, default=False, action='store_true') +args, files = ap.parse_known_args() + +assert files, "Kein File angegeben" +assert all(map(os.path.isfile, files)), "Eines der Files gibt es nicht" + +regstr = args.regex + +if args.fromlog and '-r' not in sys.argv: + # We begin wit a date in a usual sql log + regstr = r'^(\t|\s)*[0-9]{2,4}[\-./][0-9]{2}[\-./][0-9]{2,4}' + +newline_reg = re.compile(regstr, re.IGNORECASE) + +for file in files: + lastline = '' + _name, _ext = os.path.splitext(file) + oneline_file = f"{_name}.{args.ext}{_ext}" + with open(file) as rf, open(oneline_file, 'w') as wf: + for line in rf: + line = line.strip() + if newline_reg.search(line): + if lastline and not lastline.endswith(';'): + wf.write(";") + wf.write("\n") + wf.write(line + ' ') + lastline = line + print(f"{file} onelined to {oneline_file}") +