This is an automated email from the ASF dual-hosted git repository. kimmking 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 7799e5c Use KernelProcessor in JDBC (#7542) 7799e5c is described below commit 7799e5c4d5c13e80e1f675477aa850f3c109181e Author: Liang Zhang <terrym...@163.com> AuthorDate: Mon Sep 21 22:22:25 2020 +0800 Use KernelProcessor in JDBC (#7542) --- .../infra/context/kernel/KernelProcessor.java | 15 --------------- .../statement/ShardingSpherePreparedStatement.java | 20 ++++---------------- .../jdbc/core/statement/ShardingSphereStatement.java | 19 ++++--------------- .../ShardingCTLExplainBackendHandlerTest.java | 4 +++- 4 files changed, 11 insertions(+), 47 deletions(-) diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java index cec773d..75129ba 100644 --- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java +++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java @@ -22,14 +22,11 @@ import org.apache.shardingsphere.infra.context.sql.LogicSQLContext; import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext; import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContextBuilder; import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit; -import org.apache.shardingsphere.infra.executor.sql.context.SQLUnit; import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry; import org.apache.shardingsphere.infra.rewrite.engine.result.SQLRewriteResult; import org.apache.shardingsphere.infra.route.DataNodeRouter; import org.apache.shardingsphere.infra.route.context.RouteContext; -import org.apache.shardingsphere.infra.route.context.RouteResult; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; -import org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext; import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext; import java.util.Collection; @@ -48,9 +45,6 @@ public final class KernelProcessor { */ public ExecutionContext generateExecutionContext(final LogicSQLContext logicSQLContext, final ConfigurationProperties props) { Collection<ShardingSphereRule> rules = logicSQLContext.getSchemaContext().getSchema().getRules(); - if (rules.isEmpty()) { - return createDefaultExecutionContext(logicSQLContext); - } DataNodeRouter router = new DataNodeRouter(logicSQLContext.getSchemaContext().getSchema().getMetaData(), props, rules); RouteContext routeContext = router.route(logicSQLContext.getSqlStatement(), logicSQLContext.getSql(), logicSQLContext.getParameters()); SQLRewriteEntry rewriteEntry = new SQLRewriteEntry(logicSQLContext.getSchemaContext().getSchema().getMetaData().getRuleSchemaMetaData().getConfiguredSchemaMetaData(), props, rules); @@ -59,13 +53,4 @@ public final class KernelProcessor { Collection<ExecutionUnit> executionUnits = ExecutionContextBuilder.build(logicSQLContext.getSchemaContext().getSchema().getMetaData(), rewriteResult, sqlStatementContext); return new ExecutionContext(sqlStatementContext, executionUnits, routeContext); } - - private ExecutionContext createDefaultExecutionContext(final LogicSQLContext logicSQLContext) { - String dataSourceName = logicSQLContext.getSchemaContext().getSchema().getDataSources().isEmpty() - ? "" : logicSQLContext.getSchemaContext().getSchema().getDataSources().keySet().iterator().next(); - SQLStatementContext<?> sqlStatementContext = new CommonSQLStatementContext<>(logicSQLContext.getSqlStatement()); - ExecutionUnit executionUnit = new ExecutionUnit(dataSourceName, new SQLUnit(logicSQLContext.getSql(), logicSQLContext.getParameters())); - RouteContext routeContext = new RouteContext(sqlStatementContext, logicSQLContext.getParameters(), new RouteResult()); - return new ExecutionContext(sqlStatementContext, executionUnit, routeContext); - } } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java index fed9240..48a03bc 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java @@ -29,15 +29,15 @@ import org.apache.shardingsphere.driver.jdbc.core.resultset.GeneratedKeysResultS import org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResultSet; import org.apache.shardingsphere.driver.jdbc.core.statement.metadata.ShardingSphereParameterMetaData; import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey; +import org.apache.shardingsphere.infra.context.kernel.KernelProcessor; import org.apache.shardingsphere.infra.context.schema.SchemaContext; import org.apache.shardingsphere.infra.context.schema.SchemaContexts; +import org.apache.shardingsphere.infra.context.sql.LogicSQLContext; import org.apache.shardingsphere.infra.exception.ShardingSphereException; import org.apache.shardingsphere.infra.executor.kernel.InputGroup; import org.apache.shardingsphere.infra.executor.sql.ExecutorConstant; import org.apache.shardingsphere.infra.executor.sql.QueryResult; import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext; -import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContextBuilder; -import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit; import org.apache.shardingsphere.infra.executor.sql.log.SQLLogger; import org.apache.shardingsphere.infra.executor.sql.raw.RawSQLExecuteUnit; import org.apache.shardingsphere.infra.executor.sql.raw.execute.RawJDBCExecutor; @@ -50,13 +50,8 @@ import org.apache.shardingsphere.infra.executor.sql.resourced.jdbc.group.Stateme import org.apache.shardingsphere.infra.executor.sql.resourced.jdbc.queryresult.StreamQueryResult; import org.apache.shardingsphere.infra.merge.MergeEngine; import org.apache.shardingsphere.infra.merge.result.MergedResult; -import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry; -import org.apache.shardingsphere.infra.rewrite.engine.result.SQLRewriteResult; -import org.apache.shardingsphere.infra.route.DataNodeRouter; -import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.rule.DataNodeRoutedRule; import org.apache.shardingsphere.sql.parser.binder.segment.insert.keygen.GeneratedKeyContext; -import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext; import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext; import org.apache.shardingsphere.sql.parser.binder.statement.dml.SelectStatementContext; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; @@ -258,15 +253,8 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState } private ExecutionContext createExecutionContext() { - SchemaContext schemaContext = schemaContexts.getDefaultSchemaContext(); - DataNodeRouter router = new DataNodeRouter(schemaContext.getSchema().getMetaData(), schemaContexts.getProps(), schemaContext.getSchema().getRules()); - RouteContext routeContext = router.route(sqlStatement, sql, getParameters()); - SQLRewriteEntry rewriteEntry = new SQLRewriteEntry(schemaContext.getSchema().getMetaData().getRuleSchemaMetaData().getConfiguredSchemaMetaData(), - schemaContexts.getProps(), schemaContext.getSchema().getRules()); - SQLRewriteResult rewriteResult = rewriteEntry.rewrite(sql, new ArrayList<>(getParameters()), routeContext); - SQLStatementContext<?> sqlStatementContext = routeContext.getSqlStatementContext(); - Collection<ExecutionUnit> executionUnits = ExecutionContextBuilder.build(schemaContext.getSchema().getMetaData(), rewriteResult, sqlStatementContext); - ExecutionContext result = new ExecutionContext(sqlStatementContext, executionUnits, routeContext); + LogicSQLContext logicSQLContext = new LogicSQLContext(schemaContexts.getDefaultSchemaContext(), sql, new ArrayList<>(getParameters()), sqlStatement); + ExecutionContext result = new KernelProcessor().generateExecutionContext(logicSQLContext, schemaContexts.getProps()); findGeneratedKey(result).ifPresent(generatedKey -> generatedValues.add(generatedKey.getGeneratedValues().getLast())); logSQL(result); return result; diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java index d18a4bc..3b880d0 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java @@ -27,15 +27,15 @@ import org.apache.shardingsphere.driver.jdbc.core.resultset.GeneratedKeysResultS import org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResultSet; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey; +import org.apache.shardingsphere.infra.context.kernel.KernelProcessor; import org.apache.shardingsphere.infra.context.schema.SchemaContext; import org.apache.shardingsphere.infra.context.schema.SchemaContexts; +import org.apache.shardingsphere.infra.context.sql.LogicSQLContext; import org.apache.shardingsphere.infra.exception.ShardingSphereException; import org.apache.shardingsphere.infra.executor.kernel.InputGroup; import org.apache.shardingsphere.infra.executor.sql.ExecutorConstant; import org.apache.shardingsphere.infra.executor.sql.QueryResult; import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext; -import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContextBuilder; -import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit; import org.apache.shardingsphere.infra.executor.sql.log.SQLLogger; import org.apache.shardingsphere.infra.executor.sql.raw.RawSQLExecuteUnit; import org.apache.shardingsphere.infra.executor.sql.raw.execute.RawJDBCExecutor; @@ -48,13 +48,8 @@ import org.apache.shardingsphere.infra.executor.sql.resourced.jdbc.group.Stateme import org.apache.shardingsphere.infra.executor.sql.resourced.jdbc.queryresult.StreamQueryResult; import org.apache.shardingsphere.infra.merge.MergeEngine; import org.apache.shardingsphere.infra.merge.result.MergedResult; -import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry; -import org.apache.shardingsphere.infra.rewrite.engine.result.SQLRewriteResult; -import org.apache.shardingsphere.infra.route.DataNodeRouter; -import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.rule.DataNodeRoutedRule; import org.apache.shardingsphere.sql.parser.binder.segment.insert.keygen.GeneratedKeyContext; -import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext; import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext; import org.apache.shardingsphere.sql.parser.binder.statement.dml.SelectStatementContext; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; @@ -285,14 +280,8 @@ public final class ShardingSphereStatement extends AbstractStatementAdapter { clearStatements(); SchemaContext schemaContext = schemaContexts.getDefaultSchemaContext(); SQLStatement sqlStatement = schemaContext.getRuntimeContext().getSqlParserEngine().parse(sql, false); - DataNodeRouter router = new DataNodeRouter(schemaContext.getSchema().getMetaData(), schemaContexts.getProps(), schemaContext.getSchema().getRules()); - RouteContext routeContext = router.route(sqlStatement, sql, Collections.emptyList()); - SQLRewriteEntry rewriteEntry = new SQLRewriteEntry(schemaContext.getSchema().getMetaData().getRuleSchemaMetaData().getConfiguredSchemaMetaData(), - schemaContexts.getProps(), schemaContext.getSchema().getRules()); - SQLRewriteResult rewriteResult = rewriteEntry.rewrite(sql, Collections.emptyList(), routeContext); - SQLStatementContext<?> sqlStatementContext = routeContext.getSqlStatementContext(); - Collection<ExecutionUnit> executionUnits = ExecutionContextBuilder.build(schemaContext.getSchema().getMetaData(), rewriteResult, sqlStatementContext); - ExecutionContext result = new ExecutionContext(sqlStatementContext, executionUnits, routeContext); + LogicSQLContext logicSQLContext = new LogicSQLContext(schemaContext, sql, Collections.emptyList(), sqlStatement); + ExecutionContext result = new KernelProcessor().generateExecutionContext(logicSQLContext, schemaContexts.getProps()); logSQL(sql, schemaContexts.getProps(), result); return result; } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/sctl/explain/ShardingCTLExplainBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/sctl/explain/ShardingCTLExplainBackendHandlerTest.java index 5e0ee23..7feffdb 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/sctl/explain/ShardingCTLExplainBackendHandlerTest.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/sctl/explain/ShardingCTLExplainBackendHandlerTest.java @@ -8,6 +8,7 @@ import org.apache.shardingsphere.infra.context.schema.impl.StandardSchemaContext import org.apache.shardingsphere.infra.context.schema.runtime.RuntimeContext; import org.apache.shardingsphere.infra.context.schema.ShardingSphereSchema; import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.rdl.parser.engine.ShardingSphereSQLParserEngine; @@ -24,6 +25,7 @@ import java.util.Properties; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -63,7 +65,7 @@ public final class ShardingCTLExplainBackendHandlerTest { private Map<String, SchemaContext> getSchemaContextMap() { RuntimeContext runtimeContext = new RuntimeContext(null, null, new ShardingSphereSQLParserEngine(new StandardSQLParserEngine("MySQL"))); ShardingSphereSchema schema = new ShardingSphereSchema(Collections.emptyList(), - Collections.emptyList(), Collections.singletonMap("ds0", mock(DataSource.class)), null); + Collections.emptyList(), Collections.singletonMap("ds0", mock(DataSource.class)), mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS)); return Collections.singletonMap("schema", new SchemaContext("schema", schema, runtimeContext)); }