Beispiel #1
0
    def _add_group_by_item(self, sql_expr, select_statement):
        self.lexer_engine.unsupported_if_equal(*self.get_unsupported_keyword_before_group_by_item())
        order_by_type = OrderDirection.ASC
        if self.lexer_engine.equal_any(DefaultKeyword.ASC):
            self.lexer_engine.next_token()
        elif self.lexer_engine.skip_if_equal(DefaultKeyword.DESC):
            order_by_type = OrderDirection.DESC

        if isinstance(sql_expr, SQLPropertyExpression):
            order_item = OrderItem(sqlutil.get_exactly_value(sql_expr.owner.name),
                                   sqlutil.get_exactly_value(sql_expr.name), order_by_type, OrderDirection.ASC,
                                   select_statement.get_alias(
                                       sqlutil.get_exactly_value(sql_expr.owner.name) + '.' + sqlutil.get_exactly_value(
                                           sql_expr.name)))
        elif isinstance(sql_expr, SQLIdentifierExpression):
            order_item = OrderItem(None, sqlutil.get_exactly_value(sql_expr.name),
                                   order_by_type, OrderDirection.ASC,
                                   select_statement.get_alias(sqlutil.get_exactly_value(sql_expr.name)))
        elif isinstance(sql_expr, SQLIgnoreExpression):
            order_item = OrderItem(None, sql_expr.expression,
                                   order_by_type, OrderDirection.ASC,
                                   select_statement.get_alias(sql_expr.expression))
        else:
            return
        select_statement.group_by_items.append(order_item)
Beispiel #2
0
 def get_alias(self, name):
     if self.contain_star:
         return
     raw_name = sqlutil.get_exactly_value(name)
     for each in self.select_items:
         if strutil.equals_ignore_case(
                 raw_name, sqlutil.get_exactly_value(each.expression)):
             return each.alias
         if strutil.equals_ignore_case(raw_name, each.alias):
             return raw_name
Beispiel #3
0
 def _parse_set_column(self, update_statement):
     if self.lexer_engine.equal_any(Symbol.LEFT_PAREN):
         self.lexer_engine.skip_parentheses(update_statement)
         return
     begin_position = self.lexer_engine.get_current_token().end_position
     literals = self.lexer_engine.get_current_token().literals
     self.lexer_engine.next_token()
     if self.lexer_engine.skip_if_equal(Symbol.DOT):
         if strutil.equals_ignore_case(update_statement.tables.get_single_table_name(),
                                       sqlutil.get_exactly_value(literals)):
             update_statement.sql_tokens.append(TableToken(begin_position - len(literals), 0, literals))
         self.lexer_engine.next_token()
Beispiel #4
0
 def _parse_common_or_star_select_item(self, select_statement):
     result = ""
     literals = self.lexer_engine.get_current_token().literals
     position = self.lexer_engine.get_current_token().end_position - len(literals)
     result += literals
     self.lexer_engine.next_token()
     if self.lexer_engine.equal_any(Symbol.LEFT_PAREN):
         result += self.lexer_engine.skip_parentheses(select_statement)
     elif self.lexer_engine.equal_any(Symbol.DOT):
         table_name = sqlutil.get_exactly_value(literals)
         if self.sharding_rule.try_find_table_rule_by_logic_table(
                 table_name) or self.sharding_rule.find_binding_table_rule(table_name):
             select_statement.sql_tokens.append(TableToken(position, 0, literals))
         result += self.lexer_engine.get_current_token().literals
         self.lexer_engine.next_token()
         if self.lexer_engine.equal_any(Symbol.STAR):
             return self._parse_star_select_item()
         result += self.lexer_engine.get_current_token().literals
         self.lexer_engine.next_token()
     return CommonSelectItem(sqlutil.get_exactly_value(result) + self._parse_rest_select_item(select_statement),
                             self.alias_expression_parser.parse_select_item_alias())
Beispiel #5
0
    def _parse_select_order_by_item(self, select_statement):
        sql_expr = self.basic_expression_parser.parse(select_statement)
        order_by_type = OrderDirection.ASC
        if self.lexer_engine.skip_if_equal(DefaultKeyword.ASC):
            order_by_type = OrderDirection.ASC
        elif self.lexer_engine.skip_if_equal(DefaultKeyword.DESC):
            order_by_type = OrderDirection.DESC

        if isinstance(sql_expr, SQLNumberExpression):
            return OrderItem(None, None, order_by_type, OrderDirection.ASC, None, sql_expr.number)
        if isinstance(sql_expr, SQLIdentifierExpression):
            return OrderItem(None, sqlutil.get_exactly_value(sql_expr.name),
                             order_by_type, OrderDirection.ASC,
                             select_statement.get_alias(sqlutil.get_exactly_value(sql_expr.name)))
        if isinstance(sql_expr, SQLPropertyExpression):
            return OrderItem(sqlutil.get_exactly_value(sql_expr.owner.name),
                             sqlutil.get_exactly_value(sql_expr.name), order_by_type, OrderDirection.ASC,
                             select_statement.get_alias(
                                 sqlutil.get_exactly_value(sql_expr.owner.name) + '.' + sqlutil.get_exactly_value(
                                     sql_expr.name)))
        if isinstance(sql_expr, SQLIgnoreExpression):
            return OrderItem(None, sqlutil.get_exactly_value(sql_expr.expression),
                             order_by_type, OrderDirection.ASC,
                             select_statement.get_alias(sql_expr.expression))
        raise SQLParsingException(self.lexer_engine)
