]> git.phdru.name Git - sqlconvert.git/commitdiff
Join demo scripts; add argparse parameters parsing
authorOleg Broytman <phd@phdru.name>
Mon, 5 Sep 2016 20:57:41 +0000 (23:57 +0300)
committerOleg Broytman <phd@phdru.name>
Mon, 5 Sep 2016 21:25:19 +0000 (00:25 +0300)
Join group-{file,sql}.py into demo-group.py
     parse-{file,sql}.py into demo-parse.py.

demo/demo-group.py [moved from demo/group-sql.py with 51% similarity]
demo/demo-parse.py [new file with mode: 0755]
demo/group-file.py [deleted file]
demo/parse-file.py [deleted file]
demo/parse-sql.py [deleted file]

similarity index 51%
rename from demo/group-sql.py
rename to demo/demo-group.py
index 11f1f8cd002869745cd4b980856180016dceb249..267c2ce6bc758901d81c96ee3fb81d58f3374e94 100755 (executable)
@@ -1,15 +1,16 @@
 #! /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("----------")
@@ -26,8 +27,8 @@ def main(*queries):
             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`) "
@@ -35,12 +36,31 @@ def test():
     )
 
 
+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)
diff --git a/demo/demo-parse.py b/demo/demo-parse.py
new file mode 100755 (executable)
index 0000000..7307934
--- /dev/null
@@ -0,0 +1,59 @@
+#! /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)
diff --git a/demo/group-file.py b/demo/group-file.py
deleted file mode 100755 (executable)
index 87df331..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-#! /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])
diff --git a/demo/parse-file.py b/demo/parse-file.py
deleted file mode 100755 (executable)
index 55a7829..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-#! /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])
diff --git a/demo/parse-sql.py b/demo/parse-sql.py
deleted file mode 100755 (executable)
index abf38ba..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-#! /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)