2 from sqlparse import parse
3 from sqlparse.compat import PY3
4 from sqlparse.tokens import Name, Error, Punctuation, Comment, Newline, \
8 def requote_names(token_list):
9 """Remove backticks, quote non-lowercase identifiers"""
10 for token in token_list.flatten():
11 if token.ttype is Name:
13 if (value[0] == "`") and (value[-1] == "`"):
16 token.normalized = token.value = value
18 token.normalized = token.value = '"%s"' % value
21 def find_error(token_list):
23 for token in token_list.flatten():
24 if token.ttype is Error:
33 class StatementGrouper(object):
34 """Collect lines and reparse until the last statement is complete"""
36 def __init__(self, encoding=None):
39 self.encoding = encoding
41 def process_line(self, line):
42 self.lines.append(line)
45 def process_lines(self):
46 statements = parse(''.join(self.lines), encoding=self.encoding)
47 last_stmt = statements[-1]
48 for i in xrange(len(last_stmt.tokens) - 1, 0, -1):
49 token = last_stmt.tokens[i]
50 if token.ttype in (Comment.Single, Comment.Multiline,
53 if token.ttype is Punctuation and token.value == ';':
54 break # The last statement is complete
55 # The last statement is still incomplete - wait for the next line
58 self.statements = statements
60 def get_statements(self):
61 for stmt in self.statements:
68 tokens = parse(''.join(self.lines), encoding=self.encoding)
70 if (token.ttype not in (Comment.Single, Comment.Multiline,
71 Newline, Whitespace)):
72 raise ValueError("Incomplete SQL statement: %s" %