X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=mysql2sql%2Fprocess_tokens.py;h=7f30a5589b7fad13265b4a9e147fe6308afba545;hb=3151032d036f9a66bad633dc1018395a14f46bac;hp=70dadb23bf6080b7764154e8ef3356763e552e37;hpb=5654b305081bc195027ce00302b38af1fb0e249b;p=sqlconvert.git diff --git a/mysql2sql/process_tokens.py b/mysql2sql/process_tokens.py index 70dadb2..7f30a55 100644 --- a/mysql2sql/process_tokens.py +++ b/mysql2sql/process_tokens.py @@ -1,14 +1,13 @@ -from sqlparse.sql import TokenList -from sqlparse.tokens import Name +from sqlparse.sql import Statement +from sqlparse.tokens import Name, Error, Punctuation, Comment, Newline, \ + Whitespace def requote_names(token_list): """Remove backticks, quote non-lowercase identifiers""" - for token in token_list: - if isinstance(token, TokenList): - requote_names(token) - elif token.ttype is Name: + for token in token_list.flatten(): + if token.ttype is Name: value = token.value if (value[0] == "`") and (value[-1] == "`"): value = value[1:-1] @@ -16,3 +15,36 @@ def requote_names(token_list): token.normalized = token.value = value else: token.normalized = token.value = '"%s"' % value + + +def find_error(token_list): + """Find an error""" + for token in token_list.flatten(): + if token.ttype is Error: + return True + return False + + +class StatementGrouper(object): + def __init__(self): + self.tokens = [] + self.statements = [] + + def get_statements(self): + for statement in self.statements: + yield statement + self.statements = [] + + def process(self, tokens): + for token in tokens: + self.tokens.append(token) + if (token.ttype == Punctuation) and (token.value == ';'): + self.statements.append(Statement(self.tokens)) + self.tokens = [] + + def close(self): + 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