From: Oleg Broytman Date: Fri, 5 Aug 2016 16:27:44 +0000 (+0300) Subject: Add a demo script that prints queries from a file X-Git-Tag: 0.0.1~39 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=e801bb92787a0101c8ae43927a2727687034d211;p=sqlconvert.git Add a demo script that prints queries from a file Also add a sample file with queries. --- diff --git a/sample/sample.sql b/sample/sample.sql new file mode 100644 index 0000000..01ac31e --- /dev/null +++ b/sample/sample.sql @@ -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.py b/scripts/print_subtree_from_cl.py similarity index 100% rename from scripts/print_subtree.py rename to scripts/print_subtree_from_cl.py diff --git a/scripts/print_subtree_from_file.py b/scripts/print_subtree_from_file.py new file mode 100755 index 0000000..a701bfe --- /dev/null +++ b/scripts/print_subtree_from_file.py @@ -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])