2 from sqlparse.sql import Comment
3 from sqlparse import tokens as T
6 def _is_directive_token(token):
7 if isinstance(token, Comment):
8 subtokens = token.tokens
10 comment = subtokens[0]
11 if comment.ttype is T.Comment.Multiline and \
12 comment.value.startswith('/*!'):
17 def is_directive_statement(statement):
18 tokens = statement.tokens
19 if not _is_directive_token(tokens[0]):
21 if tokens[-1].ttype is not T.Punctuation or tokens[-1].value != ';':
23 for token in tokens[1:-1]:
24 if token.ttype not in (T.Newline, T.Whitespace):
29 def remove_directives(statement):
30 """Remove /*! directives */ from the first-level"""
32 for token in statement.tokens:
33 if _is_directive_token(token):
35 new_tokens.append(token)
36 statement.tokens = new_tokens
39 def requote_names(token_list):
40 """Remove backticks, quote non-lowercase identifiers"""
41 for token in token_list.flatten():
42 if token.ttype is T.Name:
44 if (value[0] == "`") and (value[-1] == "`"):
47 token.normalized = token.value = value
49 token.normalized = token.value = '"%s"' % value
52 def process_statement(statement):
53 remove_directives(statement)
54 requote_names(statement)