Beispiel #6
0
 def _get_expression(self, literals, sql_statement):
     if self.lexer_engine.equal_any(Symbol.QUESTION):
         sql_statement.increase_parameters_index()
         return SQLPlaceholderExpression(sql_statement.parameters_index - 1)
     if self.lexer_engine.equal_any(Literals.CHARS):
         return SQLTextExpression(literals)
     if self.lexer_engine.equal_any(Literals.INT):
         return SQLNumberExpression(int(literals))
     if self.lexer_engine.equal_any(Literals.FLOAT):
         return SQLNumberExpression(float(literals))
     if self.lexer_engine.equal_any(Literals.HEX):
         return SQLNumberExpression(int(literals, 16))
     if self.lexer_engine.equal_any(Literals.IDENTIFIER):
         return SQLIdentifierExpression(sqlutil.get_exactly_value(literals))
     return SQLIgnoreExpression(literals)
Beispiel #7
0
 def parse_table_factor(self, sql_statement, is_single_table_only):
     literals = self.lexer_engine.get_current_token().literals
     begin_position = self.lexer_engine.get_current_token().end_position - len(literals)
     skipped_schema_name_length = 0
     self.lexer_engine.next_token()
     if self.lexer_engine.skip_if_equal(Symbol.DOT):
         skipped_schema_name_length += len(literals) + len(Symbol.DOT.value)
         literals = self.lexer_engine.get_current_token().literals
     table_name = sqlutil.get_exactly_value(literals)
     if not table_name:
         return
     alias = self.alias_expression_parser.parse_table_alias()
     if is_single_table_only or self.sharding_rule.try_find_table_rule_by_logic_table(
             table_name) or self.sharding_rule.find_binding_table_rule(
         table_name) or self.sharding_rule.sharding_data_source_names.get_default_data_source_name() in \
             self.sharding_rule.sharding_data_source_names.data_source_names:
         sql_statement.sql_tokens.append(TableToken(begin_position, skipped_schema_name_length, literals))
         sql_statement.tables.add(Table(table_name, alias))
     self._parse_force_index(table_name, sql_statement)
     self._parse_join_table(sql_statement)
     if is_single_table_only and not sql_statement.tables.is_single_table():
         raise UnsupportedOperationException("Cannot support Multiple-Table")
Beispiel #8
0
 def parse(self, insert_statement, sharding_meta_data):
     result = list()
     table_name = insert_statement.tables.get_single_table_name()
     generated_key_column = self.sharding_rule.get_generate_key_column(table_name)
     count = 0
     if self.lexer_engine.equal_any(Symbol.LEFT_PAREN):
         while True:
             self.lexer_engine.next_token()
             column_name = sqlutil.get_exactly_value(self.lexer_engine.get_current_token().literals)
             result.append(Column(column_name, table_name))
             self.lexer_engine.next_token()
             if generated_key_column and strutil.equals_ignore_case(generated_key_column.name, column_name):
                 insert_statement.generate_key_column_index = count
             count += 1
             if self.lexer_engine.equal_any(Symbol.RIGHT_PAREN) or self.lexer_engine.equal_any(Assist.END):
                 break
         insert_statement.columns_list_last_position = self.lexer_engine.get_current_token().end_position - len(
             self.lexer_engine.get_current_token().literals)
         self.lexer_engine.next_token()
     else:
         column_names = sharding_meta_data.table_meta_data_map.get(table_name).get_all_column_names()
         begin_position = self.lexer_engine.get_current_token().end_position - len(
             self.lexer_engine.get_current_token().literals) - 1
         insert_statement.sql_tokens.append(InsertColumnToken(begin_position, '('))
         columns_token = ItemsToken(begin_position)
         columns_token.is_first_of_items_special = True
         for column_name in column_names:
             result.append(Column(column_name, table_name))
             if generated_key_column and strutil.equals_ignore_case(generated_key_column.name, column_name):
                 insert_statement.generate_key_column_index = count
             columns_token.items.append(column_name)
             count += 1
         insert_statement.sql_tokens.append(columns_token)
         insert_statement.sql_tokens.append(InsertColumnToken(begin_position, ')'))
         insert_statement.columns_list_last_position = begin_position
     insert_statement.columns.extend(result)
Beispiel #9
0
 def _parse_alias(self):
     result = sqlutil.get_exactly_value(
         self.lexer_engine.get_current_token().literals)
     self.lexer_engine.next_token()
     return result
Beispiel #10
0
 def index_name(self):
     return sqlutil.get_exactly_value(self.original_literals)
Beispiel #11
0
 def table_name(self):
     return sqlutil.get_exactly_value(self._table_name)
Beispiel #12
0
 def _set_table_token(self, sql_statement, begin_position, property_expr):
     owner = property_expr.owner.name
     if sqlutil.get_exactly_value(
             owner) in sql_statement.tables.get_table_names():
         sql_statement.sql_tokens.append(
             TableToken(begin_position - len(owner), 0, owner))
Beispiel #13
0
 def expression(self):
     return sqlutil.get_exactly_value(self.aggregation_type.name +
                                      self.inner_expression)
Beispiel #14
0
 def _get_column_with_owner(self, tables, property_expression):
     table = tables.find(sqlutil.get_exactly_value(property_expression.owner.name))
     if table:
         return Column(sqlutil.get_exactly_value(property_expression.name), table.name)
Beispiel #15
0
 def _get_column_without_owner(self, tables, identifier_expression):
     if tables.is_single_table():
         return Column(sqlutil.get_exactly_value(identifier_expression.name), tables.get_single_table_name())
Beispiel #16
0
 def _is_star_select_item(self):
     return sqlutil.get_exactly_value(self.lexer_engine.get_current_token().literals) == Symbol.STAR.value
Beispiel #17
0
 def original_literals(self):
     return sqlutil.get_exactly_value(self._original_literals)