from sqlparse.sql import Comment, Function, Identifier, Parenthesis, \
- Statement, Token
+ Statement, Token, Values
from sqlparse import tokens as T
from .process_tokens import escape_strings, is_comment_or_space
token.normalized = token.value = value
-def is_insert(statement):
+def get_DML_type(statement):
for token in statement.tokens:
if is_comment_or_space(token):
continue
- return (token.ttype is T.DML) and (token.normalized == 'INSERT')
+ if (token.ttype is T.DML):
+ return token.normalized
+ break
+ raise ValueError("Not a DML statement")
def split_ext_insert(statement):
expected = 'VALUES'
continue
elif expected == 'VALUES':
+ if isinstance(token, Values):
+ for subtoken in token.tokens:
+ if isinstance(subtoken, Parenthesis):
+ values_tokens.append(subtoken)
+ insert_tokens.append(Token(T.Keyword, 'VALUES'))
+ insert_tokens.append(Token(T.Whitespace, ' '))
+ expected = 'VALUES_OR_SEMICOLON'
+ continue
if (token.ttype is T.Keyword) and (token.normalized == 'VALUES'):
insert_tokens.append(token)
expected = 'VALUES_OR_SEMICOLON'
if i == len(values_tokens) - 1: # Last but one statement
# Insert newlines only between split statements but not after
new_lines = []
- # The statemnt sets `parent` attribute of the every token to self
+ # The statement sets `parent` attribute of the every token to self
# but we don't care.
statement = Statement(insert_tokens + [values] +
end_tokens + new_lines)
yield statement
-def process_statement(statement, quoting_style='sqlite'):
+def process_statement(statement, dbname='sqlite'):
requote_names(statement)
unescape_strings(statement)
remove_directive_tokens(statement)
- escape_strings(statement, quoting_style)
- if is_insert(statement):
+ escape_strings(statement, dbname)
+ try:
+ dml_type = get_DML_type(statement)
+ except ValueError:
+ dml_type = 'UNKNOWN'
+ if dml_type == 'INSERT':
for statement in split_ext_insert(statement):
yield statement
else: