2 from sqlparse.sql import Comment
3 from sqlparse import tokens as T
6 def requote_names(token_list):
7 """Remove backticks, quote non-lowercase identifiers"""
8 for token in token_list.flatten():
9 if token.ttype is T.Name:
11 if (value[0] == "`") and (value[-1] == "`"):
14 token.normalized = token.value = value
16 token.normalized = token.value = '"%s"' % value
19 def remove_directives(statement):
20 """Remove /*! directives */ from the first-level"""
22 for token in statement.tokens:
23 if isinstance(token, Comment):
24 subtokens = token.tokens
26 comment = subtokens[0]
27 if comment.ttype is T.Comment.Multiline and \
28 comment.value.startswith('/*!'):
30 new_tokens.append(token)
31 statement.tokens = new_tokens
34 def process_statement(statement):
35 requote_names(statement)
36 remove_directives(statement)