]> git.phdru.name Git - sqlconvert.git/blob - scripts/group-sql.py
A trailing space added by distutils
[sqlconvert.git] / scripts / group-sql.py
1 #! /usr/bin/env python
2 from __future__ import print_function
3
4 import sys
5 from sqlparse import parse
6 from mysql2sql.print_tokens import print_tokens
7 from mysql2sql.process_tokens import requote_names, find_error, \
8     StatementGrouper
9
10
11 def main(*queries):
12     grouper = StatementGrouper()
13     for query in queries:
14         grouper.process(parse(query)[0])
15         if grouper.statements:
16             for statement in grouper.get_statements():
17                 print("----------")
18                 if find_error(statement):
19                     print("ERRORS IN QUERY")
20                 requote_names(statement)
21                 print_tokens(statement)
22                 print()
23                 statement._pprint_tree()
24             print("----------")
25     tokens = grouper.close()
26     for token in tokens:
27         print_tokens(token)
28         print(repr(token))
29
30
31 def test():
32     main(
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 if __name__ == '__main__':
41     if len(sys.argv) <= 1:
42         sys.exit("Usage: %s [-t | sql_query_string [; sql_query_string ...]]" %
43                  sys.argv[0])
44     if sys.argv[1] == '-t':
45         test()
46     else:
47         queries = sys.argv[1:]
48         main(*queries)