Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor _get_first_name method for clarity and flexibility #778

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions sqlparse/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,23 +386,28 @@ def get_parent_name(self):
dot_idx, _ = self.token_next_by(m=(T.Punctuation, '.'))
_, prev_ = self.token_prev(dot_idx)
return remove_quotes(prev_.value) if prev_ is not None else None

def _get_first_name(self, idx=None, reverse=False, keywords=False, real_name=False):
"""Returns the name of the first token with a name"""

def _get_first_name(self, idx=None, reverse=False, keywords=False,
real_name=False):
"""Returns the name of the first token with a name"""
tokens = self.tokens[idx:] if idx is not None else self.tokens
tokens = reversed(tokens) if reverse else tokens

tokens = self.tokens[idx:] if idx else self.tokens
tokens = reversed(tokens) if reverse else tokens
types = [T.Name, T.Wildcard, T.String.Symbol]
name_types = [T.Name, T.Wildcard, T.String.Symbol]
if keywords:
name_types.append(T.Keyword)

if keywords:
types.append(T.Keyword)
for token in tokens:
if token.ttype in name_types:
return remove_quotes(token.value)
elif isinstance(token, (Identifier, Function)):
if real_name:
return token.get_real_name()
else:
return token.get_name()

return None

for token in tokens:
if token.ttype in types:
return remove_quotes(token.value)
elif isinstance(token, (Identifier, Function)):
return token.get_real_name() if real_name else token.get_name()


class Statement(TokenList):
Expand Down
Loading