Skip to content

Commit

Permalink
Refactor _get_first_name method for clarity and flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Imran Imtiaz committed Jul 4, 2024
1 parent 8b03427 commit 2bc4493
Showing 1 changed file with 18 additions and 13 deletions.
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

0 comments on commit 2bc4493

Please sign in to comment.