From 6285bbcc0fc9ff36b8c84204fc7101560344c9e2 Mon Sep 17 00:00:00 2001 From: Hugo Cisneros Date: Wed, 24 May 2023 17:52:01 +0200 Subject: [PATCH 1/5] Create .pre-commit-hooks.yaml --- .pre-commit-hooks.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .pre-commit-hooks.yaml diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 00000000..41c1ef71 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,6 @@ +- id: sqlformat + name: SQL Formatter + entry: sqlformat -r -k upper -s --wrap_after 120 --inplace + language: python + files: \.sql$ + types: [text, file] From ee291310c082357105620f7307d9fa2d49f1ec0c Mon Sep 17 00:00:00 2001 From: Hugo Cisneros Date: Wed, 24 May 2023 18:23:17 +0200 Subject: [PATCH 2/5] Update cli.py --- sqlparse/cli.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/sqlparse/cli.py b/sqlparse/cli.py index 7a8aacbf..1bfda44c 100755 --- a/sqlparse/cli.py +++ b/sqlparse/cli.py @@ -39,7 +39,7 @@ def create_parser(): usage='%(prog)s [OPTIONS] FILE, ...', ) - parser.add_argument('filename') + parser.add_argument('filename', nargs='+') parser.add_argument( '-o', '--outfile', @@ -144,6 +144,12 @@ def create_parser(): dest='encoding', default='utf-8', help='Specify the input encoding (default utf-8)') + + group.add_argument( + '--inplace', + action='store_true', + default=False, + help='modify file in place') return parser @@ -153,12 +159,8 @@ def _error(msg): sys.stderr.write('[ERROR] {}\n'.format(msg)) return 1 - -def main(args=None): - parser = create_parser() - args = parser.parse_args(args) - - if args.filename == '-': # read from stdin +def process_file(args, filename): + if filename == ['-']: # read from stdin wrapper = TextIOWrapper(sys.stdin.buffer, encoding=args.encoding) try: data = wrapper.read() @@ -166,11 +168,11 @@ def main(args=None): wrapper.detach() else: try: - with open(args.filename, encoding=args.encoding) as f: + with open(filename, encoding=args.encoding) as f: data = ''.join(f.readlines()) except OSError as e: return _error( - 'Failed to read {}: {}'.format(args.filename, e)) + 'Failed to read {}: {}'.format(filename, e)) close_stream = False if args.outfile: @@ -179,6 +181,8 @@ def main(args=None): close_stream = True except OSError as e: return _error('Failed to open {}: {}'.format(args.outfile, e)) + elif filename != '-' and args.inplace: + stream = open(filename, 'w', encoding=args.encoding) else: stream = sys.stdout @@ -194,3 +198,11 @@ def main(args=None): if close_stream: stream.close() return 0 + + +def main(args=None): + parser = create_parser() + args = parser.parse_args(args) + for filename in args.filename: + process_file(args, filename) + return 0 From 1afe3c17187ad127738e07d891fcd67a3c29beb9 Mon Sep 17 00:00:00 2001 From: Hugo Cisneros Date: Mon, 12 Jun 2023 15:49:44 +0200 Subject: [PATCH 3/5] Strip file to remove newlines --- sqlparse/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlparse/cli.py b/sqlparse/cli.py index 1bfda44c..382a0f47 100755 --- a/sqlparse/cli.py +++ b/sqlparse/cli.py @@ -192,7 +192,7 @@ def process_file(args, filename): except SQLParseError as e: return _error('Invalid options: {}'.format(e)) - s = sqlparse.format(data, **formatter_opts) + s = sqlparse.format(data, **formatter_opts).strip() stream.write(s) stream.flush() if close_stream: From 938c5e6d4b3efe2590abc0e099c8193e39b9a058 Mon Sep 17 00:00:00 2001 From: Hugo Cisneros Date: Mon, 12 Jun 2023 15:52:22 +0200 Subject: [PATCH 4/5] Update cli.py --- sqlparse/cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sqlparse/cli.py b/sqlparse/cli.py index 382a0f47..18ba659b 100755 --- a/sqlparse/cli.py +++ b/sqlparse/cli.py @@ -194,6 +194,7 @@ def process_file(args, filename): s = sqlparse.format(data, **formatter_opts).strip() stream.write(s) + stream.write("\n") # Newline at end of file stream.flush() if close_stream: stream.close() From e19f0370ca621722261f1f203520f2d9efcf1545 Mon Sep 17 00:00:00 2001 From: hugcis Date: Thu, 21 Sep 2023 14:03:50 +0200 Subject: [PATCH 5/5] Check for idempotence --- sqlparse/cli.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sqlparse/cli.py b/sqlparse/cli.py index 18ba659b..0594a1bf 100755 --- a/sqlparse/cli.py +++ b/sqlparse/cli.py @@ -193,6 +193,8 @@ def process_file(args, filename): return _error('Invalid options: {}'.format(e)) s = sqlparse.format(data, **formatter_opts).strip() + if (new_s := sqlparse.format(s, **formatter_opts).strip()) != s and len(new_s) < len(s): # No idempotence + s = new_s stream.write(s) stream.write("\n") # Newline at end of file stream.flush()