]> git.phdru.name Git - sqlconvert.git/blob - demo/demo-process.py
Fix flake8 warning: remove unused import
[sqlconvert.git] / demo / demo-process.py
1 #! /usr/bin/env python
2 from __future__ import print_function
3
4 import argparse
5 from sqlconvert.process_mysql import process_statement
6 from sqlconvert.print_tokens import print_tokens
7 from sqlconvert.process_tokens import find_error, StatementGrouper
8
9
10 def process_lines(*lines):
11     grouper = StatementGrouper(encoding='utf-8')
12     for line in lines:
13         grouper.process_line(line)
14         if grouper.statements:
15             for statement in grouper.get_statements():
16                 print("----- -----")
17                 if find_error(statement):
18                     print("ERRORS IN QUERY")
19                 process_statement(statement)
20                 print_tokens(statement, encoding='utf-8')
21                 print()
22                 statement._pprint_tree()
23             print("-----/-----")
24     tokens = grouper.close()
25     if tokens:
26         for token in tokens:
27             print_tokens(token, encoding='utf-8')
28             print(repr(token))
29
30
31 def process_test(_args):
32     process_lines(
33         "SELECT * FROM `mytable`; -- line-comment",
34         "INSERT into /* inline comment */ mytable VALUES (1, 'one');",
35         "/*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`) "
36         "VALUES (1, 'one');"
37     )
38
39
40 def process_sql(args):
41     process_lines(*args.lines)
42
43
44 def process_file(args):
45     infile = open(args.filename, 'rt')
46     lines = infile.readlines()
47     infile.close()
48     process_lines(*lines)
49
50
51 if __name__ == '__main__':
52     main_parser = argparse.ArgumentParser(description='Group')
53     subparsers = main_parser.add_subparsers(help='Commands')
54
55     parser = subparsers.add_parser('sql', help='SQL from command line')
56     parser.add_argument('lines', nargs='+', help='SQL lines')
57     parser.set_defaults(func=process_sql)
58
59     parser = subparsers.add_parser('test', help='SQL from test data')
60     parser.set_defaults(func=process_test)
61
62     parser = subparsers.add_parser('file', help='SQL from a file')
63     parser.add_argument('filename', help='SQL file')
64     parser.set_defaults(func=process_file)
65
66     args = main_parser.parse_args()
67     args.func(args)