1616from sqlparse .utils import imt , remove_quotes
1717
1818
19+ class NameAliasMixin :
20+ """Implements get_real_name and get_alias."""
21+
22+ def get_real_name (self ):
23+ """Returns the real name (object name) of this identifier."""
24+ # a.b
25+ dot_idx , _ = self .token_next_by (m = (T .Punctuation , '.' ))
26+ return self ._get_first_name (dot_idx , real_name = True )
27+
28+ def get_alias (self ):
29+ """Returns the alias for this identifier or ``None``."""
30+
31+ # "name AS alias"
32+ kw_idx , kw = self .token_next_by (m = (T .Keyword , 'AS' ))
33+ if kw is not None :
34+ return self ._get_first_name (kw_idx + 1 , keywords = True )
35+
36+ # "name alias" or "complicated column expression alias"
37+ _ , ws = self .token_next_by (t = T .Whitespace )
38+ if len (self .tokens ) > 2 and ws is not None :
39+ return self ._get_first_name (reverse = True )
40+
41+
1942@unicode_compatible
2043class Token (object ):
2144 """Base class for all other classes in this module.
@@ -341,16 +364,7 @@ def has_alias(self):
341364
342365 def get_alias (self ):
343366 """Returns the alias for this identifier or ``None``."""
344-
345- # "name AS alias"
346- kw_idx , kw = self .token_next_by (m = (T .Keyword , 'AS' ))
347- if kw is not None :
348- return self ._get_first_name (kw_idx + 1 , keywords = True )
349-
350- # "name alias" or "complicated column expression alias"
351- _ , ws = self .token_next_by (t = T .Whitespace )
352- if len (self .tokens ) > 2 and ws is not None :
353- return self ._get_first_name (reverse = True )
367+ return None
354368
355369 def get_name (self ):
356370 """Returns the name of this identifier.
@@ -363,9 +377,7 @@ def get_name(self):
363377
364378 def get_real_name (self ):
365379 """Returns the real name (object name) of this identifier."""
366- # a.b
367- dot_idx , _ = self .token_next_by (m = (T .Punctuation , '.' ))
368- return self ._get_first_name (dot_idx , real_name = True )
380+ return None
369381
370382 def get_parent_name (self ):
371383 """Return name of the parent object if any.
@@ -433,7 +445,7 @@ def get_type(self):
433445 return 'UNKNOWN'
434446
435447
436- class Identifier (TokenList ):
448+ class Identifier (NameAliasMixin , TokenList ):
437449 """Represents an identifier.
438450
439451 Identifiers may have aliases or typecasts.
@@ -599,7 +611,7 @@ def get_cases(self, skip_ws=False):
599611 return ret
600612
601613
602- class Function (TokenList ):
614+ class Function (NameAliasMixin , TokenList ):
603615 """A function or procedure call."""
604616
605617 def get_parameters (self ):
0 commit comments