X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=sqlconvert%2Fprocess_tokens.py;h=cb14467ccf958b478d888714ebaa6240e10fede2;hb=71665d24bf836911ce782d7438e67a5163a2b94b;hp=6bd618b6af1ce33f14588d0d6c4f11323bfe37af;hpb=b8225ccc168f04789db1935a90462b5cb0bbd6a6;p=sqlconvert.git diff --git a/sqlconvert/process_tokens.py b/sqlconvert/process_tokens.py index 6bd618b..cb14467 100644 --- a/sqlconvert/process_tokens.py +++ b/sqlconvert/process_tokens.py @@ -1,4 +1,5 @@ +from sqlparse.sql import Comment from sqlobject.converters import sqlrepr from sqlparse import parse from sqlparse.compat import PY3 @@ -13,6 +14,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: @@ -50,8 +57,7 @@ class StatementGrouper(object): 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 @@ -64,15 +70,16 @@ class StatementGrouper(object): for stmt in self.statements: yield stmt self.statements = [] - raise StopIteration + return def close(self): if not self.lines: 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