From b2d8bf8c3a4e4a28d76c2c5bd9492095b14c0db7 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Thu, 4 Aug 2016 20:01:09 +0300 Subject: [PATCH] Find an error in parse tree --- mysql2sql/process_tokens.py | 13 ++++++++++++- scripts/print_subtree.py | 6 +++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mysql2sql/process_tokens.py b/mysql2sql/process_tokens.py index 70dadb2..2e39a2a 100644 --- a/mysql2sql/process_tokens.py +++ b/mysql2sql/process_tokens.py @@ -1,6 +1,6 @@ from sqlparse.sql import TokenList -from sqlparse.tokens import Name +from sqlparse.tokens import Name, Error def requote_names(token_list): @@ -16,3 +16,14 @@ def requote_names(token_list): token.normalized = token.value = value else: token.normalized = token.value = '"%s"' % value + + +def find_error(token_list): + """Find an error""" + for token in token_list: + if isinstance(token, TokenList): + if find_error(token): + return True + elif token.ttype is Error: + return True + return False diff --git a/scripts/print_subtree.py b/scripts/print_subtree.py index 100fa64..76ac675 100755 --- a/scripts/print_subtree.py +++ b/scripts/print_subtree.py @@ -3,8 +3,8 @@ from __future__ import print_function import sys from sqlparse import parse -from mysql2sql.process_tokens import requote_names from mysql2sql.print_tokens import print_tokens +from mysql2sql.process_tokens import requote_names, find_error def test(): @@ -16,6 +16,8 @@ def test(): ): for parsed in parse(query): print("----------") + if find_error(parsed): + print("ERRORS IN QUERY") requote_names(parsed) print_tokens(parsed) print() @@ -27,6 +29,8 @@ def main(*queries): for query in queries: for parsed in parse(query): print("----------") + if find_error(parsed): + print("ERRORS IN QUERY") requote_names(parsed) print_tokens(parsed) print() -- 2.39.5