]> git.phdru.name Git - sqlconvert.git/blob - demo/demo-process.py
Build(setup.py): Use setuptools instead of distutils
[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         for statement in grouper.get_statements():
15             print("----- -----")
16             if find_error(statement):
17                 print("ERRORS IN QUERY")
18             for statement in process_statement(statement):
19                 print_tokens(statement, encoding='utf-8')
20                 print()
21                 statement._pprint_tree()
22             print("-----/-----")
23     tokens = grouper.close()
24     if tokens:
25         for token in tokens:
26             print_tokens(token, encoding='utf-8')
27             print(repr(token))
28
29
30 def process_test(_args):
31     process_lines(
32         "SELECT * FROM `mytable`; -- line-comment",
33         "INSERT into /* inline comment */ mytable VALUES (1, 'one');",
34         "/*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`) "
35         "VALUES (1, 'one');"
36     )
37
38
39 def process_sql(args):
40     process_lines(*args.lines)
41
42
43 def process_file(args):
44     infile = open(args.filename, 'rt')
45     lines = infile.readlines()
46     infile.close()
47     process_lines(*lines)
48
49
50 if __name__ == '__main__':
51     main_parser = argparse.ArgumentParser(description='Group')
52     subparsers = main_parser.add_subparsers(help='Commands')
53
54     parser = subparsers.add_parser('sql', help='SQL from command line')
55     parser.add_argument('lines', nargs='+', help='SQL lines')
56     parser.set_defaults(func=process_sql)
57
58     parser = subparsers.add_parser('test', help='SQL from test data')
59     parser.set_defaults(func=process_test)
60
61     parser = subparsers.add_parser('file', help='SQL from a file')
62     parser.add_argument('filename', help='SQL file')
63     parser.set_defaults(func=process_file)
64
65     args = main_parser.parse_args()
66     args.func(args)