From 2bc4493faa682a066851375609682ae83616bebf Mon Sep 17 00:00:00 2001 From: Imran Imtiaz Date: Thu, 4 Jul 2024 10:29:48 +0400 Subject: [PATCH] Refactor _get_first_name method for clarity and flexibility --- sqlparse/sql.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 10373751..4191d889 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -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):