This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 645b8d6b400 Condition engine refactoring (#22857)
645b8d6b400 is described below
commit 645b8d6b4009fcfa42b2b370b1692d85b8d82f85
Author: Vignesh.V.B <[email protected]>
AuthorDate: Tue Jan 3 08:07:34 2023 +0530
Condition engine refactoring (#22857)
* Changes
* Checkstyle changes
* Service loader file
* Registered the ShardingConditionEngine type in the service loader
* Testcase fixes
* Review comments
* Removed blank lines
* Review changes
* Doc changes
* Upstream merge fixes
---
docs/document/content/dev-manual/sharding.cn.md | 16 +++++
docs/document/content/dev-manual/sharding.en.md | 16 +++++
.../condition/engine/ShardingConditionEngine.java | 13 +++-
.../engine/ShardingConditionEngineFactory.java | 10 +--
.../impl/DefaultShardingConditionEngine.java | 51 ++++++++++++++
.../impl/InsertClauseShardingConditionEngine.java | 11 ++-
.../impl/WhereClauseShardingConditionEngine.java | 11 ++-
...engine.condition.engine.ShardingConditionEngine | 18 +++++
.../decider/ShardingSQLFederationDeciderTest.java | 5 +-
.../engine/ShardingConditionEngineFactoryTest.java | 80 ++++++++++++++++------
10 files changed, 197 insertions(+), 34 deletions(-)
diff --git a/docs/document/content/dev-manual/sharding.cn.md
b/docs/document/content/dev-manual/sharding.cn.md
index 60dac120dc4..28ead0899f7 100644
--- a/docs/document/content/dev-manual/sharding.cn.md
+++ b/docs/document/content/dev-manual/sharding.cn.md
@@ -85,3 +85,19 @@ chapter = true
| ----------------------- |
------------------------------------------------------------ |
---------------------------- |
| DatabaseDatetimeService | 从数据库中获取当前时间进行路由 |
[`org.apache.shardingsphere.agent.metrics.prometheus.service.PrometheusPluginBootService`](https://github.com/apache/shardingsphere/blob/master/infra/datetime/type/database/src/main/java/org/apache/shardingsphere/datetime/database/DatabaseDatetimeService.java)
|
| SystemDatetime | 从应用系统时间中获取当前时间进行路由 |
[`org.apache.shardingsphere.datetime.system.SystemDatetimeService`](https://github.com/apache/shardingsphere/blob/master/infra/datetime/type/system/src/main/java/org/apache/shardingsphere/datetime/system/SystemDatetimeService.java)
|
+
+## ShardingConditionEngine
+
+### 全限定类名
+
+[`org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine`](https://github.com/apache/shardingsphere/blob/master/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngine.java)
+
+### 定义
+
+Sharding condition generator definition
+
+### 已知实现
+
+| *配置标识* | *详细说明*
| *全限定类名*
|
+|--------------------------------|-------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| DefaultShardingConditionEngine | Default implementation that generates the
sharding conditions from the configured sharding rule |
[`org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.DefaultShardingConditionEngine`](https://github.com/apache/shardingsphere/blob/master/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/DefaultShardingConditionEngine.java)
|
diff --git a/docs/document/content/dev-manual/sharding.en.md
b/docs/document/content/dev-manual/sharding.en.md
index 5c7adf149d6..89423844b64 100644
--- a/docs/document/content/dev-manual/sharding.en.md
+++ b/docs/document/content/dev-manual/sharding.en.md
@@ -85,3 +85,19 @@ Obtain the current date for routing definition
| ----------------------- |
------------------------------------------------------------ |
---------------------------- |
| DatabaseDatetimeService | Get the current time from the database for routing
|
[`org.apache.shardingsphere.agent.metrics.prometheus.service.PrometheusPluginBootService`](https://github.com/apache/shardingsphere/blob/master/infra/datetime/type/database/src/main/java/org/apache/shardingsphere/datetime/database/DatabaseDatetimeService.java)
|
| SystemDatetime | Get the current time from the application system
for routing |
[`org.apache.shardingsphere.datetime.system.SystemDatetimeService`](https://github.com/apache/shardingsphere/blob/master/infra/datetime/type/system/src/main/java/org/apache/shardingsphere/datetime/system/SystemDatetimeService.java)
|
+
+## ShardingConditionEngine
+
+### Fully-qualified class name
+
+[`org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine`](https://github.com/apache/shardingsphere/blob/master/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngine.java)
+
+### Definition
+
+Sharding condition generator definition
+
+### Implementation classes
+
+| *Configuration Type* | *Description*
| *Fully-qualified class
name*
|
+|--------------------------------|-------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| DefaultShardingConditionEngine | Default implementation that generates the
sharding conditions from the configured sharding rule |
[`org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.DefaultShardingConditionEngine`](https://github.com/apache/shardingsphere/blob/master/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/DefaultShardingConditionEngine.java)
|
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngine.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngine.java
index c0f100b2b9b..05bb65c3e46 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngine.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngine.java
@@ -21,11 +21,22 @@ import
org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditi
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import java.util.List;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.util.spi.type.required.RequiredSPI;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
/**
* Sharding condition engine.
*/
-public interface ShardingConditionEngine<T extends SQLStatementContext<?>> {
+public interface ShardingConditionEngine<T extends SQLStatementContext<?>>
extends RequiredSPI {
+
+ /**
+ * Initialize the sharding condition engine.
+ *
+ * @param rule sharding rule
+ * @param database sharding database
+ */
+ void init(ShardingRule rule, ShardingSphereDatabase database);
/**
* Create sharding conditions.
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngineFactory.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngineFactory.java
index cc40e12c769..6156674a1dd 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngineFactory.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngineFactory.java
@@ -21,10 +21,8 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.binder.QueryContext;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.InsertClauseShardingConditionEngine;
-import
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.WhereClauseShardingConditionEngine;
+import
org.apache.shardingsphere.infra.util.spi.type.required.RequiredSPIRegistry;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
/**
@@ -38,7 +36,7 @@ public final class ShardingConditionEngineFactory {
*
* @param queryContext query context
* @param database database
- * @param rule sharding rule
+ * @param rule sharding rule
* @return created instance
*/
public static ShardingConditionEngine<?>
createShardingConditionEngine(final QueryContext queryContext, final
ShardingSphereDatabase database, final ShardingRule rule) {
@@ -54,6 +52,8 @@ public final class ShardingConditionEngineFactory {
* @return created instance
*/
public static ShardingConditionEngine<?>
createShardingConditionEngine(final SQLStatementContext<?> sqlStatementContext,
final ShardingSphereDatabase database, final ShardingRule rule) {
- return sqlStatementContext instanceof InsertStatementContext ? new
InsertClauseShardingConditionEngine(rule, database) : new
WhereClauseShardingConditionEngine(rule, database);
+ ShardingConditionEngine result =
RequiredSPIRegistry.getRegisteredService(ShardingConditionEngine.class);
+ result.init(rule, database);
+ return result;
}
}
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/DefaultShardingConditionEngine.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/DefaultShardingConditionEngine.java
new file mode 100644
index 00000000000..3cb1f400383
--- /dev/null
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/DefaultShardingConditionEngine.java
@@ -0,0 +1,51 @@
+/*
+ * 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.sharding.route.engine.condition.engine.impl;
+
+import java.util.List;
+
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import
org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
+import
org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+
+/**
+ * Default implementation of the sharding condition engine.
+ */
+public final class DefaultShardingConditionEngine implements
ShardingConditionEngine<SQLStatementContext<?>> {
+
+ private ShardingRule shardingRule;
+
+ private ShardingSphereDatabase database;
+
+ @Override
+ public void init(final ShardingRule shardingRule, final
ShardingSphereDatabase database) {
+ this.shardingRule = shardingRule;
+ this.database = database;
+ }
+
+ @Override
+ public List<ShardingCondition> createShardingConditions(final
SQLStatementContext<?> sqlStatementContext, final List<Object> parameters) {
+ if (sqlStatementContext instanceof InsertStatementContext) {
+ return new InsertClauseShardingConditionEngine(shardingRule,
database).createShardingConditions((InsertStatementContext)
sqlStatementContext, parameters);
+ }
+ return new WhereClauseShardingConditionEngine(shardingRule,
database).createShardingConditions(sqlStatementContext, parameters);
+ }
+}
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
index 2f224aebd56..d0be26c0485 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
@@ -30,7 +30,6 @@ import
org.apache.shardingsphere.infra.util.spi.type.required.RequiredSPIRegistr
import
org.apache.shardingsphere.sharding.exception.data.NullShardingValueException;
import
org.apache.shardingsphere.sharding.route.engine.condition.ExpressionConditionUtils;
import
org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
-import
org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine;
import
org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
@@ -53,13 +52,19 @@ import java.util.stream.IntStream;
* Sharding condition engine for insert clause.
*/
@RequiredArgsConstructor
-public final class InsertClauseShardingConditionEngine implements
ShardingConditionEngine<InsertStatementContext> {
+public final class InsertClauseShardingConditionEngine {
private final ShardingRule shardingRule;
private final ShardingSphereDatabase database;
- @Override
+ /**
+ * Create sharding conditions.
+ *
+ * @param sqlStatementContext SQL statement context
+ * @param params SQL parameters
+ * @return sharding conditions
+ */
public List<ShardingCondition> createShardingConditions(final
InsertStatementContext sqlStatementContext, final List<Object> params) {
List<ShardingCondition> result = null ==
sqlStatementContext.getInsertSelectContext()
?
createShardingConditionsWithInsertValues(sqlStatementContext, params)
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java
index 3374dbd6f03..0ba695c3b27 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java
@@ -28,7 +28,6 @@ import
org.apache.shardingsphere.sharding.exception.data.ShardingValueDataTypeEx
import
org.apache.shardingsphere.sharding.route.engine.condition.AlwaysFalseShardingCondition;
import org.apache.shardingsphere.sharding.route.engine.condition.Column;
import
org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
-import
org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine;
import
org.apache.shardingsphere.sharding.route.engine.condition.generator.ConditionValueGeneratorFactory;
import
org.apache.shardingsphere.sharding.route.engine.condition.value.AlwaysFalseShardingConditionValue;
import
org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
@@ -59,13 +58,19 @@ import java.util.Set;
* Sharding condition engine for where clause.
*/
@RequiredArgsConstructor
-public final class WhereClauseShardingConditionEngine implements
ShardingConditionEngine<SQLStatementContext<?>> {
+public final class WhereClauseShardingConditionEngine {
private final ShardingRule shardingRule;
private final ShardingSphereDatabase database;
- @Override
+ /**
+ * Create sharding conditions.
+ *
+ * @param sqlStatementContext SQL statement context
+ * @param params SQL parameters
+ * @return sharding conditions
+ */
public List<ShardingCondition> createShardingConditions(final
SQLStatementContext<?> sqlStatementContext, final List<Object> params) {
if (!(sqlStatementContext instanceof WhereAvailable)) {
return Collections.emptyList();
diff --git
a/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine
b/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine
new file mode 100644
index 00000000000..dd6f99b26cf
--- /dev/null
+++
b/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.DefaultShardingConditionEngine
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/decider/ShardingSQLFederationDeciderTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/decider/ShardingSQLFederationDeciderTest.java
index 76f8d504185..e7741263bb4 100644
---
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/decider/ShardingSQLFederationDeciderTest.java
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/decider/ShardingSQLFederationDeciderTest.java
@@ -26,8 +26,9 @@ import
org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseT
import org.apache.shardingsphere.infra.datanode.DataNode;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
+import
org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine;
import
org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngineFactory;
-import
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.WhereClauseShardingConditionEngine;
+import
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.DefaultShardingConditionEngine;
import
org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
import org.apache.shardingsphere.sharding.rule.BindingTableRule;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
@@ -75,7 +76,7 @@ public final class ShardingSQLFederationDeciderTest {
SQLFederationDeciderContext actual = new SQLFederationDeciderContext();
ShardingSQLFederationDecider federationDecider = new
ShardingSQLFederationDecider();
try (MockedStatic<ShardingConditionEngineFactory>
shardingConditionEngineFactory =
mockStatic(ShardingConditionEngineFactory.class)) {
- WhereClauseShardingConditionEngine shardingConditionEngine =
mock(WhereClauseShardingConditionEngine.class);
+ ShardingConditionEngine shardingConditionEngine =
mock(DefaultShardingConditionEngine.class);
when(shardingConditionEngine.createShardingConditions(any(),
any())).thenReturn(createShardingConditions());
shardingConditionEngineFactory.when(() ->
ShardingConditionEngineFactory.createShardingConditionEngine(any(QueryContext.class),
any(), any())).thenReturn(shardingConditionEngine);
federationDecider.decide(actual, queryContext, createDatabase(),
createShardingRule(), new ConfigurationProperties(new Properties()));
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngineFactoryTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngineFactoryTest.java
index 2913654ccb0..f1558f311bc 100644
---
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngineFactoryTest.java
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/ShardingConditionEngineFactoryTest.java
@@ -17,24 +17,43 @@
package org.apache.shardingsphere.sharding.route.engine.condition.engine;
+import org.apache.groovy.util.Maps;
import org.apache.shardingsphere.infra.binder.QueryContext;
+import
org.apache.shardingsphere.infra.binder.segment.insert.values.InsertValueContext;
+import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.infra.database.DefaultDatabase;
+import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import
org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereSchema;
-import
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.InsertClauseShardingConditionEngine;
-import
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.WhereClauseShardingConditionEngine;
+import
org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
+import
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.DefaultShardingConditionEngine;
+import
org.apache.shardingsphere.sharding.route.engine.condition.value.RangeShardingConditionValue;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.junit.Before;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenExpression;
+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.dml.predicate.WhereSegment;
+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.statement.dml.InsertStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -44,29 +63,50 @@ public final class ShardingConditionEngineFactoryTest {
@Mock
private QueryContext queryContext;
- @Mock(answer = Answers.RETURNS_DEEP_STUBS)
- private ShardingSphereDatabase database;
-
@Mock
private ShardingRule shardingRule;
- @Before
- public void setUp() {
-
when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
- }
-
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void assertCreateInsertClauseShardingConditionEngine() {
- SQLStatementContext insertStatementContext =
mock(InsertStatementContext.class);
-
when(queryContext.getSqlStatementContext()).thenReturn(insertStatementContext);
- ShardingConditionEngine<?> actual =
ShardingConditionEngineFactory.createShardingConditionEngine(queryContext,
database, shardingRule);
- assertThat(actual,
instanceOf(InsertClauseShardingConditionEngine.class));
+ ShardingSphereDatabase database =
ShardingSphereDatabase.create("test_db",
DatabaseTypeEngine.getDatabaseType("MySQL"));
+ InsertStatementContext insertStatementContext =
mock(InsertStatementContext.class);
+ InsertStatement insertStatement = mock(InsertStatement.class);
+ InsertValueContext insertValueContext = new
InsertValueContext(Collections.singletonList(new LiteralExpressionSegment(0,
10, "1")), Collections.emptyList(), 0);
+ when(insertStatement.getTable()).thenReturn(new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("foo_table"))));
+
when(insertStatementContext.getSqlStatement()).thenReturn(insertStatement);
+
when(insertStatementContext.getColumnNames()).thenReturn(Collections.singletonList("foo_col"));
+
when(insertStatementContext.getInsertValueContexts()).thenReturn(Collections.singletonList(insertValueContext));
+
when(insertStatementContext.getGeneratedKeyContext()).thenReturn(Optional.empty());
+
when(queryContext.getSqlStatementContext()).thenReturn((SQLStatementContext)
insertStatementContext);
+ ShardingConditionEngine engine =
ShardingConditionEngineFactory.createShardingConditionEngine(queryContext,
database, shardingRule);
+ assertThat(engine, instanceOf(DefaultShardingConditionEngine.class));
+ List<ShardingCondition> shardingConditions =
engine.createShardingConditions(insertStatementContext,
Collections.emptyList());
+ assertThat(shardingConditions.get(0).getStartIndex(), is(0));
+ assertTrue(shardingConditions.get(0).getValues().isEmpty());
}
@Test
public void assertCreateWhereClauseShardingConditionEngine() {
- ShardingConditionEngine<?> actual =
ShardingConditionEngineFactory.createShardingConditionEngine(queryContext,
database, shardingRule);
- assertThat(actual,
instanceOf(WhereClauseShardingConditionEngine.class));
+ ShardingSphereDatabase database =
ShardingSphereDatabase.create("test_db",
DatabaseTypeEngine.getDatabaseType("MySQL"));
+ SelectStatementContext sqlStatementContext =
mock(SelectStatementContext.class);
+ WhereSegment whereSegment = mock(WhereSegment.class);
+ TablesContext tablesContext = mock(TablesContext.class);
+ int betweenStart = 1;
+ int betweenEnd = 100;
+ ColumnSegment left = new ColumnSegment(0, 0, new
IdentifierValue("foo_sharding_col"));
+ ExpressionSegment betweenSegment = new LiteralExpressionSegment(0, 0,
betweenStart);
+ ExpressionSegment andSegment = new LiteralExpressionSegment(0, 0,
betweenEnd);
+ BetweenExpression betweenExpression = new BetweenExpression(0, 0,
left, betweenSegment, andSegment, false);
+ when(whereSegment.getExpr()).thenReturn(betweenExpression);
+ when(shardingRule.findShardingColumn(any(),
any())).thenReturn(Optional.of("foo_sharding_col"));
+
when(sqlStatementContext.getWhereSegments()).thenReturn(Collections.singleton(whereSegment));
+ when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext);
+ when(tablesContext.findTableNamesByColumnSegment(anyCollection(),
any())).thenReturn(Maps.of("foo_sharding_col", "table_1"));
+ ShardingConditionEngine engine =
ShardingConditionEngineFactory.createShardingConditionEngine(queryContext,
database, shardingRule);
+ assertThat(engine, instanceOf(DefaultShardingConditionEngine.class));
+ List<ShardingCondition> shardingConditions =
engine.createShardingConditions(sqlStatementContext, Collections.emptyList());
+ assertThat(shardingConditions.get(0).getStartIndex(), is(0));
+ assertTrue(shardingConditions.get(0).getValues().get(0) instanceof
RangeShardingConditionValue);
}
}