Ejemplo n.º 1
0
 def _process_name(self, stmt, token_group, token, tokengroup_set):
     if token_group.last_token().match_type_value(Token(T.Keyword, 'AS')):
         # Alias follows AS Keyword
         token.ttype = ST.AliasName
         token_group.append(token)
     elif (token_group.parent.ttype in (ST.SelectClause, ST.FromClause)
           and ((token_group.ttype == ST.Identifier
                 and token_group.last_token().ttype == T.Name) or
                (token_group.ttype == ST.Function
                 and token_group.last_token().ttype == ST.ArgumentList) or
                (token_group.ttype == ST.SubQuery
                 and token_group.last_token().match_type_value(
                     Token(T.Punctuation, ')'))) or
                (token_group.ttype == ST.ComputedIdentifier
                 and token_group.last_token().ttype != T.Operator)
                or token_group.ttype == ST.SelectConstantIdentifier)):
         # Alias withot AS Keyword. Here TokenGroup has to be direct child of Select/From clause
         # and must satisfy other appropriate conditions
         token.ttype = ST.AliasName
         token_group.append(token)
     elif (token_group.parent.ttype == ST.ComputedIdentifier
           and token_group.ttype == ST.Identifier
           and token_group.last_token().ttype == T.Name):
         # Case: SELECT A.x+B.y SomeAlias FROM ...
         # here Computed Identifier => A.x+B.y and Identifier => B.y followed by Alias
         return self._process_name(stmt,
                                   self._switch_to_parent(token_group),
                                   token, tokengroup_set)
     elif (token_group.ttype == ST.SelectClause
           and token_group.last_token().ttype
           not in (T.Keyword, T.Punctuation)):
         # Case: SELECT CASE ... END AS case_alias
         token_group = token_group.merge_into_token_group(
             ST.ComputedIdentifier,
             token_list_start_index_included=token_group.last_token_index())
         token.ttype = ST.AliasName
         token_group.append(token)
     elif token_group.ttype == ST.Identifier:
         token_group.append(token)
     elif token_group.ttype == PT.WithClause:
         # Eg. WITH aliasname AS (SQL)
         token.ttype = PT.WithQueryAliasName
         with_identifier_grp = TokenGroup([
             token,
         ], PT.WithIdentifier)
         token_group.append(with_identifier_grp)
         token_group = with_identifier_grp
     else:
         identifier_grp = TokenGroup([
             token,
         ], ST.Identifier)
         token_group.append(identifier_grp)
         token_group = identifier_grp
     return token_group
Ejemplo n.º 2
0
    def _process_select_clause(self, stmt, token_group, token, tokengroup_set):
        select_clause_grp = TokenGroup([
            token,
        ], ST.SelectClause)
        if token_group.ttype in (ST.RoundBracket, ST.CollectionSet):
            # TODO: Should we check the preceeding token to make sure SELECT is the first keyword in Brackets
            token_group.ttype = ST.SubQuery
        elif (
                token_group.ttype
                in (ST.Select, ST.SelectInto, ST.InsertIntoSelect, ST.SubQuery)
                and (token_group.last_token().match_type_value(
                    Token(T.Keyword, 'UNION')) or
                     (token_group.last_token().match_type_value(
                         Token(T.Keyword, 'ALL')) and token_group.token_before(
                             next_token_index=token_group.last_token_index()).
                      match_type_value(Token(T.Keyword, 'UNION'))))
        ):  # token_group.has_token_as_immediate_child(Token(T.Keyword, 'UNION')):
            pass
        else:
            while token_group.ttype is not None and token_group.ttype not in tokengroup_set:
                # If Select Clause is preceeded by Insert Into Clause, get out of that Clause Token Group
                token_group = self._switch_to_parent(token_group)
            if token_group.ttype in tokengroup_set:
                token_group = self._switch_to_parent(token_group)

        token_group.append(select_clause_grp)

        # If we are at the Statement Token Group, set the Statement Type
        if token_group == stmt:
            if token_group.ttype is None:
                token_group.ttype = ST.Select
            elif token_group.ttype is ST.Insert:
                token_group.ttype = ST.InsertIntoSelect
        return select_clause_grp
