]> git.phdru.name Git - sqlconvert.git/commitdiff
Add some initial code and a small demo script
authorOleg Broytman <phd@phdru.name>
Sat, 23 Jul 2016 03:06:19 +0000 (06:06 +0300)
committerOleg Broytman <phd@phdru.name>
Sat, 23 Jul 2016 03:06:19 +0000 (06:06 +0300)
mysql2sql/print_tokens.py [new file with mode: 0644]
mysql2sql/process_tokens.py [new file with mode: 0644]
scripts/print_subtree.py [new file with mode: 0755]

diff --git a/mysql2sql/print_tokens.py b/mysql2sql/print_tokens.py
new file mode 100644 (file)
index 0000000..05af1c1
--- /dev/null
@@ -0,0 +1,20 @@
+
+import sys
+from sqlparse.sql import TokenList
+
+
+def print_subtree(token_list, ident=0):
+    for token in token_list:
+        print " "*ident, repr(token)
+        if isinstance(token, TokenList):
+            print_subtree(token, ident+4)
+
+
+def print_tokens(token_list, level=0):
+    for token in token_list:
+        if not isinstance(token, TokenList):
+            sys.stdout.write(token.normalized)
+        if isinstance(token, TokenList):
+            print_tokens(token, level+1)
+    if level == 0:
+        print ';'
diff --git a/mysql2sql/process_tokens.py b/mysql2sql/process_tokens.py
new file mode 100644 (file)
index 0000000..c395b37
--- /dev/null
@@ -0,0 +1,18 @@
+
+from sqlparse.sql import TokenList
+from sqlparse.tokens import Name
+
+
+def requote_names(token_list):
+    """Remove backticks, quote non-lowercase identifiers"""
+    for token in token_list:
+        if isinstance(token, TokenList):
+            requote_names(token)
+        else:
+            if token.ttype is Name:
+                value = token.value
+                if (value[0] == "`") and (value[-1] == "`"):
+                    value = value[1:-1]
+                    token.normalized = token.value = value
+                if not value.islower():
+                    token.normalized = token.value = '"%s"' % value
diff --git a/scripts/print_subtree.py b/scripts/print_subtree.py
new file mode 100755 (executable)
index 0000000..da5eae8
--- /dev/null
@@ -0,0 +1,37 @@
+#! /usr/bin/env python
+
+import sys
+from sqlparse import parse
+from mysql2sql.process_tokens import requote_names
+from mysql2sql.print_tokens import print_tokens, print_subtree
+
+
+def test():
+    for query in (
+        "SELECT * FROM `mytable`; -- line-comment",
+        "INSERT into /* inline comment */ mytable VALUES (1, 'one')",
+        "/*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`) "
+        "VALUES (1, 'one')"
+    ):
+        print "----------"
+        for parsed in parse(query):
+            requote_names(parsed)
+            print_tokens(parsed)
+            print_subtree(parsed)
+    print "----------"
+
+
+def main(query):
+    parsed = parse(query)[0]
+    requote_names(parsed)
+    print_tokens(parsed)
+    print_subtree(parsed)
+
+if __name__ == '__main__':
+    if len(sys.argv) != 2:
+        sys.exit("Usage: %s [-t | sql_query_string]" % sys.argv[0])
+    if sys.argv[1] == '-t':
+        test()
+    else:
+        query = sys.argv[1]
+        main(query)