Skip to content

Commit

Permalink
add OrderItemBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 2, 2019
1 parent 8995419 commit b43c03d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.antlr.filler.SQLStatementFiller;
import io.shardingsphere.core.parsing.antlr.filler.impl.dql.GroupByFiller;
import io.shardingsphere.core.parsing.antlr.filler.impl.dql.OrderByFiller;
import io.shardingsphere.core.parsing.antlr.filler.impl.dql.SelectClauseFiller;
import io.shardingsphere.core.parsing.antlr.sql.segment.expr.SubquerySegment;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* </p>
*/

package io.shardingsphere.core.parsing.antlr.filler.impl;
package io.shardingsphere.core.parsing.antlr.filler.impl.dql;

import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.antlr.filler.SQLStatementFiller;
Expand All @@ -36,9 +36,8 @@ public final class GroupByFiller implements SQLStatementFiller<GroupBySegment> {
public void fill(final GroupBySegment sqlSegment, final SQLStatement sqlStatement, final String sql, final ShardingRule shardingRule, final ShardingTableMetaData shardingTableMetaData) {
SelectStatement selectStatement = (SelectStatement) sqlStatement;
selectStatement.setGroupByLastPosition(sqlSegment.getGroupByLastPosition());
OrderByFiller orderFiller = new OrderByFiller();
for (OrderByItemSegment each : sqlSegment.getGroupByItems()) {
selectStatement.getGroupByItems().add(orderFiller.createOrderItem(selectStatement, each));
selectStatement.getGroupByItems().add(new OrderItemBuilder(selectStatement, each).createOrderItem());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* </p>
*/

package io.shardingsphere.core.parsing.antlr.filler.impl;
package io.shardingsphere.core.parsing.antlr.filler.impl.dql;

import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.antlr.filler.SQLStatementFiller;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.parsing.antlr.filler.impl.dql;

import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.antlr.filler.SQLStatementFiller;
import io.shardingsphere.core.parsing.antlr.sql.segment.order.OrderBySegment;
import io.shardingsphere.core.parsing.antlr.sql.segment.order.item.OrderByItemSegment;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement;
import io.shardingsphere.core.rule.ShardingRule;

/**
* Order by filler.
*
* @author duhongjun
*/
public final class OrderByFiller implements SQLStatementFiller<OrderBySegment> {

@Override
public void fill(final OrderBySegment sqlSegment, final SQLStatement sqlStatement, final String sql, final ShardingRule shardingRule, final ShardingTableMetaData shardingTableMetaData) {
SelectStatement selectStatement = (SelectStatement) sqlStatement;
if (!selectStatement.getSubQueryStatements().isEmpty()) {
return;
}
for (OrderByItemSegment each : sqlSegment.getOrderByItems()) {
selectStatement.getOrderByItems().add(new OrderItemBuilder(selectStatement, each).createOrderItem());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,40 @@
* </p>
*/

package io.shardingsphere.core.parsing.antlr.filler.impl;
package io.shardingsphere.core.parsing.antlr.filler.impl.dql;

import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.antlr.filler.SQLStatementFiller;
import io.shardingsphere.core.parsing.antlr.sql.segment.order.OrderBySegment;
import io.shardingsphere.core.parsing.antlr.sql.segment.order.item.ColumnNameOrderByItemSegment;
import io.shardingsphere.core.parsing.antlr.sql.segment.order.item.ExpressionOrderByItemSegment;
import io.shardingsphere.core.parsing.antlr.sql.segment.order.item.IndexOrderByItemSegment;
import io.shardingsphere.core.parsing.antlr.sql.segment.order.item.OrderByItemSegment;
import io.shardingsphere.core.parsing.parser.context.orderby.OrderItem;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement;
import io.shardingsphere.core.parsing.parser.token.TableToken;
import io.shardingsphere.core.rule.ShardingRule;
import io.shardingsphere.core.util.SQLUtil;
import lombok.RequiredArgsConstructor;

import java.util.List;

/**
* Order by filler.
* Order item builder.
*
* @author duhongjun
* @author zhangliang
*/
public final class OrderByFiller implements SQLStatementFiller<OrderBySegment> {
@RequiredArgsConstructor
public final class OrderItemBuilder {

@Override
public void fill(final OrderBySegment sqlSegment, final SQLStatement sqlStatement, final String sql, final ShardingRule shardingRule, final ShardingTableMetaData shardingTableMetaData) {
SelectStatement selectStatement = (SelectStatement) sqlStatement;
if (!selectStatement.getSubQueryStatements().isEmpty()) {
return;
}
for (OrderByItemSegment each : sqlSegment.getOrderByItems()) {
selectStatement.getOrderByItems().add(createOrderItem(selectStatement, each));
}
}
private final SelectStatement selectStatement;

private final OrderByItemSegment orderByItemSegment;

/**
* create order item.
* Create order item.
*
* @param selectStatement select statement
* @param orderByItemSegment order by item segment
* @return order item
*/
public OrderItem createOrderItem(final SelectStatement selectStatement, final OrderByItemSegment orderByItemSegment) {
public OrderItem createOrderItem() {
if (orderByItemSegment instanceof IndexOrderByItemSegment) {
return createOrderItem((IndexOrderByItemSegment) orderByItemSegment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
* </p>
*/

package io.shardingsphere.core.parsing.antlr.filler.impl;
package io.shardingsphere.core.parsing.antlr.filler.impl.dql;

import com.google.common.base.Optional;
import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.antlr.filler.SQLStatementFiller;
import io.shardingsphere.core.parsing.antlr.filler.impl.ExpressionFiller;
import io.shardingsphere.core.parsing.antlr.sql.segment.SelectClauseSegment;
import io.shardingsphere.core.parsing.antlr.sql.segment.expr.CommonExpressionSegment;
import io.shardingsphere.core.parsing.antlr.sql.segment.expr.ExpressionSegment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
<filler-rule sql-segment-class="definition.column.alter.RenameColumnSegment" filler-class="ddl.alter.RenameColumnDefinitionFiller" />
<filler-rule sql-segment-class="definition.column.alter.DropColumnDefinitionSegment" filler-class="ddl.alter.DropColumnDefinitionFiller" />
<filler-rule sql-segment-class="definition.constraint.DropPrimaryKeySegment" filler-class="ddl.alter.DropPrimaryKeyFiller" />
<filler-rule sql-segment-class="SelectClauseSegment" filler-class="SelectClauseFiller" />
<filler-rule sql-segment-class="SelectClauseSegment" filler-class="dql.SelectClauseFiller" />
<filler-rule sql-segment-class="FromWhereSegment" filler-class="FromWhereFiller" />
<filler-rule sql-segment-class="condition.SubQueryConditionSegment" filler-class="SubQueryConditionFiller" />
<filler-rule sql-segment-class="order.GroupBySegment" filler-class="GroupByFiller" />
<filler-rule sql-segment-class="order.OrderBySegment" filler-class="OrderByFiller" />
<filler-rule sql-segment-class="LimitSegment" filler-class="LimitFiller" />
<filler-rule sql-segment-class="order.GroupBySegment" filler-class="dql.GroupByFiller" />
<filler-rule sql-segment-class="order.OrderBySegment" filler-class="dql.OrderByFiller" />
<filler-rule sql-segment-class="LimitSegment" filler-class="dql.LimitFiller" />
<filler-rule sql-segment-class="ShowParamSegment" filler-class="ShowParamFiller" />
<filler-rule sql-segment-class="tcl.SetAutoCommitSegment" filler-class="tcl.SetAutoCommitFiller" />
</filler-rule-definition>

0 comments on commit b43c03d

Please sign in to comment.