Skip to content

Commit

Permalink
for #2084, remove useless codes for SubquerySegment
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Apr 25, 2019
1 parent b182cfe commit 41a46fc
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@
import com.google.common.base.Optional;
import org.antlr.v4.runtime.ParserRuleContext;
import org.apache.shardingsphere.core.parse.antlr.extractor.api.OptionalSQLSegmentExtractor;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.ExtractorUtils;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.RuleName;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.SelectItemsSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.WhereSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.expr.SubquerySegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.order.GroupBySegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.order.OrderBySegment;

import java.util.Map;

Expand All @@ -37,47 +32,18 @@
*/
public final class SubqueryExtractor implements OptionalSQLSegmentExtractor {

private final SelectWhereExtractor selectWhereExtractor = new SelectWhereExtractor();

private final GroupByExtractor groupByExtractor = new GroupByExtractor();

private final OrderByExtractor orderByExtractor = new OrderByExtractor();

@Override
public Optional<SubquerySegment> extract(final ParserRuleContext subqueryNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
if (!RuleName.SUBQUERY.getName().endsWith(subqueryNode.getClass().getSimpleName())) {
return Optional.absent();
}
boolean subqueryInFrom = false;
ParserRuleContext parentNode = subqueryNode.getParent();
while (null != parentNode) {
if (RuleName.FROM_CLAUSE.getName().equals(parentNode.getClass().getSimpleName())) {
subqueryInFrom = true;
break;
}
parentNode = parentNode.getParent();
}
SubquerySegment result = new SubquerySegment(subqueryNode.getStart().getStartIndex(), subqueryNode.getStop().getStopIndex(), subqueryInFrom);
Optional<SelectItemsSegment> selectItemsSegment = new SelectItemsExtractor().extract(subqueryNode, parameterMarkerIndexes);
if (selectItemsSegment.isPresent()) {
result.setSelectItemsSegment(selectItemsSegment.get());
}
Optional<WhereSegment> whereSegment = selectWhereExtractor.extract(subqueryNode, parameterMarkerIndexes);
if (whereSegment.isPresent()) {
result.setWhereSegment(whereSegment.get());
}
Optional<GroupBySegment> groupBySegment = groupByExtractor.extract(subqueryNode, parameterMarkerIndexes);
if (groupBySegment.isPresent()) {
result.setGroupBySegment(groupBySegment.get());
}
Optional<OrderBySegment> orderBySegment = orderByExtractor.extract(subqueryNode, parameterMarkerIndexes);
if (orderBySegment.isPresent()) {
result.setOrderBySegment(orderBySegment.get());
}
Optional<ParserRuleContext> aliasNode = ExtractorUtils.findFirstChildNode(subqueryNode.getParent(), RuleName.ALIAS);
if (aliasNode.isPresent()) {
result.setAlias(aliasNode.get().getText());
}
return Optional.of(result);
return Optional.of(new SubquerySegment(subqueryNode.getStart().getStartIndex(), subqueryNode.getStop().getStopIndex()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,5 @@ public void fill(final SubquerySegment sqlSegment, final SQLStatement sqlStateme
SelectStatement selectStatement = (SelectStatement) sqlStatement;
SelectStatement subqueryStatement = new SelectStatement();
subqueryStatement.setParentStatement(selectStatement);
if (sqlSegment.getSelectItemsSegment().isPresent()) {
new SelectItemsFiller().fill(sqlSegment.getSelectItemsSegment().get(), subqueryStatement);
}
if (!sqlSegment.isSubqueryInFrom()) {
return;
}
if (sqlSegment.getGroupBySegment().isPresent()) {
new GroupByFiller().fill(sqlSegment.getGroupBySegment().get(), subqueryStatement);
}
if (sqlSegment.getOrderBySegment().isPresent()) {
new OrderByFiller().fill(sqlSegment.getOrderBySegment().get(), subqueryStatement);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,82 +17,24 @@

package org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.expr;

import com.google.common.base.Optional;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.core.parse.antlr.sql.AliasAvailable;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.SelectItemsSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.WhereSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.item.SelectItemSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.order.GroupBySegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.dml.order.OrderBySegment;
import org.apache.shardingsphere.core.parse.old.parser.expression.SQLExpression;
import org.apache.shardingsphere.core.parse.old.parser.expression.SQLIgnoreExpression;
import org.apache.shardingsphere.core.parse.util.SQLUtil;

/**
* Subquery expression segment.
*
* @author duhongjun
*/
@RequiredArgsConstructor
@Getter
@Setter
public final class SubquerySegment implements SelectItemSegment, ExpressionSegment, AliasAvailable {
public final class SubquerySegment implements SelectItemSegment, ExpressionSegment {

private final int startIndex;

@Getter
private final int stopIndex;

private final boolean subqueryInFrom;

private SelectItemsSegment selectItemsSegment;

private WhereSegment whereSegment;

private GroupBySegment groupBySegment;

private OrderBySegment orderBySegment;

private String alias;

/**
* Get select items segment.
*
* @return select items segment
*/
public Optional<SelectItemsSegment> getSelectItemsSegment() {
return Optional.fromNullable(selectItemsSegment);
}

/**
* Get group by segment.
*
* @return group by segment
*/
public Optional<GroupBySegment> getGroupBySegment() {
return Optional.fromNullable(groupBySegment);
}

/**
* Get order by segment.
*
* @return order by segment
*/
public Optional<OrderBySegment> getOrderBySegment() {
return Optional.fromNullable(orderBySegment);
}

@Override
public Optional<String> getAlias() {
return Optional.fromNullable(alias);
}

@Override
public void setAlias(final String alias) {
this.alias = SQLUtil.getExactlyValue(alias);
}

@Override
public SQLExpression getSQLExpression(final String sql) {
Expand Down

0 comments on commit 41a46fc

Please sign in to comment.