]> git.phdru.name Git - sqlconvert.git/commitdiff
Allow whitespaces and comments after the last statement
authorOleg Broytman <phd@phdru.name>
Tue, 23 Aug 2016 19:57:30 +0000 (22:57 +0300)
committerOleg Broytman <phd@phdru.name>
Wed, 24 Aug 2016 14:40:45 +0000 (17:40 +0300)
mysql2sql/process_tokens.py
sample/sample.sql
scripts/group-file.py
scripts/group-sql.py
tests/test_stgrouper.py

index 94879cc555ea2bbbb399ecb655657dfead8ceab0..7f30a5589b7fad13265b4a9e147fe6308afba545 100644 (file)
@@ -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
index 01ac31e2f70284983e3b804e3133340d0715d20e..fb31ec364763533aef6d857ce53b63c6d229fd87 100644 (file)
@@ -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
index 41f9b3318b47248100e892db2d4362201961594f..53a11f10fb4362b5df18da0a9174a3b5f613491f 100755 (executable)
@@ -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__':
index f1f09886ea6be49b31632f767bf640ed25b8b141..0eab6b4042253cba3a629df455841924e2935c22 100755 (executable)
@@ -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():
index 0e72ced759e47ecad9130d1012c05d84a53ba1e4..36a14c2c6350b7ff76b86e63716ac7e76ed7e711 100755 (executable)
@@ -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()