本文整理汇总了Python中sqlparse.sql.Identifier方法的典型用法代码示例。如果您正苦于以下问题:Python sql.Identifier方法的具体用法?Python sql.Identifier怎么用?Python sql.Identifier使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlparse.sql
的用法示例。
在下文中一共展示了sql.Identifier方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_partial_identifier
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def parse_partial_identifier(word):
"""Attempt to parse a (partially typed) word as an identifier
word may include a schema qualification, like `schema_name.partial_name`
or `schema_name.` There may also be unclosed quotation marks, like
`"schema`, or `schema."partial_name`
:param word: string representing a (partially complete) identifier
:return: sqlparse.sql.Identifier, or None
"""
p = sqlparse.parse(word)[0]
n_tok = len(p.tokens)
if n_tok == 1 and isinstance(p.tokens[0], Identifier):
return p.tokens[0]
elif p.token_next_by(m=(Error, '"'))[1]:
# An unmatched double quote, e.g. '"foo', 'foo."', or 'foo."bar'
# Close the double quote, then reparse
return parse_partial_identifier(word + '"')
else:
return None
示例2: sql_literal_to_model
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def sql_literal_to_model(tok, m=M):
"""
:param m: the source model to "append" the literal to.
defaults to M - the sqlitis models module (which means a fresh model
is created)
:return: the resulting model
"""
def is_string_literal(tok):
text = tok.normalized
return all([text.startswith('"'), text.endswith('"')])
# sqlparse treats string literals as identifiers
if type(tok) is S.Identifier and is_string_literal(tok):
return m.Field(tok.normalized, literal=True)
elif type(tok) is S.Identifier:
return m.Field(tok.normalized)
elif tok.ttype is T.Comparison:
return m.Op(tok.normalized)
elif tok.ttype in [T.Literal, T.String, T.Number, T.Number.Integer, T.Number.Float]:
return m.Field(tok.normalized, literal=True)
return None
示例3: _extract_table_identifiers
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def _extract_table_identifiers(token_stream):
for item in token_stream:
if isinstance(item, IdentifierList):
for ident in item.get_identifiers():
try:
alias = ident.get_alias()
schema_name = ident.get_parent_name()
real_name = ident.get_real_name()
except AttributeError:
continue
if real_name:
yield Reference(schema_name, real_name,
alias, _identifier_is_function(ident))
elif isinstance(item, Identifier):
yield Reference(item.get_parent_name(), item.get_real_name(),
item.get_alias(), _identifier_is_function(item))
elif isinstance(item, Function):
yield Reference(item.get_parent_name(), item.get_real_name(),
item.get_alias(), _identifier_is_function(item))
示例4: group_period
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def group_period(tlist):
def match(token):
return token.match(T.Punctuation, '.')
def valid_prev(token):
sqlcls = sql.SquareBrackets, sql.Identifier
ttypes = T.Name, T.String.Symbol
return imt(token, i=sqlcls, t=ttypes)
def valid_next(token):
# issue261, allow invalid next token
return True
def post(tlist, pidx, tidx, nidx):
# next_ validation is being performed here. issue261
sqlcls = sql.SquareBrackets, sql.Function
ttypes = T.Name, T.String.Symbol, T.Wildcard
next_ = tlist[nidx] if nidx is not None else None
valid_next = imt(next_, i=sqlcls, t=ttypes)
return (pidx, nidx) if valid_next else (pidx, tidx)
_group(tlist, sql.Identifier, match, valid_prev, valid_next, post)
示例5: group_comparison
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def group_comparison(tlist):
sqlcls = (sql.Parenthesis, sql.Function, sql.Identifier,
sql.Operation)
ttypes = T_NUMERICAL + T_STRING + T_NAME
def match(token):
return token.ttype == T.Operator.Comparison
def valid(token):
if imt(token, t=ttypes, i=sqlcls):
return True
elif token and token.is_keyword and token.normalized == 'NULL':
return True
else:
return False
def post(tlist, pidx, tidx, nidx):
return pidx, nidx
valid_prev = valid_next = valid
_group(tlist, sql.Comparison, match,
valid_prev, valid_next, post, extend=False)
示例6: group_arrays
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def group_arrays(tlist):
sqlcls = sql.SquareBrackets, sql.Identifier, sql.Function
ttypes = T.Name, T.String.Symbol
def match(token):
return isinstance(token, sql.SquareBrackets)
def valid_prev(token):
return imt(token, i=sqlcls, t=ttypes)
def valid_next(token):
return True
def post(tlist, pidx, tidx, nidx):
return pidx, tidx
_group(tlist, sql.Identifier, match,
valid_prev, valid_next, post, extend=True, recurse=False)
示例7: group_operator
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def group_operator(tlist):
ttypes = T_NUMERICAL + T_STRING + T_NAME
sqlcls = (sql.SquareBrackets, sql.Parenthesis, sql.Function,
sql.Identifier, sql.Operation)
def match(token):
return imt(token, t=(T.Operator, T.Wildcard))
def valid(token):
return imt(token, i=sqlcls, t=ttypes)
def post(tlist, pidx, tidx, nidx):
tlist[tidx].ttype = T.Operator
return pidx, nidx
valid_prev = valid_next = valid
_group(tlist, sql.Operation, match,
valid_prev, valid_next, post, extend=False)
示例8: group_identifier_list
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def group_identifier_list(tlist):
m_role = T.Keyword, ('null', 'role')
sqlcls = (sql.Function, sql.Case, sql.Identifier, sql.Comparison,
sql.IdentifierList, sql.Operation)
ttypes = (T_NUMERICAL + T_STRING + T_NAME +
(T.Keyword, T.Comment, T.Wildcard))
def match(token):
return token.match(T.Punctuation, ',')
def valid(token):
return imt(token, i=sqlcls, m=m_role, t=ttypes)
def post(tlist, pidx, tidx, nidx):
return pidx, nidx
valid_prev = valid_next = valid
_group(tlist, sql.IdentifierList, match,
valid_prev, valid_next, post, extend=True)
示例9: group_aliased
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def group_aliased(tlist):
clss = (sql.Identifier, sql.Function, sql.Case)
[group_aliased(sgroup) for sgroup in tlist.get_sublists()
if not isinstance(sgroup, clss)]
idx = 0
token = tlist.token_next_by_instance(idx, clss)
while token:
next_ = tlist.token_next(tlist.token_index(token))
if next_ is not None and isinstance(next_, clss):
if not next_.value.upper().startswith('VARCHAR'):
grp = tlist.tokens_between(token, next_)[1:]
token.tokens.extend(grp)
for t in grp:
tlist.tokens.remove(t)
idx = tlist.token_index(token) + 1
token = tlist.token_next_by_instance(idx, clss)
示例10: pop
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def pop(self):
next_val = self.peek()
self.index += 1
# We need to handle three cases here where the next_val could be:
# 1. <table_name> ('business')
# 2. <database_name>.<table_name> ('yelp.business')
# 3. <database_name>.<table_name> <extended_query>
# ('yelp.business change col_one col_two')
# In all the cases we should return a token consisting of only the table
# name or if the database name is present then the database name and the
# table name. Case #3 occurs because SQLParse incorrectly parses certain
# queries.
if isinstance(next_val, Identifier):
tokens = next_val.tokens
if len(tokens) > 1 and tokens[1].value == '.':
str_token = "{db_name}{punctuation}{table_name}".format(
db_name=tokens[0].value,
punctuation=tokens[1].value,
table_name=tokens[2].value
)
return TK(Token.Name, str_token)
else:
return next_val.token_first()
return next_val
示例11: group_aliased
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def group_aliased(tlist):
clss = (sql.Identifier, sql.Function, sql.Case)
[group_aliased(sgroup) for sgroup in tlist.get_sublists()
if not isinstance(sgroup, clss)]
idx = 0
token = tlist.token_next_by_instance(idx, clss)
while token:
next_ = tlist.token_next(tlist.token_index(token))
if next_ is not None and isinstance(next_, clss):
# for jython str.upper()
# if not next_.value.upper().startswith('VARCHAR'):
text = next_.value
if sys.version_info[0] < 3 and isinstance(text, str):
text = text.decode('utf-8').upper().encode('utf-8')
if not text.startswith('VARCHAR'):
grp = tlist.tokens_between(token, next_)[1:]
token.tokens.extend(grp)
for t in grp:
tlist.tokens.remove(t)
idx = tlist.token_index(token) + 1
token = tlist.token_next_by_instance(idx, clss)
示例12: __custom_process_parenthesis_order
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def __custom_process_parenthesis_order(self, parenthesis):
open_punc = parenthesis.token_next_match(0, T.Punctuation, '(')
close_punc = parenthesis.token_next_match(open_punc, T.Punctuation, ')')
self.indent += 2
parenthesis.insert_after(open_punc, self.nl())
for token in parenthesis.tokens_between(open_punc, close_punc)[1:-1]:
if isinstance(token, Phrase):
parenthesis.insert_before(token, self.nl())
self._process_phrase(token, kwds=False)
parenthesis.insert_after(token, self.nl_with_indent(1))
elif isinstance(token, sql.Identifier) and len(token.tokens)==1 and isinstance(token.tokens[0], Phrase):
# 中がPhraseのIdentifier
child_token = token.tokens[0]
parenthesis.insert_before(token, self.nl())
self._process_phrase(child_token, kwds=False)
parenthesis.insert_after(token, self.nl_with_indent(1))
elif token.is_group():
self._process(token)
self.indent -= 1
parenthesis.insert_before(close_punc, self.nl())
self.indent -= 1
示例13: init_group_token
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def init_group_token(self, token):
tokens = token.get_target_tokens()
with_token = tokens[0]
start_prev = with_token
end = None
for tkn in tokens[1:]:
if tu.is_comma(tkn):
start = tu.token_next_enable(token, start_prev)
token.group_tokens(sql.Identifier, token.tokens_between(start, end))
start_prev = tkn
continue
end = tkn
start = tu.token_next_enable(token, with_token)
end = tu.token_prev_enable(token)
token.group_tokens(sql.IdentifierList, token.tokens_between(start, end))
示例14: _validate_comparison
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def _validate_comparison(cls, tokens):
base_error_string = "Invalid comparison clause"
if len(tokens) != 3:
raise MlflowException("{}. Expected 3 tokens found {}".format(base_error_string,
len(tokens)),
error_code=INVALID_PARAMETER_VALUE)
if not isinstance(tokens[0], Identifier):
raise MlflowException("{}. Expected 'Identifier' found '{}'".format(base_error_string,
str(tokens[0])),
error_code=INVALID_PARAMETER_VALUE)
if not isinstance(tokens[1], Token) and tokens[1].ttype != TokenType.Operator.Comparison:
raise MlflowException("{}. Expected comparison found '{}'".format(base_error_string,
str(tokens[1])),
error_code=INVALID_PARAMETER_VALUE)
if not isinstance(tokens[2], Token) and \
(tokens[2].ttype not in cls.STRING_VALUE_TYPES.union(cls.NUMERIC_VALUE_TYPES) or
isinstance(tokens[2], Identifier)):
raise MlflowException("{}. Expected value token found '{}'".format(base_error_string,
str(tokens[2])),
error_code=INVALID_PARAMETER_VALUE)
示例15: _validate_order_by_and_generate_token
# 需要导入模块: from sqlparse import sql [as 别名]
# 或者: from sqlparse.sql import Identifier [as 别名]
def _validate_order_by_and_generate_token(cls, order_by):
try:
parsed = sqlparse.parse(order_by)
except Exception:
raise MlflowException(f"Error on parsing order_by clause '{order_by}'",
error_code=INVALID_PARAMETER_VALUE)
if len(parsed) != 1 or not isinstance(parsed[0], Statement):
raise MlflowException(f"Invalid order_by clause '{order_by}'. Could not be parsed.",
error_code=INVALID_PARAMETER_VALUE)
statement = parsed[0]
if len(statement.tokens) == 1 and isinstance(statement[0], Identifier):
token_value = statement.tokens[0].value
elif len(statement.tokens) == 1 and \
statement.tokens[0].match(ttype=TokenType.Keyword,
values=[cls.ORDER_BY_KEY_TIMESTAMP]):
token_value = cls.ORDER_BY_KEY_TIMESTAMP
elif statement.tokens[0].match(ttype=TokenType.Keyword,
values=[cls.ORDER_BY_KEY_TIMESTAMP])\
and all([token.is_whitespace for token in statement.tokens[1:-1]])\
and statement.tokens[-1].ttype == TokenType.Keyword.Order:
token_value = cls.ORDER_BY_KEY_TIMESTAMP + ' ' + statement.tokens[-1].value
else:
raise MlflowException(f"Invalid order_by clause '{order_by}'. Could not be parsed.",
error_code=INVALID_PARAMETER_VALUE)
return token_value