From: Oleg Broytman Date: Sat, 23 Jul 2016 03:06:19 +0000 (+0300) Subject: Add some initial code and a small demo script X-Git-Tag: 0.0.1~58 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=3289238688d9c3dfefccf143d21f6c406faad9e4;p=sqlconvert.git Add some initial code and a small demo script --- diff --git a/mysql2sql/print_tokens.py b/mysql2sql/print_tokens.py new file mode 100644 index 0000000..05af1c1 --- /dev/null +++ b/mysql2sql/print_tokens.py @@ -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 index 0000000..c395b37 --- /dev/null +++ b/mysql2sql/process_tokens.py @@ -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 index 0000000..da5eae8 --- /dev/null +++ b/scripts/print_subtree.py @@ -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)