--- /dev/null
+
+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 ';'
--- /dev/null
+
+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
--- /dev/null
+#! /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)