]> git.phdru.name Git - sqlconvert.git/blob - demo/group-sql.py
Remove processing from demo scripts
[sqlconvert.git] / demo / group-sql.py
1 #! /usr/bin/env python
2 from __future__ import print_function
3
4 import sys
5 from sqlconvert.print_tokens import print_tokens
6 from sqlconvert.process_tokens import find_error, StatementGrouper
7
8
9 def main(*queries):
10     grouper = StatementGrouper(encoding='utf-8')
11     for query in queries:
12         grouper.process_line(query)
13         if grouper.statements:
14             for statement in grouper.get_statements():
15                 print("----------")
16                 if find_error(statement):
17                     print("ERRORS IN QUERY")
18                 print_tokens(statement, encoding='utf-8')
19                 print()
20                 statement._pprint_tree()
21             print("----------")
22     tokens = grouper.close()
23     if tokens:
24         for token in tokens:
25             print_tokens(token, encoding='utf-8')
26             print(repr(token))
27
28
29 def test():
30     main(
31         "SELECT * FROM `mytable`; -- line-comment",
32         "INSERT into /* inline comment */ mytable VALUES (1, 'one');",
33         "/*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`) "
34         "VALUES (1, 'one');"
35     )
36
37
38 if __name__ == '__main__':
39     if len(sys.argv) <= 1:
40         sys.exit("Usage: %s [-t | sql_query_string [; sql_query_string ...]]" %
41                  sys.argv[0])
42     if sys.argv[1] == '-t':
43         test()
44     else:
45         queries = sys.argv[1:]
46         main(*queries)