2 from sqlparse import parse
3 from sqlparse.compat import PY3
4 from sqlparse import tokens as T
7 def find_error(token_list):
9 for token in token_list.flatten():
10 if token.ttype is T.Error:
15 def is_newline_statement(statement):
16 for token in statement.tokens[:]:
17 if token.ttype is not T.Newline:
26 class StatementGrouper(object):
27 """Collect lines and reparse until the last statement is complete"""
29 def __init__(self, encoding=None):
32 self.encoding = encoding
34 def process_line(self, line):
35 self.lines.append(line)
38 def process_lines(self):
39 statements = parse(''.join(self.lines), encoding=self.encoding)
40 last_stmt = statements[-1]
41 for i in xrange(len(last_stmt.tokens) - 1, 0, -1):
42 token = last_stmt.tokens[i]
43 if token.ttype in (T.Comment.Single, T.Comment.Multiline,
44 T.Newline, T.Whitespace):
46 if token.ttype is T.Punctuation and token.value == ';':
47 break # The last statement is complete
48 # The last statement is still incomplete - wait for the next line
51 self.statements = statements
53 def get_statements(self):
54 for stmt in self.statements:
61 tokens = parse(''.join(self.lines), encoding=self.encoding)
63 if (token.ttype not in (T.Comment.Single, T.Comment.Multiline,
64 T.Newline, T.Whitespace)):
65 raise ValueError("Incomplete SQL statement: %s" %