This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new c7f4fce1e70 Refactor AbstractShadowDMLStatementRouteEngine (#25901)
c7f4fce1e70 is described below
commit c7f4fce1e708def51fa4bad432e4c708180cfbf1
Author: Liang Zhang <[email protected]>
AuthorDate: Thu May 25 23:46:41 2023 +0800
Refactor AbstractShadowDMLStatementRouteEngine (#25901)
* Refactor AbstractShadowDMLStatementRouteEngine
* Refactor AbstractShadowDMLStatementRouteEngine
---
.../dml/AbstractShadowDMLStatementRouteEngine.java | 71 +++++++------
.../dml/ShadowDeleteStatementRoutingEngine.java | 30 ++----
.../dml/ShadowInsertStatementRoutingEngine.java | 29 ++----
.../dml/ShadowSelectStatementRoutingEngine.java | 30 ++----
.../dml/ShadowUpdateStatementRoutingEngine.java | 30 ++----
.../ShadowDeleteStatementRoutingEngineTest.java | 113 ---------------------
.../ShadowInsertStatementRoutingEngineTest.java | 109 --------------------
.../ShadowSelectStatementRoutingEngineTest.java | 108 --------------------
.../ShadowUpdateStatementRoutingEngineTest.java | 88 ----------------
9 files changed, 69 insertions(+), 539 deletions(-)
diff --git
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/AbstractShadowDMLStatementRouteEngine.java
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/AbstractShadowDMLStatementRouteEngine.java
index 1ff61eb2616..a59e2152280 100644
---
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/AbstractShadowDMLStatementRouteEngine.java
+++
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/AbstractShadowDMLStatementRouteEngine.java
@@ -17,7 +17,11 @@
package org.apache.shardingsphere.shadow.route.engine.dml;
+import lombok.AccessLevel;
import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.type.TableAvailable;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.shadow.api.shadow.ShadowOperationType;
import
org.apache.shardingsphere.shadow.api.shadow.column.ColumnShadowAlgorithm;
@@ -29,7 +33,10 @@ import
org.apache.shardingsphere.shadow.route.engine.determiner.ColumnShadowAlgo
import
org.apache.shardingsphere.shadow.route.engine.determiner.HintShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.CommentSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Collection;
import java.util.Collections;
@@ -37,18 +44,24 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
+import java.util.stream.Collectors;
/**
* Abstract shadow DML statement route engine.
*/
+@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public abstract class AbstractShadowDMLStatementRouteEngine implements
ShadowRouteEngine {
+ private final SQLStatementContext sqlStatementContext;
+
+ private final ShadowOperationType operationType;
+
private final Map<String, String> tableAliasNameMappings = new
LinkedHashMap<>();
@Override
public final void route(final RouteContext routeContext, final ShadowRule
rule) {
-
tableAliasNameMappings.putAll(getTableAliasNameMappings(getAllTables()));
+
tableAliasNameMappings.putAll(getTableAliasNameMappings(((TableAvailable)
sqlStatementContext).getAllTables()));
decorateRouteContext(routeContext, rule,
findShadowDataSourceMappings(rule));
}
@@ -64,32 +77,33 @@ public abstract class AbstractShadowDMLStatementRouteEngine
implements ShadowRou
private Map<String, String> findShadowDataSourceMappings(final ShadowRule
rule) {
Collection<String> relatedShadowTables =
rule.getRelatedShadowTables(tableAliasNameMappings.values());
- if (relatedShadowTables.isEmpty() && isMatchDefaultAlgorithm(rule)) {
+ Collection<String> sqlComments = getSQLComments();
+ if (relatedShadowTables.isEmpty() && isMatchDefaultAlgorithm(rule,
sqlComments)) {
return rule.getAllShadowDataSourceMappings();
}
- ShadowOperationType shadowOperationType = getShadowOperationType();
- Map<String, String> result = findBySQLComments(rule,
relatedShadowTables, shadowOperationType);
- return result.isEmpty() ? findByShadowColumn(rule,
relatedShadowTables, shadowOperationType) : result;
+ Map<String, String> result = findBySQLComments(rule, sqlComments,
relatedShadowTables);
+ return result.isEmpty() ? findByShadowColumn(rule,
relatedShadowTables) : result;
+ }
+
+ private Collection<String> getSQLComments() {
+ SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
+ return ((AbstractSQLStatement)
sqlStatement).getCommentSegments().stream().map(CommentSegment::getText).collect(Collectors.toList());
}
@SuppressWarnings("unchecked")
- private boolean isMatchDefaultAlgorithm(final ShadowRule rule) {
- Optional<Collection<String>> sqlComments = parseSQLComments();
- if (!sqlComments.isPresent()) {
- return false;
- }
+ private boolean isMatchDefaultAlgorithm(final ShadowRule rule, final
Collection<String> sqlComments) {
Optional<ShadowAlgorithm> defaultAlgorithm =
rule.getDefaultShadowAlgorithm();
if (defaultAlgorithm.isPresent() && defaultAlgorithm.get() instanceof
HintShadowAlgorithm<?>) {
ShadowDetermineCondition determineCondition = new
ShadowDetermineCondition("", ShadowOperationType.HINT_MATCH);
- return
HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>)
defaultAlgorithm.get(), determineCondition.initSQLComments(sqlComments.get()),
rule);
+ return
HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>)
defaultAlgorithm.get(), determineCondition.initSQLComments(sqlComments), rule);
}
return false;
}
- private Map<String, String> findBySQLComments(final ShadowRule rule, final
Collection<String> relatedShadowTables, final ShadowOperationType
shadowOperationType) {
+ private Map<String, String> findBySQLComments(final ShadowRule rule, final
Collection<String> sqlComments, final Collection<String> relatedShadowTables) {
Map<String, String> result = new LinkedHashMap<>();
for (String each : relatedShadowTables) {
- if (isContainsShadowInSQLComments(rule, each, new
ShadowDetermineCondition(each, shadowOperationType))) {
+ if (isContainsShadowInSQLComments(rule, each, sqlComments, new
ShadowDetermineCondition(each, operationType))) {
result.putAll(rule.getRelatedShadowDataSourceMappings(each));
return result;
}
@@ -97,40 +111,37 @@ public abstract class
AbstractShadowDMLStatementRouteEngine implements ShadowRou
return result;
}
- private boolean isContainsShadowInSQLComments(final ShadowRule rule, final
String tableName, final ShadowDetermineCondition shadowCondition) {
- return parseSQLComments().filter(each ->
isMatchAnyHintShadowAlgorithms(rule, tableName,
shadowCondition.initSQLComments(each))).isPresent();
- }
-
- private boolean isMatchAnyHintShadowAlgorithms(final ShadowRule rule,
final String tableName, final ShadowDetermineCondition shadowCondition) {
+ private boolean isContainsShadowInSQLComments(final ShadowRule rule, final
String tableName, final Collection<String> sqlComments, final
ShadowDetermineCondition shadowCondition) {
+ ShadowDetermineCondition shadowConditionWithComments =
shadowCondition.initSQLComments(sqlComments);
for (HintShadowAlgorithm<Comparable<?>> each :
rule.getRelatedHintShadowAlgorithms(tableName)) {
- if (HintShadowAlgorithmDeterminer.isShadow(each, shadowCondition,
rule)) {
+ if (HintShadowAlgorithmDeterminer.isShadow(each,
shadowConditionWithComments, rule)) {
return true;
}
}
return false;
}
- private Map<String, String> findByShadowColumn(final ShadowRule rule,
final Collection<String> relatedShadowTables, final ShadowOperationType
shadowOperationType) {
+ private Map<String, String> findByShadowColumn(final ShadowRule rule,
final Collection<String> relatedShadowTables) {
for (String each : relatedShadowTables) {
- Collection<String> relatedShadowColumnNames =
rule.getRelatedShadowColumnNames(shadowOperationType, each);
- if (!relatedShadowColumnNames.isEmpty() &&
isMatchAnyColumnShadowAlgorithms(rule, each, relatedShadowColumnNames,
shadowOperationType)) {
+ Collection<String> relatedShadowColumnNames =
rule.getRelatedShadowColumnNames(operationType, each);
+ if (!relatedShadowColumnNames.isEmpty() &&
isMatchAnyColumnShadowAlgorithms(rule, each, relatedShadowColumnNames)) {
return rule.getRelatedShadowDataSourceMappings(each);
}
}
return Collections.emptyMap();
}
- private boolean isMatchAnyColumnShadowAlgorithms(final ShadowRule rule,
final String shadowTable, final Collection<String> shadowColumnNames, final
ShadowOperationType shadowOperation) {
+ private boolean isMatchAnyColumnShadowAlgorithms(final ShadowRule rule,
final String shadowTable, final Collection<String> shadowColumnNames) {
for (String each : shadowColumnNames) {
- if (isMatchAnyColumnShadowAlgorithms(rule, shadowTable, each,
shadowOperation)) {
+ if (isMatchAnyColumnShadowAlgorithms(rule, shadowTable, each)) {
return true;
}
}
return false;
}
- private boolean isMatchAnyColumnShadowAlgorithms(final ShadowRule rule,
final String shadowTable, final String shadowColumn, final ShadowOperationType
shadowOperationType) {
- Collection<ColumnShadowAlgorithm<Comparable<?>>>
columnShadowAlgorithms =
rule.getRelatedColumnShadowAlgorithms(shadowOperationType, shadowTable,
shadowColumn);
+ private boolean isMatchAnyColumnShadowAlgorithms(final ShadowRule rule,
final String shadowTable, final String shadowColumn) {
+ Collection<ColumnShadowAlgorithm<Comparable<?>>>
columnShadowAlgorithms = rule.getRelatedColumnShadowAlgorithms(operationType,
shadowTable, shadowColumn);
if (columnShadowAlgorithms.isEmpty()) {
return false;
}
@@ -142,7 +153,7 @@ public abstract class AbstractShadowDMLStatementRouteEngine
implements ShadowRou
continue;
}
for (ColumnShadowAlgorithm<Comparable<?>> each :
columnShadowAlgorithms) {
- shadowDetermineCondition = new
ShadowDetermineCondition(shadowTable, shadowOperationType);
+ shadowDetermineCondition = new
ShadowDetermineCondition(shadowTable, operationType);
if (ColumnShadowAlgorithmDeterminer.isShadow(each,
shadowDetermineCondition.initShadowColumnCondition(next.get()))) {
return true;
}
@@ -151,12 +162,6 @@ public abstract class
AbstractShadowDMLStatementRouteEngine implements ShadowRou
return false;
}
- protected abstract Collection<SimpleTableSegment> getAllTables();
-
- protected abstract ShadowOperationType getShadowOperationType();
-
- protected abstract Optional<Collection<String>> parseSQLComments();
-
protected abstract Iterator<Optional<ShadowColumnCondition>>
getShadowColumnConditionIterator(String shadowColumn);
protected final String getSingleTableName() {
diff --git
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngine.java
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngine.java
index 196c7cf9adf..b96d6b7fd16 100644
---
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngine.java
+++
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngine.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.shadow.route.engine.dml;
-import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.infra.binder.statement.dml.DeleteStatementContext;
import org.apache.shardingsphere.shadow.api.shadow.ShadowOperationType;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
@@ -26,7 +25,6 @@ import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.Column
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.AndPredicate;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionExtractUtils;
import java.util.Collection;
@@ -38,38 +36,26 @@ import java.util.Optional;
/**
* Shadow delete statement routing engine.
*/
-@RequiredArgsConstructor
public final class ShadowDeleteStatementRoutingEngine extends
AbstractShadowDMLStatementRouteEngine {
- private final DeleteStatementContext deleteStatementContext;
+ private final DeleteStatementContext sqlStatementContext;
private final List<Object> parameters;
- @Override
- protected Collection<SimpleTableSegment> getAllTables() {
- return deleteStatementContext.getAllTables();
- }
-
- @Override
- protected ShadowOperationType getShadowOperationType() {
- return ShadowOperationType.DELETE;
- }
-
- @Override
- protected Optional<Collection<String>> parseSQLComments() {
- Collection<String> result = new LinkedList<>();
-
deleteStatementContext.getSqlStatement().getCommentSegments().forEach(each ->
result.add(each.getText()));
- return result.isEmpty() ? Optional.empty() : Optional.of(result);
+ public ShadowDeleteStatementRoutingEngine(final DeleteStatementContext
sqlStatementContext, final List<Object> parameters) {
+ super(sqlStatementContext, ShadowOperationType.DELETE);
+ this.sqlStatementContext = sqlStatementContext;
+ this.parameters = parameters;
}
@Override
protected Iterator<Optional<ShadowColumnCondition>>
getShadowColumnConditionIterator(final String shadowColumn) {
- return new ShadowColumnConditionIterator(shadowColumn,
parseWhereSegment());
+ return new ShadowColumnConditionIterator(shadowColumn,
getWhereSegment());
}
- private Collection<ExpressionSegment> parseWhereSegment() {
+ private Collection<ExpressionSegment> getWhereSegment() {
Collection<ExpressionSegment> result = new LinkedList<>();
- for (WhereSegment each : deleteStatementContext.getWhereSegments()) {
+ for (WhereSegment each : sqlStatementContext.getWhereSegments()) {
for (AndPredicate predicate :
ExpressionExtractUtils.getAndPredicates(each.getExpr())) {
result.addAll(predicate.getPredicates());
}
diff --git
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowInsertStatementRoutingEngine.java
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowInsertStatementRoutingEngine.java
index 75c5122d1d5..bcb8e3b1439 100644
---
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowInsertStatementRoutingEngine.java
+++
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowInsertStatementRoutingEngine.java
@@ -17,13 +17,11 @@
package org.apache.shardingsphere.shadow.route.engine.dml;
-import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.infra.binder.segment.insert.values.InsertValueContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.shadow.api.shadow.ShadowOperationType;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
import
org.apache.shardingsphere.shadow.exception.syntax.UnsupportedShadowInsertValueException;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import java.util.Collection;
import java.util.Iterator;
@@ -34,35 +32,22 @@ import java.util.Optional;
/**
* Shadow insert statement routing engine.
*/
-@RequiredArgsConstructor
public final class ShadowInsertStatementRoutingEngine extends
AbstractShadowDMLStatementRouteEngine {
- private final InsertStatementContext insertStatementContext;
+ private final InsertStatementContext sqlStatementContext;
- @Override
- protected Collection<SimpleTableSegment> getAllTables() {
- return insertStatementContext.getAllTables();
- }
-
- @Override
- protected ShadowOperationType getShadowOperationType() {
- return ShadowOperationType.INSERT;
- }
-
- @Override
- protected Optional<Collection<String>> parseSQLComments() {
- Collection<String> result = new LinkedList<>();
-
insertStatementContext.getSqlStatement().getCommentSegments().forEach(each ->
result.add(each.getText()));
- return result.isEmpty() ? Optional.empty() : Optional.of(result);
+ public ShadowInsertStatementRoutingEngine(final InsertStatementContext
sqlStatementContext) {
+ super(sqlStatementContext, ShadowOperationType.INSERT);
+ this.sqlStatementContext = sqlStatementContext;
}
@Override
protected Iterator<Optional<ShadowColumnCondition>>
getShadowColumnConditionIterator(final String shadowColumn) {
- return new ShadowColumnConditionIterator(shadowColumn,
parseColumnNames().iterator(), insertStatementContext.getInsertValueContexts());
+ return new ShadowColumnConditionIterator(shadowColumn,
getColumnNames().iterator(), sqlStatementContext.getInsertValueContexts());
}
- private Collection<String> parseColumnNames() {
- return insertStatementContext.getInsertColumnNames();
+ private Collection<String> getColumnNames() {
+ return sqlStatementContext.getInsertColumnNames();
}
private final class ShadowColumnConditionIterator implements
Iterator<Optional<ShadowColumnCondition>> {
diff --git
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowSelectStatementRoutingEngine.java
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowSelectStatementRoutingEngine.java
index e81455219f4..c951c9803ac 100644
---
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowSelectStatementRoutingEngine.java
+++
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowSelectStatementRoutingEngine.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.shadow.route.engine.dml;
-import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.shadow.api.shadow.ShadowOperationType;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
@@ -27,7 +26,6 @@ import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.Expressi
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.AndPredicate;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionExtractUtils;
import java.util.Collection;
@@ -39,38 +37,26 @@ import java.util.Optional;
/**
* Shadow select statement routing engine.
*/
-@RequiredArgsConstructor
public final class ShadowSelectStatementRoutingEngine extends
AbstractShadowDMLStatementRouteEngine {
- private final SelectStatementContext selectStatementContext;
+ private final SelectStatementContext sqlStatementContext;
private final List<Object> parameters;
- @Override
- protected Collection<SimpleTableSegment> getAllTables() {
- return selectStatementContext.getAllTables();
- }
-
- @Override
- protected ShadowOperationType getShadowOperationType() {
- return ShadowOperationType.SELECT;
- }
-
- @Override
- protected Optional<Collection<String>> parseSQLComments() {
- Collection<String> result = new LinkedList<>();
-
selectStatementContext.getSqlStatement().getCommentSegments().forEach(each ->
result.add(each.getText()));
- return result.isEmpty() ? Optional.empty() : Optional.of(result);
+ public ShadowSelectStatementRoutingEngine(final SelectStatementContext
sqlStatementContext, final List<Object> parameters) {
+ super(sqlStatementContext, ShadowOperationType.SELECT);
+ this.sqlStatementContext = sqlStatementContext;
+ this.parameters = parameters;
}
@Override
protected Iterator<Optional<ShadowColumnCondition>>
getShadowColumnConditionIterator(final String shadowColumn) {
- return new ShadowColumnConditionIterator(shadowColumn,
parseWhereSegment());
+ return new ShadowColumnConditionIterator(shadowColumn,
getWhereSegment());
}
- private Collection<ExpressionSegment> parseWhereSegment() {
+ private Collection<ExpressionSegment> getWhereSegment() {
Collection<ExpressionSegment> result = new LinkedList<>();
- for (WhereSegment each : selectStatementContext.getWhereSegments()) {
+ for (WhereSegment each : sqlStatementContext.getWhereSegments()) {
for (AndPredicate predicate :
ExpressionExtractUtils.getAndPredicates(each.getExpr())) {
result.addAll(predicate.getPredicates());
}
diff --git
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngine.java
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngine.java
index 5dac45ed076..526776da839 100644
---
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngine.java
+++
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngine.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.shadow.route.engine.dml;
-import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementContext;
import org.apache.shardingsphere.shadow.api.shadow.ShadowOperationType;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
@@ -26,7 +25,6 @@ import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.Column
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.AndPredicate;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionExtractUtils;
import java.util.Collection;
@@ -38,38 +36,26 @@ import java.util.Optional;
/**
* Shadow update statement routing engine.
*/
-@RequiredArgsConstructor
public final class ShadowUpdateStatementRoutingEngine extends
AbstractShadowDMLStatementRouteEngine {
- private final UpdateStatementContext updateStatementContext;
+ private final UpdateStatementContext sqlStatementContext;
private final List<Object> parameters;
- @Override
- protected Collection<SimpleTableSegment> getAllTables() {
- return updateStatementContext.getAllTables();
- }
-
- @Override
- protected ShadowOperationType getShadowOperationType() {
- return ShadowOperationType.UPDATE;
- }
-
- @Override
- protected Optional<Collection<String>> parseSQLComments() {
- Collection<String> result = new LinkedList<>();
-
updateStatementContext.getSqlStatement().getCommentSegments().forEach(each ->
result.add(each.getText()));
- return result.isEmpty() ? Optional.empty() : Optional.of(result);
+ public ShadowUpdateStatementRoutingEngine(final UpdateStatementContext
sqlStatementContext, final List<Object> parameters) {
+ super(sqlStatementContext, ShadowOperationType.UPDATE);
+ this.sqlStatementContext = sqlStatementContext;
+ this.parameters = parameters;
}
@Override
protected Iterator<Optional<ShadowColumnCondition>>
getShadowColumnConditionIterator(final String shadowColumn) {
- return new ShadowColumnConditionIterator(shadowColumn,
parseWhereSegment());
+ return new ShadowColumnConditionIterator(shadowColumn,
getWhereSegment());
}
- private Collection<ExpressionSegment> parseWhereSegment() {
+ private Collection<ExpressionSegment> getWhereSegment() {
Collection<ExpressionSegment> result = new LinkedList<>();
- for (WhereSegment each : updateStatementContext.getWhereSegments()) {
+ for (WhereSegment each : sqlStatementContext.getWhereSegments()) {
for (AndPredicate predicate :
ExpressionExtractUtils.getAndPredicates(each.getExpr())) {
result.addAll(predicate.getPredicates());
}
diff --git
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngineTest.java
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngineTest.java
deleted file mode 100644
index b94a801f8f4..00000000000
---
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngineTest.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-
-package org.apache.shardingsphere.shadow.route.engine.dml;
-
-import
org.apache.shardingsphere.infra.binder.statement.dml.DeleteStatementContext;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
-import org.apache.shardingsphere.infra.route.context.RouteUnit;
-import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
-import
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
-import
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.CommentSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
-import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLDeleteStatement;
-import org.apache.shardingsphere.test.util.PropertiesBuilder;
-import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.Optional;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-class ShadowDeleteStatementRoutingEngineTest {
-
- private ShadowDeleteStatementRoutingEngine
shadowDeleteStatementRoutingEngine;
-
- @BeforeEach
- void init() {
- shadowDeleteStatementRoutingEngine = new
ShadowDeleteStatementRoutingEngine(createDeleteStatementContext(),
Collections.emptyList());
- }
-
- private DeleteStatementContext createDeleteStatementContext() {
- DeleteStatementContext result = mock(DeleteStatementContext.class);
- Collection<SimpleTableSegment> allTables = new LinkedList<>();
- allTables.add(new SimpleTableSegment(new TableNameSegment(20, 25, new
IdentifierValue("t_order"))));
- when(result.getAllTables()).thenReturn(allTables);
- BinaryOperationExpression binaryOperationExpression =
mock(BinaryOperationExpression.class);
- when(binaryOperationExpression.getLeft()).thenReturn(new
ColumnSegment(0, 0, new IdentifierValue("user_id")));
- when(binaryOperationExpression.getRight()).thenReturn(new
LiteralExpressionSegment(0, 0, "1"));
- WhereSegment whereSegment = new WhereSegment(0, 0,
binaryOperationExpression);
-
when(result.getWhereSegments()).thenReturn(Collections.singletonList(whereSegment));
- MySQLDeleteStatement deleteStatement = new MySQLDeleteStatement();
- deleteStatement.getCommentSegments().add(new
CommentSegment("/*shadow:true,foo:bar*/", 0, 20));
- deleteStatement.getCommentSegments().add(new
CommentSegment("/*aaa:bbb*/", 21, 30));
- when(result.getSqlStatement()).thenReturn(deleteStatement);
- return result;
- }
-
- @Test
- void assertRouteAndParseShadowColumnConditions() {
- RouteContext routeContext = mock(RouteContext.class);
-
when(routeContext.getRouteUnits()).thenReturn(Collections.singleton(new
RouteUnit(new RouteMapper("ds", "ds_shadow"), Collections.emptyList())));
- shadowDeleteStatementRoutingEngine.route(routeContext, new
ShadowRule(createShadowRuleConfiguration()));
- Optional<Collection<String>> sqlNotes =
shadowDeleteStatementRoutingEngine.parseSQLComments();
- assertTrue(sqlNotes.isPresent());
- assertThat(sqlNotes.get().size(), is(2));
- Iterator<String> sqlNotesIt = sqlNotes.get().iterator();
- assertThat(sqlNotesIt.next(), is("/*shadow:true,foo:bar*/"));
- assertThat(sqlNotesIt.next(), is("/*aaa:bbb*/"));
- }
-
- private ShadowRuleConfiguration createShadowRuleConfiguration() {
- ShadowRuleConfiguration result = new ShadowRuleConfiguration();
- result.setDataSources(Collections.singletonList(new
ShadowDataSourceConfiguration("shadow-data-source-0", "ds", "ds_shadow")));
- result.setTables(
- Collections.singletonMap("t_order", new
ShadowTableConfiguration(Collections.singletonList("shadow-data-source-0"),
Collections.singleton("user-id-delete-regex-algorithm"))));
-
result.setShadowAlgorithms(Collections.singletonMap("user-id-delete-regex-algorithm",
createShadowAlgorithm()));
- return result;
- }
-
- private AlgorithmConfiguration createShadowAlgorithm() {
- return new AlgorithmConfiguration("REGEX_MATCH",
PropertiesBuilder.build(new Property("column", "user_id"), new
Property("operation", "delete"), new Property("regex", "[1]")));
- }
-
- @Test
- void assertGetAllTables() {
- Collection<SimpleTableSegment> allTables =
shadowDeleteStatementRoutingEngine.getAllTables();
- assertThat(allTables.size(), is(1));
-
assertThat(allTables.iterator().next().getTableName().getIdentifier().getValue(),
is("t_order"));
- }
-}
diff --git
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowInsertStatementRoutingEngineTest.java
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowInsertStatementRoutingEngineTest.java
deleted file mode 100644
index f766d0c6fda..00000000000
---
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowInsertStatementRoutingEngineTest.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-
-package org.apache.shardingsphere.shadow.route.engine.dml;
-
-import
org.apache.shardingsphere.infra.binder.segment.insert.values.InsertValueContext;
-import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
-import org.apache.shardingsphere.infra.route.context.RouteUnit;
-import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
-import
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
-import
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.CommentSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
-import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement;
-import org.apache.shardingsphere.test.util.PropertiesBuilder;
-import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Optional;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-class ShadowInsertStatementRoutingEngineTest {
-
- private ShadowInsertStatementRoutingEngine shadowRouteEngine;
-
- @BeforeEach
- void init() {
- shadowRouteEngine = new
ShadowInsertStatementRoutingEngine(createInsertStatementContext());
- }
-
- private InsertStatementContext createInsertStatementContext() {
- InsertStatementContext result = mock(InsertStatementContext.class);
- when(result.getAllTables()).thenReturn(Collections.singleton(new
SimpleTableSegment(new TableNameSegment(20, 25, new
IdentifierValue("t_order")))));
-
when(result.getInsertColumnNames()).thenReturn(Arrays.asList("user_id",
"order_code", "order_name"));
- List<ExpressionSegment> valueExpressions = Arrays.asList(new
LiteralExpressionSegment(0, 10, "1"),
- new LiteralExpressionSegment(11, 20, "orderCode"), new
LiteralExpressionSegment(21, 30, "orderName"));
-
when(result.getInsertValueContexts()).thenReturn(Collections.singletonList(new
InsertValueContext(valueExpressions, Collections.emptyList(), 0)));
- MySQLInsertStatement insertStatement = new MySQLInsertStatement();
- insertStatement.getCommentSegments().add(new
CommentSegment("/*shadow:true,foo:bar*/", 0, 20));
- insertStatement.getCommentSegments().add(new
CommentSegment("/*aaa:bbb*/", 21, 30));
- when(result.getSqlStatement()).thenReturn(insertStatement);
- return result;
- }
-
- @Test
- void assertRouteAndParseShadowColumnConditions() {
- RouteContext routeContext = mock(RouteContext.class);
-
when(routeContext.getRouteUnits()).thenReturn(Collections.singleton(new
RouteUnit(new RouteMapper("ds", "ds_shadow"), Collections.emptyList())));
- shadowRouteEngine.route(routeContext, new
ShadowRule(createShadowRuleConfiguration()));
- Optional<Collection<String>> sqlNotes =
shadowRouteEngine.parseSQLComments();
- assertTrue(sqlNotes.isPresent());
- assertThat(sqlNotes.get().size(), is(2));
- Iterator<String> sqlNotesIt = sqlNotes.get().iterator();
- assertThat(sqlNotesIt.next(), is("/*shadow:true,foo:bar*/"));
- assertThat(sqlNotesIt.next(), is("/*aaa:bbb*/"));
- }
-
- private ShadowRuleConfiguration createShadowRuleConfiguration() {
- ShadowRuleConfiguration result = new ShadowRuleConfiguration();
- result.setDataSources(Collections.singletonList(new
ShadowDataSourceConfiguration("shadow-data-source-0", "ds", "ds_shadow")));
- result.setTables(Collections.singletonMap("t_order", new
ShadowTableConfiguration(Collections.singleton("shadow-data-source-0"),
Collections.singleton("user-id-insert-regex-algorithm"))));
-
result.setShadowAlgorithms(Collections.singletonMap("user-id-insert-regex-algorithm",
createShadowAlgorithm()));
- return result;
- }
-
- private AlgorithmConfiguration createShadowAlgorithm() {
- return new AlgorithmConfiguration("REGEX_MATCH",
PropertiesBuilder.build(new Property("column", "user_id"), new
Property("operation", "insert"), new Property("regex", "[1]")));
- }
-
- @Test
- void assertGetAllTables() {
- Collection<SimpleTableSegment> actual =
shadowRouteEngine.getAllTables();
- assertThat(actual.size(), is(1));
-
assertThat(actual.iterator().next().getTableName().getIdentifier().getValue(),
is("t_order"));
- }
-}
diff --git
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowSelectStatementRoutingEngineTest.java
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowSelectStatementRoutingEngineTest.java
deleted file mode 100644
index e6a74d8a062..00000000000
---
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowSelectStatementRoutingEngineTest.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-
-package org.apache.shardingsphere.shadow.route.engine.dml;
-
-import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
-import org.apache.shardingsphere.infra.route.context.RouteUnit;
-import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
-import
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
-import
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.CommentSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
-import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
-import org.apache.shardingsphere.test.util.PropertiesBuilder;
-import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.Optional;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-class ShadowSelectStatementRoutingEngineTest {
-
- private ShadowSelectStatementRoutingEngine shadowRouteEngine;
-
- @BeforeEach
- void init() {
- shadowRouteEngine = new
ShadowSelectStatementRoutingEngine(createSelectStatementContext(),
Collections.emptyList());
- }
-
- private SelectStatementContext createSelectStatementContext() {
- SelectStatementContext result = mock(SelectStatementContext.class);
- when(result.getAllTables()).thenReturn(Collections.singleton(new
SimpleTableSegment(new TableNameSegment(20, 25, new
IdentifierValue("t_order")))));
- BinaryOperationExpression binaryOperationExpression =
mock(BinaryOperationExpression.class);
- when(binaryOperationExpression.getLeft()).thenReturn(new
ColumnSegment(0, 0, new IdentifierValue("user_id")));
- when(binaryOperationExpression.getRight()).thenReturn(new
LiteralExpressionSegment(0, 0, "1"));
- when(result.getWhereSegments()).thenReturn(Collections.singleton(new
WhereSegment(0, 0, binaryOperationExpression)));
- MySQLSelectStatement selectStatement = new MySQLSelectStatement();
- selectStatement.getCommentSegments().add(new
CommentSegment("/*shadow:true,foo:bar*/", 0, 20));
- selectStatement.getCommentSegments().add(new
CommentSegment("/*aaa:bbb*/", 21, 30));
- when(result.getSqlStatement()).thenReturn(selectStatement);
- return result;
- }
-
- @Test
- void assertRouteAndParseShadowColumnConditions() {
- RouteContext routeContext = mock(RouteContext.class);
-
when(routeContext.getRouteUnits()).thenReturn(Collections.singleton(new
RouteUnit(new RouteMapper("ds", "ds_shadow"), Collections.emptyList())));
- shadowRouteEngine.route(routeContext, new
ShadowRule(createShadowRuleConfiguration()));
- Optional<Collection<String>> sqlNotes =
shadowRouteEngine.parseSQLComments();
- assertTrue(sqlNotes.isPresent());
- assertThat(sqlNotes.get().size(), is(2));
- Iterator<String> sqlNotesIt = sqlNotes.get().iterator();
- assertThat(sqlNotesIt.next(), is("/*shadow:true,foo:bar*/"));
- assertThat(sqlNotesIt.next(), is("/*aaa:bbb*/"));
- }
-
- private ShadowRuleConfiguration createShadowRuleConfiguration() {
- ShadowRuleConfiguration result = new ShadowRuleConfiguration();
- result.setDataSources(Collections.singletonList(new
ShadowDataSourceConfiguration("shadow-data-source-0", "ds", "ds_shadow")));
- result.setTables(Collections.singletonMap("t_order", new
ShadowTableConfiguration(Collections.singleton("shadow-data-source-0"),
Collections.singleton("user-id-select-regex-algorithm"))));
-
result.setShadowAlgorithms(Collections.singletonMap("user-id-select-regex-algorithm",
createShadowAlgorithm()));
- return result;
- }
-
- private AlgorithmConfiguration createShadowAlgorithm() {
- return new AlgorithmConfiguration("REGEX_MATCH",
PropertiesBuilder.build(new Property("column", "user_id"), new
Property("operation", "select"), new Property("regex", "[1]")));
- }
-
- @Test
- void assertGetAllTables() {
- Collection<SimpleTableSegment> actual =
shadowRouteEngine.getAllTables();
- assertThat(actual.size(), is(1));
-
assertThat(actual.iterator().next().getTableName().getIdentifier().getValue(),
is("t_order"));
- }
-}
diff --git
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngineTest.java
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngineTest.java
deleted file mode 100644
index eaa7d329db5..00000000000
---
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngineTest.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-
-package org.apache.shardingsphere.shadow.route.engine.dml;
-
-import
org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementContext;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
-import org.apache.shardingsphere.infra.route.context.RouteUnit;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.CommentSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
-import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLUpdateStatement;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.Optional;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-class ShadowUpdateStatementRoutingEngineTest {
-
- private ShadowUpdateStatementRoutingEngine shadowRouteEngine;
-
- @BeforeEach
- void init() {
- shadowRouteEngine = new
ShadowUpdateStatementRoutingEngine(createUpdateStatementContext(),
Collections.emptyList());
- }
-
- private UpdateStatementContext createUpdateStatementContext() {
- UpdateStatementContext result = mock(UpdateStatementContext.class);
- when(result.getAllTables()).thenReturn(Collections.singleton(new
SimpleTableSegment(new TableNameSegment(20, 25, new
IdentifierValue("t_order")))));
- BinaryOperationExpression binaryOperationExpression =
mock(BinaryOperationExpression.class);
- when(binaryOperationExpression.getLeft()).thenReturn(new
ColumnSegment(0, 0, new IdentifierValue("user_id")));
- when(binaryOperationExpression.getRight()).thenReturn(new
LiteralExpressionSegment(0, 0, "1"));
- when(result.getWhereSegments()).thenReturn(Collections.singleton(new
WhereSegment(0, 0, binaryOperationExpression)));
- MySQLUpdateStatement sqlStatement = new MySQLUpdateStatement();
- sqlStatement.getCommentSegments().add(new
CommentSegment("/*shadow:true,foo:bar*/", 0, 20));
- sqlStatement.getCommentSegments().add(new
CommentSegment("/*aaa:bbb*/", 21, 30));
- when(result.getSqlStatement()).thenReturn(sqlStatement);
- return result;
- }
-
- @Test
- void assertRouteAndParseShadowColumnConditions() {
- RouteContext routeContext = mock(RouteContext.class);
-
when(routeContext.getRouteUnits()).thenReturn(Collections.singleton(new
RouteUnit(new RouteMapper("ds", "ds_shadow"), Collections.emptyList())));
- Optional<Collection<String>> sqlNotes =
shadowRouteEngine.parseSQLComments();
- assertTrue(sqlNotes.isPresent());
- assertThat(sqlNotes.get().size(), is(2));
- Iterator<String> sqlNotesIt = sqlNotes.get().iterator();
- assertThat(sqlNotesIt.next(), is("/*shadow:true,foo:bar*/"));
- assertThat(sqlNotesIt.next(), is("/*aaa:bbb*/"));
- }
-
- @Test
- void assertGetAllTables() {
- Collection<SimpleTableSegment> actual =
shadowRouteEngine.getAllTables();
- assertThat(actual.size(), is(1));
-
assertThat(actual.iterator().next().getTableName().getIdentifier().getValue(),
is("t_order"));
- }
-}