Ejemplo n.º 3
0
 def _process_not(self, stmt, token_group, token, tokengroup_set):
     # TODO: NOT can be in SELECT Clause as well
     # TODO: IS Condition
     token.ttype = ST.LogicalOperator
     while token_group.ttype not in (ST.JoinOnClause, ST.WhereClause,
                                     ST.HavingClause, ST.ConditionGroup,
                                     ST.RoundBracket, ST.Comparison,
                                     ST.CaseExpression, ST.WhenExpression,
                                     ST.ThenExpression, ST.ElseExpression):
         # Get out of the Token Group until you find Where, JoinOn, Having Clause or ConditionGroup
         token_group = self._switch_to_parent(token_group)
     if token_group.ttype == ST.RoundBracket:
         token_group.ttype = ST.ConditionGroup
     if token_group.last_token().ttype not in (ST.Identifier,
                                               ST.ComputedIdentifier,
                                               ST.Function):
         not_condition_grp = TokenGroup([
             token,
         ], ST.Not)
         token_group.append(not_condition_grp)
         return not_condition_grp
     else:
         # This is the ST.Condition or ST.Comparison Token Group
         token_group = token_group.merge_into_token_group(
             ST.Condition,
             token_list_start_index_included=token_group.last_token_index())
         token_group.append(token)
         return token_group
Ejemplo n.º 4
0
 def _process_else(self, stmt, token_group, token, tokengroup_set):
     else_exp_grp = TokenGroup([
         token,
     ], ST.ElseExpression)
     while token_group.ttype not in (ST.CaseExpression):
         # Get out of the Token Group until you find Case Expression
         token_group = self._switch_to_parent(token_group)
     token_group.append(else_exp_grp)
     return else_exp_grp
Ejemplo n.º 5
0
 def _process_on(self, stmt, token_group, token, tokengroup_set):
     join_on_grp = TokenGroup([
         token,
     ], ST.JoinOnClause)
     while token_group.ttype != ST.FromClause:
         # Get out of the Token Group until you find the matching From Clause
         token_group = self._switch_to_parent(token_group)
     token_group.append(join_on_grp)
     return join_on_grp
Ejemplo n.º 6
0
 def _process_then(self, stmt, token_group, token, tokengroup_set):
     then_exp_grp = TokenGroup([
         token,
     ], ST.ThenExpression)
     while token_group.ttype not in (ST.WhenExpression):
         # Get out of the Token Group until you find When Expression
         token_group = self._switch_to_parent(token_group)
     token_group.append(then_exp_grp)
     return then_exp_grp
Ejemplo n.º 7
0
 def _process_limit(self, stmt, token_group, token, tokengroup_set):
     # Assumption: LIMIT will be at SELECT Statement Level
     limit_clause_grp = TokenGroup([
         token,
     ], PT.LimitClause)
     while token_group.ttype not in (ST.Select, ST.SelectInto,
                                     ST.InsertIntoSelect, ST.SubQuery):
         token_group = self._switch_to_parent(token_group)
     token_group.append(limit_clause_grp)
     return limit_clause_grp
Ejemplo n.º 8
0
 def _process_literal_number(self, stmt, token_group, token,
                             tokengroup_set):
     if token_group.ttype == ST.SelectClause:
         select_constant_identifier_grp = TokenGroup([
             token,
         ], ST.SelectConstantIdentifier)
         token_group.append(select_constant_identifier_grp)
         token_group = select_constant_identifier_grp
     else:
         token_group.append(token)
     return token_group
Ejemplo n.º 9
0
    def _process_with_clause(self, stmt, token_group, token, tokengroup_set):
        with_clause_grp = TokenGroup([
            token,
        ], PT.WithClause)
        if token_group.ttype in (ST.RoundBracket, ST.CollectionSet):
            token_group.ttype = ST.SubQuery
        else:
            while token_group.ttype is not None:
                token_group = self._switch_to_parent(token_group)

        token_group.append(with_clause_grp)
        return with_clause_grp
Ejemplo n.º 10
0
    def _process_where_clause(self, stmt, token_group, token, tokengroup_set):
        where_clause_grp = TokenGroup([
            token,
        ], ST.WhereClause)

        while token_group.ttype not in (ST.FromClause, ):
            # Get out of the Token Group until you find preceeding From Clause
            token_group = self._switch_to_parent(token_group)

        # Get out of the Clause Token Group and exit
        token_group = self._switch_to_parent(token_group)
        token_group.append(where_clause_grp)
        return where_clause_grp
