From 6f433a4531f0e301f3534de7f0278afbcf79d50e Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Thu, 29 Sep 2016 02:47:30 +0300 Subject: [PATCH] Make process_statement() a generator Prepare for process_statement() to yield a list of statements: extended INSERT will be split into a list of plain INSERTs. --- demo/demo-process.py | 8 ++++---- scripts/mysql2sql | 6 +++--- sqlconvert/process_mysql.py | 2 ++ tests/test_process_tokens.py | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/demo/demo-process.py b/demo/demo-process.py index 2c93338..a81f93e 100755 --- a/demo/demo-process.py +++ b/demo/demo-process.py @@ -15,10 +15,10 @@ def process_lines(*lines): print("----- -----") if find_error(statement): print("ERRORS IN QUERY") - process_statement(statement) - print_tokens(statement, encoding='utf-8') - print() - statement._pprint_tree() + for statement in process_statement(statement): + print_tokens(statement, encoding='utf-8') + print() + statement._pprint_tree() print("-----/-----") tokens = grouper.close() if tokens: diff --git a/scripts/mysql2sql b/scripts/mysql2sql index f06234a..3766c97 100755 --- a/scripts/mysql2sql +++ b/scripts/mysql2sql @@ -56,9 +56,9 @@ def main(infile, encoding, outfile, output_encoding, use_pbar, quoting_style): got_directive = is_directive_statement(statement) if got_directive: continue - process_statement(statement, quoting_style) - print_tokens(statement, outfile=outfile, - encoding=output_encoding) + for statement in process_statement(statement, quoting_style): + print_tokens(statement, outfile=outfile, + encoding=output_encoding) tokens = grouper.close() if tokens: for token in tokens: diff --git a/sqlconvert/process_mysql.py b/sqlconvert/process_mysql.py index 91ef98d..9bfef92 100644 --- a/sqlconvert/process_mysql.py +++ b/sqlconvert/process_mysql.py @@ -75,3 +75,5 @@ def process_statement(statement, quoting_style='sqlite'): requote_names(statement) unescape_strings(statement) escape_strings(statement, quoting_style) + yield statement + return diff --git a/tests/test_process_tokens.py b/tests/test_process_tokens.py index e561667..2d30dc9 100644 --- a/tests/test_process_tokens.py +++ b/tests/test_process_tokens.py @@ -69,8 +69,8 @@ def test_escape_string_sqlite(): def test_process(): parsed = parse("select /*! test */ * from /* test */ `T`")[0] - process_statement(parsed) - query = tlist2str(parsed) + statement = next(process_statement(parsed)) + query = tlist2str(statement) assert query == u'SELECT * FROM /* test */ "T"' -- 2.39.2