]> git.phdru.name Git - sqlconvert.git/blobdiff - sqlconvert/process_tokens.py
Build(GHActions): Use `checkout@v4` instead of outdated `v2`
[sqlconvert.git] / sqlconvert / process_tokens.py
index 8cf1fe5e73ffd005a717115fdbf8c114ffe24783..886f36d46c02d1f824486593f3263bd06b3cb3b3 100644 (file)
@@ -1,9 +1,14 @@
 
+from sqlparse.sql import Comment
 from sqlobject.converters import sqlrepr
 from sqlparse import parse
-from sqlparse.compat import PY3
 from sqlparse import tokens as T
 
+try:
+    xrange
+except NameError:
+    xrange = range
+
 
 def find_error(token_list):
     """Find an error"""
@@ -13,6 +18,12 @@ def find_error(token_list):
     return False
 
 
+def is_comment_or_space(token):
+    return isinstance(token, Comment) or \
+        token.ttype in (T.Comment, T.Comment.Single, T.Comment.Multiline,
+                        T.Newline, T.Whitespace)
+
+
 def is_newline_statement(statement):
     for token in statement.tokens[:]:
         if token.ttype is not T.Newline:
@@ -29,10 +40,6 @@ def escape_strings(token_list, dbname):
             token.normalized = token.value = value
 
 
-if PY3:
-    xrange = range
-
-
 class StatementGrouper(object):
     """Collect lines and reparse until the last statement is complete"""
 
@@ -47,11 +54,12 @@ class StatementGrouper(object):
 
     def process_lines(self):
         statements = parse(''.join(self.lines), encoding=self.encoding)
+        if not statements:
+            return
         last_stmt = statements[-1]
         for i in xrange(len(last_stmt.tokens) - 1, 0, -1):
             token = last_stmt.tokens[i]
-            if token.ttype in (T.Comment.Single, T.Comment.Multiline,
-                               T.Newline, T.Whitespace):
+            if is_comment_or_space(token):
                 continue
             if token.ttype is T.Punctuation and token.value == ';':
                 break  # The last statement is complete
@@ -71,8 +79,9 @@ class StatementGrouper(object):
             return
         tokens = parse(''.join(self.lines), encoding=self.encoding)
         for token in tokens:
-            if (token.ttype not in (T.Comment.Single, T.Comment.Multiline,
-                                    T.Newline, T.Whitespace)):
+            if not is_comment_or_space(token):
                 raise ValueError("Incomplete SQL statement: %s" %
                                  tokens)
+        self.lines = []
+        self.statements = []
         return tokens