]> git.phdru.name Git - sqlconvert.git/commitdiff
Add a demo script that prints queries from a file
authorOleg Broytman <phd@phdru.name>
Fri, 5 Aug 2016 16:27:44 +0000 (19:27 +0300)
committerOleg Broytman <phd@phdru.name>
Fri, 5 Aug 2016 16:27:44 +0000 (19:27 +0300)
Also add a sample file with queries.

sample/sample.sql [new file with mode: 0644]
scripts/print_subtree_from_cl.py [moved from scripts/print_subtree.py with 100% similarity]
scripts/print_subtree_from_file.py [new file with mode: 0755]

diff --git a/sample/sample.sql b/sample/sample.sql
new file mode 100644 (file)
index 0000000..01ac31e
--- /dev/null
@@ -0,0 +1,4 @@
+SELECT * FROM `mytable`; -- line-comment"
+INSERT into /* inline comment */ mytable VALUES (1, 'one');
+/*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`)
+VALUES (1, 'one');
diff --git a/scripts/print_subtree_from_file.py b/scripts/print_subtree_from_file.py
new file mode 100755 (executable)
index 0000000..a701bfe
--- /dev/null
@@ -0,0 +1,26 @@
+#! /usr/bin/env python
+from __future__ import print_function
+
+import sys
+from sqlparse import parse
+from mysql2sql.print_tokens import print_tokens
+from mysql2sql.process_tokens import requote_names, find_error
+
+
+def main(filename):
+    with open(filename) as infile:
+        for query in infile:
+            for parsed in parse(query):
+                print("----------")
+                if find_error(parsed):
+                    print("ERRORS IN QUERY")
+                requote_names(parsed)
+                print_tokens(parsed)
+                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])