Ejemplo n.º 11
0
 def _process_literal_string(self, stmt, token_group, token,
                             tokengroup_set):
     token.ttype = T.String
     if token_group.ttype == ST.SelectClause:
         select_constant_identifier_grp = TokenGroup([
             token,
         ], ST.SelectConstantIdentifier)
         token_group.append(select_constant_identifier_grp)
         token_group = select_constant_identifier_grp
     elif token_group.token_list[-1].ttype == T.String:
         setattr(token_group.token_list[-1], '_value',
                 token_group.token_list[-1].value() + token.value())
     else:
         token_group.append(token)
     return token_group
Ejemplo n.º 12
0
 def _process_case(self, stmt, token_group, token, tokengroup_set):
     case_exp_grp = TokenGroup([
         token,
     ], ST.CaseExpression)
     while token_group.ttype not in (ST.SelectClause, ST.JoinOnClause,
                                     ST.WhereClause, ST.HavingClause,
                                     ST.ConditionGroup, ST.Condition,
                                     ST.RoundBracket, ST.Not,
                                     ST.CaseExpression, ST.WhenExpression,
                                     ST.ThenExpression, ST.ElseExpression):
         # TODO: Might need to consider ComputedIdentifier Case
         # Get out of the Token Group until you find appropriate Clause
         token_group = self._switch_to_parent(token_group)
     token_group.append(case_exp_grp)
     return case_exp_grp
Ejemplo n.º 13
0
    def _process_order_by_clause(self, stmt, token_group, token,
                                 tokengroup_set):
        order_by_clause_grp = TokenGroup([
            token,
        ], ST.OrderByClause)

        while token_group.ttype not in (ST.FromClause, ST.WhereClause,
                                        ST.GroupByClause, ST.HavingClause):
            # Get out of the Token Group until you find preceeding Where / Group By / Having Clause
            token_group = self._switch_to_parent(token_group)

        # Get out of the Clause Token Group and exit
        token_group = self._switch_to_parent(token_group)
        token_group.append(order_by_clause_grp)
        return order_by_clause_grp
Ejemplo n.º 14
0
    def _process_having_clause(self, stmt, token_group, token, tokengroup_set):
        having_clause_grp = TokenGroup([
            token,
        ], ST.HavingClause)

        while token_group.ttype not in (ST.FromClause, ST.WhereClause,
                                        ST.GroupByClause):
            # NOTE: Having Clause need not require Group By Clause before it
            # Get out of the Token Group until you find preceeding From / Where / Group By Clause
            token_group = self._switch_to_parent(token_group)

        # Get out of the Clause Token Group and exit
        token_group = self._switch_to_parent(token_group)
        token_group.append(having_clause_grp)
        return having_clause_grp
Ejemplo n.º 15
0
    def _process_from_clause(self, stmt, token_group, token, tokengroup_set):
        from_clause_grp = TokenGroup([
            token,
        ], ST.FromClause)

        while token_group.ttype not in (ST.SelectClause, ST.SelectIntoClause,
                                        ST.UpdateSetClause):
            # TODO: Think about Delete From Clause before getting out
            # Get out of the Token Group until you find preceeding
            # (Select Clause, Select Into Clause, Update Set Clause)
            token_group = self._switch_to_parent(token_group)

        # Get out of the Clause Token Group and exit
        token_group = self._switch_to_parent(token_group)
        token_group.append(from_clause_grp)
        # TODO: Think about Delete From Clause before returning
        return from_clause_grp
Ejemplo n.º 16
0
    def _process_into(self, stmt, token_group, token, tokengroup_set):
        # Assumption: INTO Clause will never be in a Sub-Query
        while token_group.ttype not in (ST.InsertIntoClause, ST.SelectClause):
            # Get out of the Token Group until you find preceeding
            # (Insert Into Clause, Select Clause)
            token_group = self._switch_to_parent(token_group)
        if token_group.ttype == ST.InsertIntoClause:
            # INSERT INTO statement, just append INTO Keyword
            token_group.append(token)
        elif token_group.ttype == ST.SelectClause and token_group.parent == stmt:
            # Topmost Select Statement which should now be Select Into Statement
            stmt.ttype = ST.SelectInto
            token_group = self._switch_to_parent(token_group)
            into_clause_grp = TokenGroup([
                token,
            ], ST.SelectIntoClause)
            token_group.append(into_clause_grp)
            token_group = into_clause_grp

        return token_group
