From 3151032d036f9a66bad633dc1018395a14f46bac Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Tue, 23 Aug 2016 22:57:30 +0300 Subject: [PATCH] Allow whitespaces and comments after the last statement --- mysql2sql/process_tokens.py | 10 +++++++--- sample/sample.sql | 2 ++ scripts/group-file.py | 5 ++++- scripts/group-sql.py | 5 ++++- tests/test_stgrouper.py | 2 +- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/mysql2sql/process_tokens.py b/mysql2sql/process_tokens.py index 94879cc..7f30a55 100644 --- a/mysql2sql/process_tokens.py +++ b/mysql2sql/process_tokens.py @@ -1,6 +1,7 @@ from sqlparse.sql import Statement -from sqlparse.tokens import Name, Error, Punctuation +from sqlparse.tokens import Name, Error, Punctuation, Comment, Newline, \ + Whitespace def requote_names(token_list): @@ -42,5 +43,8 @@ class StatementGrouper(object): self.tokens = [] def close(self): - if self.tokens: - raise ValueError("Incomplete SQL statement") + for token in self.tokens: + if (token.ttype not in (Comment.Single, Comment.Multiline, + Newline, Whitespace)): + raise ValueError("Incomplete SQL statement: %s" % self.tokens) + return self.tokens diff --git a/sample/sample.sql b/sample/sample.sql index 01ac31e..fb31ec3 100644 --- a/sample/sample.sql +++ b/sample/sample.sql @@ -2,3 +2,5 @@ SELECT * FROM `mytable`; -- line-comment" INSERT into /* inline comment */ mytable VALUES (1, 'one'); /*! directive*/ INSERT INTO `MyTable` (`Id`, `Name`) VALUES (1, 'one'); + +-- The end diff --git a/scripts/group-file.py b/scripts/group-file.py index 41f9b33..53a11f1 100755 --- a/scripts/group-file.py +++ b/scripts/group-file.py @@ -23,7 +23,10 @@ def main(filename): print() statement._pprint_tree() print("----------") - grouper.close() + tokens = grouper.close() + for token in tokens: + print_tokens(token) + print(repr(token)) if __name__ == '__main__': diff --git a/scripts/group-sql.py b/scripts/group-sql.py index f1f0988..0eab6b4 100755 --- a/scripts/group-sql.py +++ b/scripts/group-sql.py @@ -22,7 +22,10 @@ def main(*queries): print() statement._pprint_tree() print("----------") - grouper.close() + tokens = grouper.close() + for token in tokens: + print_tokens(token) + print(repr(token)) def test(): diff --git a/tests/test_stgrouper.py b/tests/test_stgrouper.py index 0e72ced..36a14c2 100755 --- a/tests/test_stgrouper.py +++ b/tests/test_stgrouper.py @@ -31,7 +31,7 @@ class TestStGrouper(unittest.TestCase): self.assertEqual(query, 'SELECT * FROM "T";') self.assertRaises(StopIteration, next, g) self.assertEqual(len(grouper.statements), 0) - self.assertIsNone(grouper.close()) + self.assertEqual(grouper.close(), []) if __name__ == "__main__": main() -- 2.39.2