Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def pop(self):
next_val = self.peek()
self.index += 1
# We need to handle three cases here where the next_val could be:
# 1. ('business')
# 2. . ('yelp.business')
# 3. .
# ('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
elif isinstance(token, Comparison):
# If 'token' is a Comparison type such as
# 'select * FROM abc a JOIN def d ON a.id = d.'. Then calling
# token.value on the comparison type will only return the lhs of the
# comparison. In this case a.id. So we need to do token.tokens to get
# both sides of the comparison and pick the last token out of that
# list.
token_v = token.tokens[-1].value.lower()
elif isinstance(token, Where):
# sqlparse groups all tokens from the where clause into a single token
# list. This means that token.value may be something like
# 'where foo > 5 and '. We need to look "inside" token.tokens to handle
# suggestions in complicated where clauses correctly
prev_keyword = stmt.reduce_to_prev_keyword()
return suggest_based_on_last_token(prev_keyword, stmt)
elif isinstance(token, Identifier):
# If the previous token is an identifier, we can suggest datatypes if
# we're in a parenthesized column/field list, e.g.:
# CREATE TABLE foo (Identifier
# CREATE FUNCTION foo (Identifier
# If we're not in a parenthesized list, the most likely scenario is the
# user is about to specify an alias, e.g.:
# SELECT Identifier
# SELECT foo FROM Identifier
prev_keyword, _ = find_prev_keyword(stmt.text_before_cursor)
if prev_keyword and prev_keyword.value == "(":
# Suggest datatypes
return suggest_based_on_last_token("type", stmt)
else:
return (Keyword(),)
else:
token_v = token.value.lower()
def find_the_identifiers(sql_part):
# logging.debug("sql_part is of type %s", type(sql_part))
result = []
for token in sql_part:
# logging.debug("Token |%s| is of type |%s|", token, type(token))
if isinstance(token, Identifier):
# logging.debug("Found an Identifier: |%s|", token)
result.append(str(token))
elif isinstance(token, IdentifierList):
# logging.debug("Found an IdentifierList: |%s|", token)
result.extend(find_the_identifiers_in_list(token))
return result
def extract_deps(stmt):
token = stmt.token_first()
while token is not None:
excluded_types = [sqlparse.sql.Function] # don't dive into window functions
if type(token) not in excluded_types and token.is_group():
# this is a thing that has a name -- note that!
local_defs.add(token.get_name())
# recurse into the group
extract_deps(token)
if type(token) == sqlparse.sql.Identifier:
new_node = Relation(token.get_parent_name(), token.get_real_name())
if new_node.valid():
new_nodes.add(new_node) # don't add edges yet!
index = stmt.token_index(token)
token = stmt.token_next(index)
def fields(self):
for token in self.tokens[
self.keywords['SELECT'] + 1:self.keywords['FROM']]:
if isinstance(token, Identifier):
return list(self.parse_columns([token]))
if isinstance(token, IdentifierList):
return list(self.parse_columns(token.get_identifiers()))
def _is_underspecified_table_mention(previous_token, current_token):
"""Returns True if current_token represents a table."""
if (previous_token and previous_token.value == FROM_CLAUSE_PLACEHOLDER and
isinstance(current_token, sqlparse.sql.Identifier)):
return True
return False
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
t2 = tl.token_next_by_instance(i, (sql.Function, sql.Parenthesis), end=t2_end)
if t1 and t2:
i2 = tl.token_index(t2, start=i)
if i1 > i2:
return t2
else:
return t1
elif t1:
return t1
else:
return t2
# bottom up approach: group subgroups first
[group_identifier(sgroup) for sgroup in tlist.get_sublists()
if not isinstance(sgroup, sql.Identifier)]
# real processing
idx = 0
token = _next_token(tlist, idx)
while token:
identifier_tokens = [token] + list(
_consume_cycle(tlist,
tlist.token_index(token, start=idx) + 1))
# remove trailing whitespace
if identifier_tokens and identifier_tokens[-1].ttype is T.Whitespace:
identifier_tokens = identifier_tokens[:-1]
if not (len(identifier_tokens) == 1
and (isinstance(identifier_tokens[0], (sql.Function, sql.Parenthesis))
or identifier_tokens[0].ttype in (T.Literal.Number.Integer,
T.Literal.Number.Float))):
group = tlist.group_tokens(sql.Identifier, identifier_tokens)
for item in token_stream:
if isinstance(item, IdentifierList):
for identifier in item.get_identifiers():
# Sometimes Keywords (such as FROM ) are classified as
# identifiers which don't have the get_real_name() method.
try:
schema_name = identifier.get_parent_name()
real_name = identifier.get_real_name()
is_function = (allow_functions and
_identifier_is_function(identifier))
except AttributeError:
continue
if real_name:
yield TableReference(schema_name, real_name,
identifier.get_alias(), is_function)
elif isinstance(item, Identifier):
schema_name, real_name, alias = parse_identifier(item)
is_function = allow_functions and _identifier_is_function(item)
yield TableReference(schema_name, real_name, alias, is_function)
elif isinstance(item, Function):
schema_name, real_name, alias = parse_identifier(item)
yield TableReference(None, real_name, alias, allow_functions)
def group_order(tlist):
idx = 0
token = tlist.token_next_by_type(idx, T.Keyword.Order)
while token:
prev = tlist.token_prev(token)
if isinstance(prev, sql.Identifier):
ido = tlist.group_tokens(sql.Identifier,
tlist.tokens_between(prev, token))
idx = tlist.token_index(ido) + 1
else:
idx = tlist.token_index(token) + 1
token = tlist.token_next_by_type(idx, T.Keyword.Order)