#! /usr/bin/env python
from __future__ import print_function
+import argparse
import sys
from sqlconvert.print_tokens import print_tokens
from sqlconvert.process_tokens import find_error, StatementGrouper
-def main(*queries):
+def group_lines(*lines):
grouper = StatementGrouper(encoding='utf-8')
- for query in queries:
- grouper.process_line(query)
+ for line in lines:
+ grouper.process_line(line)
if grouper.statements:
for statement in grouper.get_statements():
print("----------")
print(repr(token))
-def test():
- main(
+def group_test(_args):
+ group_lines(
"SELECT * FROM `mytable`; -- line-comment",
"INSERT into /* inline comment */ mytable VALUES (1, 'one');",
"/*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`) "
)
+def group_sql(args):
+ group_lines(*args.lines)
+
+
+def group_file(args):
+ infile = open(args.filename, 'rt')
+ lines = infile.readlines()
+ infile.close()
+ group_lines(*lines)
+
+
if __name__ == '__main__':
- if len(sys.argv) <= 1:
- sys.exit("Usage: %s [-t | sql_query_string [; sql_query_string ...]]" %
- sys.argv[0])
- if sys.argv[1] == '-t':
- test()
- else:
- queries = sys.argv[1:]
- main(*queries)
+ main_parser = argparse.ArgumentParser(description='Group')
+ subparsers = main_parser.add_subparsers(help='Commands')
+
+ parser = subparsers.add_parser('sql', help='SQL from command line')
+ parser.add_argument('lines', nargs='+', help='SQL lines')
+ parser.set_defaults(func=group_sql)
+
+ parser = subparsers.add_parser('test', help='SQL from test data')
+ parser.set_defaults(func=group_test)
+
+ parser = subparsers.add_parser('file', help='SQL from a file')
+ parser.add_argument('filename', help='SQL file')
+ parser.set_defaults(func=group_file)
+
+ args = main_parser.parse_args()
+ args.func(args)
--- /dev/null
+#! /usr/bin/env python
+from __future__ import print_function
+
+import argparse
+import sys
+from sqlparse import parse
+from sqlconvert.print_tokens import print_tokens
+from sqlconvert.process_tokens import find_error
+
+
+def parse_queries(*queries):
+ for query in queries:
+ for parsed in parse(query, encoding='utf-8'):
+ print("----------")
+ if find_error(parsed):
+ print("ERRORS IN QUERY")
+ print_tokens(parsed, encoding='utf-8')
+ print()
+ parsed._pprint_tree()
+ print("----------")
+
+
+def parse_test(_args):
+ parse_queries(
+ "SELECT * FROM `mytable`; -- line-comment",
+ "INSERT into /* inline comment */ mytable VALUES (1, 'one')",
+ "/*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`) "
+ "VALUES (1, 'one')"
+ )
+
+
+def parse_sql(args):
+ parse_queries(*args.lines)
+
+
+def parse_file(args):
+ infile = open(args.filename, 'rt')
+ lines = infile.readlines()
+ infile.close()
+ parse_queries(*lines)
+
+
+if __name__ == '__main__':
+ main_parser = argparse.ArgumentParser(description='Parse')
+ subparsers = main_parser.add_subparsers(help='Commands')
+
+ parser = subparsers.add_parser('sql', help='SQL from command line')
+ parser.add_argument('lines', nargs='+', help='SQL lines')
+ parser.set_defaults(func=parse_sql)
+
+ parser = subparsers.add_parser('test', help='SQL from test data')
+ parser.set_defaults(func=parse_test)
+
+ parser = subparsers.add_parser('file', help='SQL from a file')
+ parser.add_argument('filename', help='SQL file')
+ parser.set_defaults(func=parse_file)
+
+ args = main_parser.parse_args()
+ args.func(args)
+++ /dev/null
-#! /usr/bin/env python
-from __future__ import print_function
-
-import sys
-from sqlconvert.print_tokens import print_tokens
-from sqlconvert.process_tokens import find_error, StatementGrouper
-
-
-def main(filename):
- grouper = StatementGrouper(encoding='utf-8')
- with open(filename) as infile:
- for line in infile:
- grouper.process_line(line)
- if grouper.statements:
- for statement in grouper.get_statements():
- print("----------")
- if find_error(statement):
- print("ERRORS IN QUERY")
- print_tokens(statement, encoding='utf-8')
- print()
- statement._pprint_tree()
- print("----------")
- tokens = grouper.close()
- if tokens:
- for token in tokens:
- print_tokens(token, encoding='utf-8')
- print(repr(token))
-
-
-if __name__ == '__main__':
- if len(sys.argv) <= 1:
- sys.exit("Usage: %s file" % sys.argv[0])
- main(sys.argv[1])
+++ /dev/null
-#! /usr/bin/env python
-from __future__ import print_function
-
-import sys
-from sqlparse import parse
-from sqlconvert.print_tokens import print_tokens
-from sqlconvert.process_tokens import find_error
-
-
-def main(filename):
- with open(filename) as infile:
- for query in infile:
- for parsed in parse(query, encoding='utf-8'):
- print("----------")
- if find_error(parsed):
- print("ERRORS IN QUERY")
- print_tokens(parsed, encoding='utf-8')
- print()
- parsed._pprint_tree()
- print("----------")
-
-if __name__ == '__main__':
- if len(sys.argv) <= 1:
- sys.exit("Usage: %s file" % sys.argv[0])
- main(sys.argv[1])
+++ /dev/null
-#! /usr/bin/env python
-from __future__ import print_function
-
-import sys
-from sqlparse import parse
-from sqlconvert.print_tokens import print_tokens
-from sqlconvert.process_tokens import find_error
-
-
-def main(*queries):
- for query in queries:
- for parsed in parse(query, encoding='utf-8'):
- print("----------")
- if find_error(parsed):
- print("ERRORS IN QUERY")
- print_tokens(parsed, encoding='utf-8')
- print()
- parsed._pprint_tree()
- print("----------")
-
-
-def test():
- main(
- "SELECT * FROM `mytable`; -- line-comment",
- "INSERT into /* inline comment */ mytable VALUES (1, 'one')",
- "/*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`) "
- "VALUES (1, 'one')"
- )
-
-
-if __name__ == '__main__':
- if len(sys.argv) <= 1:
- sys.exit("Usage: %s [-t | sql_query_string [; sql_query_string ...]]" %
- sys.argv[0])
- if sys.argv[1] == '-t':
- test()
- else:
- queries = sys.argv[1:]
- main(*queries)