X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=sqlconvert%2Fprocess_tokens.py;h=b1c26022f7ad2adbecaf943fee47cd74c7794a7d;hb=aff066ceefb79333878baaf7c16e0a2206d26c5d;hp=1e74ac9b69a5a416bb6cf8b0418219095b73b357;hpb=159b4e3d7127e719a0dd12013dd54bf05b276ba9;p=sqlconvert.git diff --git a/sqlconvert/process_tokens.py b/sqlconvert/process_tokens.py index 1e74ac9..b1c2602 100644 --- a/sqlconvert/process_tokens.py +++ b/sqlconvert/process_tokens.py @@ -1,31 +1,24 @@ from sqlparse import parse from sqlparse.compat import PY3 -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.flatten(): - if token.ttype is Name: - value = token.value - if (value[0] == "`") and (value[-1] == "`"): - value = value[1:-1] - if value.islower(): - token.normalized = token.value = value - else: - token.normalized = token.value = '"%s"' % value +from sqlparse import tokens as T def find_error(token_list): """Find an error""" for token in token_list.flatten(): - if token.ttype is Error: + if token.ttype is T.Error: return True return False +def is_newline_statement(statement): + for token in statement.tokens[:]: + if token.ttype is not T.Newline: + return False + return True + + if PY3: xrange = range @@ -47,10 +40,10 @@ 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 (Comment.Single, Comment.Multiline, - Newline, Whitespace): + if token.ttype in (T.Comment.Single, T.Comment.Multiline, + T.Newline, T.Whitespace): continue - if token.ttype is Punctuation and token.value == ';': + if token.ttype is T.Punctuation and token.value == ';': break # The last statement is complete # The last statement is still incomplete - wait for the next line return @@ -61,14 +54,15 @@ class StatementGrouper(object): for stmt in self.statements: yield stmt self.statements = [] + raise StopIteration 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 (Comment.Single, Comment.Multiline, - Newline, Whitespace)): + if (token.ttype not in (T.Comment.Single, T.Comment.Multiline, + T.Newline, T.Whitespace)): raise ValueError("Incomplete SQL statement: %s" % tokens) return tokens