Ejemplo n.º 17
0
 def _process_exists(self, stmt, token_group, token, tokengroup_set):
     while token_group.ttype not in (
             ST.WhereClause,  # ST.JoinOnClause, ST.HavingClause,
             ST.RoundBracket,
             ST.ConditionGroup,
             ST.Not,
     ):  # ST.Condition,
         # ST.CaseExpression, ST.WhenExpression, ST.ThenExpression, ST.ElseExpression):
         # Get out of the Token Group until you find Condition Clause
         token_group = self._switch_to_parent(token_group)
     if token_group.ttype == ST.RoundBracket:
         token_group.ttype = ST.ConditionGroup
     if token_group.ttype == ST.Not and token_group.last_token(
     ).match_type_value(Token(ST.LogicalOperator, 'NOT')):
         token_group.ttype = ST.NotExists
         token_group.append(token)
         return token_group
     exists_grp = TokenGroup([
         token,
     ], ST.Exists)
     token_group.append(exists_grp)
     return exists_grp
Ejemplo n.º 18
0
 def _process_punctuation(self, stmt, token_group, token, tokengroup_set):
     if token.value() == '(':
         # Open a New Sub Query Token Group
         bracket_grp = TokenGroup([
             token,
         ], ST.RoundBracket)
         if token_group.ttype == ST.Identifier and token_group.last_token(
         ).ttype == T.Name:
             # This is actually a Function Identifier
             # TODO: Maybe this should be a sub group
             # Identifier.Function instead of just Identifier
             token_group.ttype = ST.Function
             # Function Argument List instead of Sub-Query
             bracket_grp.ttype = ST.ArgumentList
         elif token_group.ttype == PT.WithIdentifier and token_group.last_token(
         ).ttype == PT.WithQueryAliasName:
             token_group = token_group.merge_into_token_group(
                 PT.WithQueryAliasIdentifier,
                 token_list_start_index_included=token_group.
                 last_token_index())
             bracket_grp.ttype = ST.ArgumentList
         elif token_group.ttype in (ST.In, ST.NotIn):
             # This can be the CollectionSet or SubQuery (ttype will set to SubQuery in subsequent steps).
             bracket_grp.ttype = ST.CollectionSet
         token_group.append(bracket_grp)
         token_group = bracket_grp
     elif token.value() == ')':
         if token_group.ttype == ST.SubQuery and token_group.has_token_as_immediate_child(
                 token):
             # This step is required because when a Sub-Query is closed with ')' we do not switch
             # to its parent. This is done so that we can add alias to it if it exists
             return self._process_punctuation(
                 stmt, self._switch_to_parent(token_group), token,
                 tokengroup_set)
         while token_group.ttype not in (ST.RoundBracket, ST.ArgumentList,
                                         ST.SubQuery, ST.CollectionSet,
                                         ST.ConditionGroup):
             # Get out of the Token Group until you find the matching opening bracket
             token_group = self._switch_to_parent(token_group)
         token_group.append(token)
         if token_group.ttype == ST.RoundBracket and token_group.parent.ttype == ST.SelectClause:
             token_group = token_group.parent.merge_into_token_group(
                 ST.ComputedIdentifier,
                 token_list_start_index_included=token_group.parent.
                 last_token_index())
         elif token_group.ttype != ST.SubQuery:
             token_group = self._switch_to_parent(token_group)
     elif token.value() == '.':
         # table.column, alias.column (in general QualifierName<QualifierOperator>Name)
         token_group.last_token().ttype = ST.QualifierName
         token.ttype = ST.QualifierOperator
         token_group.append(token)
     elif token.value() == ',':
         if token_group.ttype in (ST.Identifier, ST.ComputedIdentifier,
                                  ST.SelectConstantIdentifier, ST.Function,
                                  ST.CaseExpression, ST.SubQuery):
             # Get out of the Token Group, so that we can create another Identifier
             token_group = self._switch_to_parent(token_group)
         if token_group.ttype == PT.WithIdentifier:
             token_group = self._switch_to_parent(token_group)
         token_group.append(token)
     return token_group