本文整理匯總了Python中sqlparse.sql.Comparison方法的典型用法代碼示例。如果您正苦於以下問題:Python sql.Comparison方法的具體用法?Python sql.Comparison怎麽用?Python sql.Comparison使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sqlparse.sql
的用法示例。
在下文中一共展示了sql.Comparison方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sql_literal_to_model
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [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
示例2: build_comparison
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [as 別名]
def build_comparison(token):
assert type(token) is S.Comparison
m = M.Comparison()
for tok in remove_whitespace(token.tokens):
LOG.debug(" %s %s", tok, type(tok))
if type(tok) is S.Parenthesis:
subtokens = remove_whitespace(tok.tokens)
subquery = tokens_to_sqla(subtokens[1:-1])
if not m.left:
m.left = subquery
else:
m.right = subquery
else:
m = sql_literal_to_model(tok, m)
if not m:
raise Exception("[BUG] Failed to convert %s to model" % tok)
return m
示例3: group_comparison
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [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)
示例4: group_identifier_list
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [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)
示例5: _validate_comparison
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [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)
示例6: group_comparison
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [as 別名]
def group_comparison(tlist):
def _parts_valid(token):
return (token.ttype in (T.String.Symbol, T.String.Single,
T.Name, T.Number, T.Number.Float,
T.Number.Integer, T.Literal,
T.Literal.Number.Integer, T.Name.Placeholder)
or isinstance(token, (sql.Identifier, sql.Parenthesis))
or (token.ttype is T.Keyword
and token.value.upper() in ['NULL', ]))
_group_left_right(tlist, T.Operator.Comparison, None, sql.Comparison,
check_left=_parts_valid, check_right=_parts_valid)
示例7: test_comparison_exclude
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [as 別名]
def test_comparison_exclude(self):
# make sure operators are not handled too lazy
p = sqlparse.parse('(=)')[0]
self.assert_(isinstance(p.tokens[0], sql.Parenthesis))
self.assert_(not isinstance(p.tokens[0].tokens[1], sql.Comparison))
p = sqlparse.parse('(a=1)')[0]
self.assert_(isinstance(p.tokens[0].tokens[1], sql.Comparison))
p = sqlparse.parse('(a>=1)')[0]
self.assert_(isinstance(p.tokens[0].tokens[1], sql.Comparison))
示例8: test_comparison_with_keywords
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [as 別名]
def test_comparison_with_keywords(): # issue90
# in fact these are assignments, but for now we don't distinguish them
p = sqlparse.parse('foo = NULL')[0]
assert len(p.tokens) == 1
assert isinstance(p.tokens[0], sql.Comparison)
assert len(p.tokens[0].tokens) == 5
assert p.tokens[0].left.value == 'foo'
assert p.tokens[0].right.value == 'NULL'
# make sure it's case-insensitive
p = sqlparse.parse('foo = null')[0]
assert len(p.tokens) == 1
assert isinstance(p.tokens[0], sql.Comparison)
示例9: test_comparison_with_floats
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [as 別名]
def test_comparison_with_floats(): # issue145
p = sqlparse.parse('foo = 25.5')[0]
assert len(p.tokens) == 1
assert isinstance(p.tokens[0], sql.Comparison)
assert len(p.tokens[0].tokens) == 5
assert p.tokens[0].left.value == 'foo'
assert p.tokens[0].right.value == '25.5'
示例10: test_comparison_with_strings
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [as 別名]
def test_comparison_with_strings(): # issue148
p = sqlparse.parse('foo = \'bar\'')[0]
assert len(p.tokens) == 1
assert isinstance(p.tokens[0], sql.Comparison)
assert p.tokens[0].right.value == '\'bar\''
assert p.tokens[0].right.ttype == T.String.Single
示例11: is_comparison
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [as 別名]
def is_comparison(token):
"""
比較演算判定
"""
return isinstance(token, sql.Comparison)
示例12: _invalid_statement_token
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [as 別名]
def _invalid_statement_token(cls, token):
if isinstance(token, Comparison):
return False
elif token.is_whitespace:
return False
elif token.match(ttype=TokenType.Keyword, values=["AND"]):
return False
else:
return True
示例13: _process_statement
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [as 別名]
def _process_statement(cls, statement):
# check validity
invalids = list(filter(cls._invalid_statement_token, statement.tokens))
if len(invalids) > 0:
invalid_clauses = ", ".join("'%s'" % token for token in invalids)
raise MlflowException("Invalid clause(s) in filter string: %s" % invalid_clauses,
error_code=INVALID_PARAMETER_VALUE)
return [cls._get_comparison(si) for si in statement.tokens if isinstance(si, Comparison)]
示例14: _parse_filter_for_model_registry
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [as 別名]
def _parse_filter_for_model_registry(cls, filter_string, valid_search_keys):
if not filter_string or filter_string == "":
return []
expected = "Expected search filter with single comparison operator. e.g. name='myModelName'"
try:
parsed = sqlparse.parse(filter_string)
except Exception:
raise MlflowException("Error while parsing filter '%s'. %s" % (filter_string, expected),
error_code=INVALID_PARAMETER_VALUE)
if len(parsed) == 0 or not isinstance(parsed[0], Statement):
raise MlflowException("Invalid filter '%s'. Could not be parsed. %s" %
(filter_string, expected), error_code=INVALID_PARAMETER_VALUE)
elif len(parsed) > 1:
raise MlflowException("Search filter '%s' contains multiple expressions. "
"%s " % (filter_string, expected),
error_code=INVALID_PARAMETER_VALUE)
statement = parsed[0]
invalids = list(filter(cls._invalid_statement_token, statement.tokens))
if len(invalids) > 0:
invalid_clauses = ", ".join("'%s'" % token for token in invalids)
raise MlflowException("Invalid clause(s) in filter string: %s. "
"%s" % (invalid_clauses, expected),
error_code=INVALID_PARAMETER_VALUE)
return [cls._get_comparison_for_model_registry(
si,
valid_search_keys)
for si in statement.tokens if isinstance(si, Comparison)]
示例15: tokens2sql
# 需要導入模塊: from sqlparse import sql [as 別名]
# 或者: from sqlparse.sql import Comparison [as 別名]
def tokens2sql(token: Token,
query: 'query_module.BaseQuery'
) -> Iterator[all_token_types]:
from .functions import SQLFunc
if isinstance(token, Identifier):
# Bug fix for sql parse
if isinstance(token[0], Parenthesis):
try:
int(token[0][1].value)
except ValueError:
raise
else:
yield SQLConstIdentifier(token, query)
elif isinstance(token[0], Function):
yield SQLFunc.token2sql(token, query)
else:
yield SQLIdentifier(token, query)
elif isinstance(token, Function):
yield SQLFunc.token2sql(token, query)
elif isinstance(token, Comparison):
yield SQLComparison(token, query)
elif isinstance(token, IdentifierList):
for tok in token.get_identifiers():
yield from SQLToken.tokens2sql(tok, query)
elif isinstance(token, Parenthesis):
yield SQLPlaceholder(token, query)
else:
raise SQLDecodeError(f'Unsupported: {token.value}')