提交 ed737e37 编写于 作者: T tristaZero

refactor ColumnProjectionSegment

上级 1deb1252
......@@ -66,13 +66,13 @@ public final class EncryptProjectionTokenGenerator extends BaseEncryptSQLTokenGe
}
private boolean isEncryptLogicColumn(final ProjectionSegment projectionSegment, final EncryptTable encryptTable) {
return projectionSegment instanceof ColumnProjectionSegment && encryptTable.getLogicColumns().contains(((ColumnProjectionSegment) projectionSegment).getIdentifier().getValue());
return projectionSegment instanceof ColumnProjectionSegment && encryptTable.getLogicColumns().contains(((ColumnProjectionSegment) projectionSegment).getColumn().getIdentifier().getValue());
}
private SubstitutableColumnNameToken generateSQLToken(final ColumnProjectionSegment segment, final String tableName) {
Optional<String> plainColumn = getEncryptRule().findPlainColumn(tableName, segment.getIdentifier().getValue());
String columnName = plainColumn.isPresent() && !queryWithCipherColumn ? plainColumn.get() : getEncryptRule().getCipherColumn(tableName, segment.getIdentifier().getValue());
return segment.getOwner().isPresent() ? new SubstitutableColumnNameToken(segment.getOwner().get().getStopIndex() + 2, segment.getStopIndex(), columnName)
Optional<String> plainColumn = getEncryptRule().findPlainColumn(tableName, segment.getColumn().getIdentifier().getValue());
String columnName = plainColumn.isPresent() && !queryWithCipherColumn ? plainColumn.get() : getEncryptRule().getCipherColumn(tableName, segment.getColumn().getIdentifier().getValue());
return segment.getColumn().getOwner().isPresent() ? new SubstitutableColumnNameToken(segment.getColumn().getOwner().get().getStopIndex() + 2, segment.getStopIndex(), columnName)
: new SubstitutableColumnNameToken(segment.getStartIndex(), segment.getStopIndex(), columnName);
}
}
......@@ -20,7 +20,6 @@ package org.apache.shardingsphere.sql.parser.sql.predicate;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ColumnProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.PredicateSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.OwnerSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
......@@ -55,11 +54,6 @@ public final class PredicateExtractor {
OwnerSegment segment = ((ColumnSegment) predicate.getRightValue()).getOwner().get();
result.add(new TableSegment(segment.getStartIndex(), segment.getStopIndex(), segment.getIdentifier()));
}
if (isToGenerateTableTokenForProjection()) {
Preconditions.checkState(((ColumnProjectionSegment) predicate.getRightValue()).getOwner().isPresent());
OwnerSegment segment = ((ColumnProjectionSegment) predicate.getRightValue()).getOwner().get();
new TableSegment(segment.getStartIndex(), segment.getStopIndex(), segment.getIdentifier());
}
return result;
}
......@@ -72,11 +66,6 @@ public final class PredicateExtractor {
&& ((ColumnSegment) predicate.getRightValue()).getOwner().isPresent() && isTable(((ColumnSegment) predicate.getRightValue()).getOwner().get());
}
private boolean isToGenerateTableTokenForProjection() {
return predicate.getRightValue() instanceof ColumnProjectionSegment && ((ColumnProjectionSegment) predicate.getRightValue()).getOwner().isPresent()
&& isTable(((ColumnProjectionSegment) predicate.getRightValue()).getOwner().get());
}
private boolean isTable(final OwnerSegment owner) {
for (TableSegment each : tables) {
if (owner.getIdentifier().getValue().equals(each.getAlias().orElse(null))) {
......
......@@ -36,7 +36,7 @@ import java.util.Optional;
@Getter
@Setter
@ToString
public class ColumnSegment implements SQLSegment, PredicateRightValue, OwnerAvailable {
public final class ColumnSegment implements SQLSegment, PredicateRightValue, OwnerAvailable {
private final int startIndex;
......@@ -51,12 +51,12 @@ public class ColumnSegment implements SQLSegment, PredicateRightValue, OwnerAvai
*
* @return qualified name
*/
public final String getQualifiedName() {
public String getQualifiedName() {
return null == owner ? identifier.getValue() : owner.getIdentifier().getValue() + "." + identifier.getValue();
}
@Override
public final Optional<OwnerSegment> getOwner() {
public Optional<OwnerSegment> getOwner() {
return Optional.ofNullable(owner);
}
}
......@@ -29,7 +29,10 @@ import java.util.Optional;
/**
* Column projection segment.
*/
public final class ColumnProjectionSegment extends ColumnSegment implements ProjectionSegment, AliasAvailable {
public final class ColumnProjectionSegment implements ProjectionSegment, AliasAvailable {
@Getter
private final ColumnSegment column;
@Getter
private final String text;
......@@ -38,15 +41,24 @@ public final class ColumnProjectionSegment extends ColumnSegment implements Proj
private AliasSegment alias;
public ColumnProjectionSegment(final String text, final ColumnSegment columnSegment) {
super(columnSegment.getStartIndex(), columnSegment.getStopIndex(), columnSegment.getIdentifier());
column = columnSegment;
this.text = SQLUtil.getExpressionWithoutOutsideParentheses(text);
if (columnSegment.getOwner().isPresent()) {
setOwner(columnSegment.getOwner().get());
}
}
@Override
public Optional<String> getAlias() {
return null == alias ? Optional.empty() : Optional.ofNullable(alias.getIdentifier().getValue());
}
@Override
public int getStartIndex() {
return column.getStartIndex();
}
@Override
public int getStopIndex() {
return column.getStopIndex();
// TODO
// return null == alias ? alias.getStopIndex() : column.getStopIndex();
}
}
......@@ -537,9 +537,6 @@ public final class MySQLDMLVisitor extends MySQLVisitor implements DMLVisitor {
if (predicate.getRightValue() instanceof ColumnSegment && ((ColumnSegment) predicate.getRightValue()).getOwner().isPresent()) {
result.getValue().add(createTableSegment(((ColumnSegment) predicate.getRightValue()).getOwner().get()));
}
if (predicate.getRightValue() instanceof ColumnProjectionSegment && ((ColumnProjectionSegment) predicate.getRightValue()).getOwner().isPresent()) {
result.getValue().add(createTableSegment(((ColumnProjectionSegment) predicate.getRightValue()).getOwner().get()));
}
}
return result;
}
......
......@@ -441,9 +441,6 @@ public final class OracleDMLVisitor extends OracleVisitor implements DMLVisitor
if (predicate.getRightValue() instanceof ColumnSegment && ((ColumnSegment) predicate.getRightValue()).getOwner().isPresent()) {
result.getValue().add(createTableSegment(((ColumnSegment) predicate.getRightValue()).getOwner().get()));
}
if (predicate.getRightValue() instanceof ColumnProjectionSegment && ((ColumnProjectionSegment) predicate.getRightValue()).getOwner().isPresent()) {
result.getValue().add(createTableSegment(((ColumnProjectionSegment) predicate.getRightValue()).getOwner().get()));
}
}
return result;
}
......
......@@ -450,9 +450,6 @@ public final class PostgreSQLDMLVisitor extends PostgreSQLVisitor implements DML
if (predicate.getRightValue() instanceof ColumnSegment && ((ColumnSegment) predicate.getRightValue()).getOwner().isPresent()) {
result.getValue().add(createTableSegment(((ColumnSegment) predicate.getRightValue()).getOwner().get()));
}
if (predicate.getRightValue() instanceof ColumnProjectionSegment && ((ColumnProjectionSegment) predicate.getRightValue()).getOwner().isPresent()) {
result.getValue().add(createTableSegment(((ColumnProjectionSegment) predicate.getRightValue()).getOwner().get()));
}
}
return result;
}
......
......@@ -77,8 +77,8 @@ public final class ProjectionEngine {
}
private ColumnProjection createProjection(final ColumnProjectionSegment projectionSegment) {
String owner = projectionSegment.getOwner().isPresent() ? projectionSegment.getOwner().get().getIdentifier().getValue() : null;
return new ColumnProjection(owner, projectionSegment.getIdentifier().getValue(), projectionSegment.getAlias().orElse(null));
String owner = projectionSegment.getColumn().getOwner().isPresent() ? projectionSegment.getColumn().getOwner().get().getIdentifier().getValue() : null;
return new ColumnProjection(owner, projectionSegment.getColumn().getIdentifier().getValue(), projectionSegment.getAlias().orElse(null));
}
private ExpressionProjection createProjection(final ExpressionProjectionSegment projectionSegment) {
......
......@@ -36,6 +36,7 @@ import org.apache.shardingsphere.sql.parser.relation.segment.table.TableAvailabl
import org.apache.shardingsphere.sql.parser.relation.segment.table.TablesContext;
import org.apache.shardingsphere.sql.parser.relation.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.predicate.PredicateExtractor;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ColumnProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ProjectionsSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.order.item.ColumnOrderByItemSegment;
......@@ -212,16 +213,30 @@ public final class SelectStatementContext extends CommonSQLStatementContext<Sele
private Collection<TableSegment> getAllTablesFromProjections(final ProjectionsSegment projections) {
Collection<TableSegment> result = new LinkedList<>();
for (ProjectionSegment each : projections.getProjections()) {
if (each instanceof OwnerAvailable) {
Optional<OwnerSegment> owner = ((OwnerAvailable) each).getOwner();
if (owner.isPresent() && isTable(owner.get(), getSqlStatement().getTables())) {
result.add(new TableSegment(owner.get().getStartIndex(), owner.get().getStopIndex(), owner.get().getIdentifier()));
}
}
Optional<TableSegment> table = getTableSegment(each);
table.ifPresent(result::add);
}
return result;
}
private Optional<TableSegment> getTableSegment(final ProjectionSegment each) {
Optional<OwnerSegment> owner = getTableOwner(each);
if (owner.isPresent() && isTable(owner.get(), getSqlStatement().getTables())) {
return Optional .of(new TableSegment(owner.get().getStartIndex(), owner.get().getStopIndex(), owner.get().getIdentifier()));
}
return Optional.empty();
}
private Optional<OwnerSegment> getTableOwner(final ProjectionSegment each) {
if (each instanceof OwnerAvailable) {
return ((OwnerAvailable) each).getOwner();
}
if (each instanceof ColumnProjectionSegment) {
return ((ColumnProjectionSegment) each).getColumn().getOwner();
}
return Optional.empty();
}
private Collection<TableSegment> getAllTablesFromOrderByItems(final Collection<OrderByItemSegment> orderByItems) {
Collection<TableSegment> result = new LinkedList<>();
for (OrderByItemSegment each : orderByItems) {
......
......@@ -157,7 +157,7 @@ public final class ProjectionsContextEngineTest {
ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("col"));
columnSegment.setOwner(owner);
ColumnProjectionSegment columnProjectionSegment = new ColumnProjectionSegment("ColumnProjectionSegment", columnSegment);
columnProjectionSegment.setOwner(owner);
columnProjectionSegment.getColumn().setOwner(owner);
projectionsSegment.getProjections().addAll(Lists.newArrayList(columnProjectionSegment, shorthandProjectionSegment));
OrderByItem orderByItem = new OrderByItem(new ColumnOrderByItemSegment(new ColumnSegment(0, 0, new IdentifierValue("name")), OrderDirection.ASC));
OrderByContext orderByContext = new OrderByContext(Collections.singleton(orderByItem), false);
......
......@@ -413,9 +413,6 @@ public final class SQL92DMLVisitor extends SQL92Visitor implements DMLVisitor {
if (predicate.getRightValue() instanceof ColumnSegment && ((ColumnSegment) predicate.getRightValue()).getOwner().isPresent()) {
result.getValue().add(createTableSegment(((ColumnSegment) predicate.getRightValue()).getOwner().get()));
}
if (predicate.getRightValue() instanceof ColumnProjectionSegment && ((ColumnProjectionSegment) predicate.getRightValue()).getOwner().isPresent()) {
result.getValue().add(createTableSegment(((ColumnProjectionSegment) predicate.getRightValue()).getOwner().get()));
}
}
return result;
}
......
......@@ -436,9 +436,6 @@ public final class SQLServerDMLVisitor extends SQLServerVisitor implements DMLVi
if (predicate.getRightValue() instanceof ColumnSegment && ((ColumnSegment) predicate.getRightValue()).getOwner().isPresent()) {
result.getValue().add(createTableSegment(((ColumnSegment) predicate.getRightValue()).getOwner().get()));
}
if (predicate.getRightValue() instanceof ColumnProjectionSegment && ((ColumnProjectionSegment) predicate.getRightValue()).getOwner().isPresent()) {
result.getValue().add(createTableSegment(((ColumnProjectionSegment) predicate.getRightValue()).getOwner().get()));
}
}
return result;
}
......
......@@ -111,17 +111,19 @@ public final class ProjectionAssert {
}
private static void assertColumnProjection(final SQLCaseAssertContext assertContext, final ColumnProjectionSegment actual, final ExpectedColumnProjection expected) {
assertThat(assertContext.getText("Column projection name assertion error: "), actual.getIdentifier().getValue(), is(expected.getName()));
assertThat(assertContext.getText("Column projection start delimiter assertion error: "), actual.getIdentifier().getQuoteCharacter().getStartDelimiter(), is(expected.getStartDelimiter()));
assertThat(assertContext.getText("Column projection end delimiter assertion error: "), actual.getIdentifier().getQuoteCharacter().getEndDelimiter(), is(expected.getEndDelimiter()));
assertThat(assertContext.getText("Column projection name assertion error: "), actual.getColumn().getIdentifier().getValue(), is(expected.getName()));
assertThat(assertContext.getText("Column projection start delimiter assertion error: "),
actual.getColumn().getIdentifier().getQuoteCharacter().getStartDelimiter(), is(expected.getStartDelimiter()));
assertThat(assertContext.getText("Column projection end delimiter assertion error: "),
actual.getColumn().getIdentifier().getQuoteCharacter().getEndDelimiter(), is(expected.getEndDelimiter()));
assertThat(assertContext.getText("Column projection alias assertion error: "), actual.getAlias().orElse(null), is(expected.getAlias()));
if (null != expected.getOwner()) {
assertTrue(assertContext.getText("Actual owner should exist."), actual.getOwner().isPresent());
assertTrue(assertContext.getText("Actual owner should exist."), actual.getColumn().getOwner().isPresent());
// TODO OwnerAssert is needed.
OwnerSegment owner = actual.getOwner().get();
OwnerSegment owner = actual.getColumn().getOwner().get();
TableAssert.assertOwner(assertContext, new TableSegment(owner.getStartIndex(), owner.getStopIndex(), owner.getIdentifier()), expected.getOwner());
} else {
assertFalse(assertContext.getText("Actual owner should not exist."), actual.getOwner().isPresent());
assertFalse(assertContext.getText("Actual owner should not exist."), actual.getColumn().getOwner().isPresent());
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册