Example #1
0
 def parse_identifier(item):
     alias = item.get_alias()
     sp_idx = item.token_next_by(t=Whitespace)[0] or len(item.tokens)
     item_rev = Identifier(list(reversed(item.tokens[:sp_idx])))
     name = item_rev._get_first_name(real_name=True)
     alias = alias or name
     dot_idx, _ = item_rev.token_next_by(m=(Punctuation, '.'))
     if dot_idx is not None:
         schema_name = item_rev._get_first_name(dot_idx, real_name=True)
         dot_idx, _ = item_rev.token_next_by(m=(Punctuation, '.'),
                                             idx=dot_idx)
         if dot_idx is not None:
             catalog_name = item_rev._get_first_name(dot_idx,
                                                     real_name=True)
         else:
             catalog_name = None
     else:
         schema_name = None
         catalog_name = None
     schema_quoted = schema_name and item.value[0] == '"'
     if schema_name and not schema_quoted:
         schema_name = schema_name.lower()
     quote_count = item.value.count('"')
     name_quoted = quote_count > 2 or (quote_count and not schema_quoted)
     alias_quoted = alias and item.value[-1] == '"'
     if alias_quoted or name_quoted and not alias and name.islower():
         alias = '"' + (alias or name) + '"'
     if name and not name_quoted and not name.islower():
         if not alias:
             alias = name
         name = name.lower()
     return catalog_name, schema_name, name, alias
Example #2
0
 def get_identifier_parents(self):
     if self.identifier is None:
         return None, None
     item_rev = Identifier(list(reversed(self.identifier.tokens)))
     name = item_rev._get_first_name(real_name = True)
     dot_idx, _ = item_rev.token_next_by(m=(Punctuation, '.'))
     if dot_idx is not None:
         schema_name = item_rev._get_first_name(dot_idx, real_name = True)
         dot_idx, _ = item_rev.token_next_by(m=(Punctuation, '.'), idx = dot_idx)
         if dot_idx is not None:
             catalog_name = item_rev._get_first_name(dot_idx, real_name = True)
         else:
             catalog_name = None
     else:
         schema_name = None
         catalog_name = None
     return catalog_name, schema_name
Example #3
0
 def parse_identifier(item):
     alias = item.get_alias()
     sp_idx = item.token_next_by(t=Whitespace)[0] or len(item.tokens)
     item_rev = Identifier(list(reversed(item.tokens[:sp_idx])))
     name = item_rev._get_first_name(real_name=True)
     alias = alias or name
     dot_idx, _ = item_rev.token_next_by(m=(Punctuation, '.'))
     if dot_idx is not None:
         schema_name = item_rev._get_first_name(dot_idx, real_name=True)
         dot_idx, _ = item_rev.token_next_by(m=(Punctuation, '.'),
                                             idx=dot_idx)
         if dot_idx is not None:
             catalog_name = item_rev._get_first_name(dot_idx,
                                                     real_name=True)
         else:
             catalog_name = None
     else:
         schema_name = None
         catalog_name = None
     # TODO: this business below needs help
     # for one we need to apply this logic to catalog_name
     # then the logic around name_quoted = quote_count > 2 obviously
     # doesn't work.  Finally, quotechar needs to be customized
     schema_quoted = schema_name and item.value[0] == '"'
     if schema_name and not schema_quoted:
         schema_name = schema_name.lower()
     quote_count = item.value.count('"')
     name_quoted = quote_count > 2 or (quote_count and not schema_quoted)
     alias_quoted = alias and item.value[-1] == '"'
     if alias_quoted or name_quoted and not alias and name.islower():
         alias = '"' + (alias or name) + '"'
     if name and not name_quoted and not name.islower():
         if not alias:
             alias = name
         name = name.lower()
     return catalog_name, schema_name, name, alias
Example #4
0
 def create(identifier: Identifier):
     # rewrite identifier's get_real_name method, by matching the last dot instead of the first dot, so that the
     # real name for a.b.c will be c instead of b
     dot_idx, _ = identifier._token_matching(
         lambda token: imt(token, m=(Punctuation, ".")),
         start=len(identifier.tokens),
         reverse=True,
     )
     real_name = identifier._get_first_name(dot_idx, real_name=True)
     # rewrite identifier's get_parent_name accordingly
     parent_name = (
         "".join(
             [
                 escape_identifier_name(token.value)
                 for token in identifier.tokens[:dot_idx]
             ]
         )
         if dot_idx
         else None
     )
     schema = Schema(parent_name) if parent_name is not None else Schema()
     return Table(real_name, schema)