From e801bb92787a0101c8ae43927a2727687034d211 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 5 Aug 2016 19:27:44 +0300 Subject: [PATCH] Add a demo script that prints queries from a file Also add a sample file with queries. --- sample/sample.sql | 4 +++ ...nt_subtree.py => print_subtree_from_cl.py} | 0 scripts/print_subtree_from_file.py | 26 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 sample/sample.sql rename scripts/{print_subtree.py => print_subtree_from_cl.py} (100%) create mode 100755 scripts/print_subtree_from_file.py 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]) -- 2.39.5