From 6515f62c7098470dd136168aad58c0278078fb42 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Wed, 29 May 2024 11:54:05 +0800 Subject: [PATCH] Refactor ShardingSphereStatement --- .../ShardingSpherePreparedStatement.java | 45 +++++++++---------- .../statement/ShardingSphereStatement.java | 16 ++++--- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java index 9bb56e20c9421..3109edbd1609b 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java @@ -253,6 +253,22 @@ private ShardingSphereResultSet doExecuteQuery(final ExecutionContext executionC return new ShardingSphereResultSet(resultSets, mergedResult, this, selectContainsEnhancedTable, executionContext, columnLabelAndIndexMap); } + private List executeQuery0(final ExecutionContext executionContext) throws SQLException { + if (hasRawExecutionRule()) { + return executor.getRawExecutor().execute(createRawExecutionGroupContext(executionContext), + executionContext.getQueryContext(), new RawSQLExecutorCallback()).stream().map(QueryResult.class::cast).collect(Collectors.toList()); + } + ExecutionGroupContext executionGroupContext = createExecutionGroupContext(executionContext); + cacheStatements(executionGroupContext.getInputGroups()); + PreparedStatementExecuteQueryCallback callback = new PreparedStatementExecuteQueryCallback(metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType(), + metaDataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData(), sqlStatement, SQLExecutorExceptionHandler.isExceptionThrown()); + return executor.getRegularExecutor().executeQuery(executionGroupContext, executionContext.getQueryContext(), callback); + } + + private boolean hasRawExecutionRule() { + return !metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getAttributes(RawExecutionRuleAttribute.class).isEmpty(); + } + private void handleAutoCommit(final SQLStatement sqlStatement) throws SQLException { if (AutoCommitUtils.needOpenTransaction(sqlStatement)) { connection.handleAutoCommit(); @@ -265,19 +281,6 @@ private void resetParameters() throws SQLException { replaySetParameter(); } - private List executeQuery0(final ExecutionContext executionContext) throws SQLException { - if (hasRawExecutionRule()) { - return executor.getRawExecutor().execute(createRawExecutionGroupContext(executionContext), - executionContext.getQueryContext(), new RawSQLExecutorCallback()).stream().map(QueryResult.class::cast).collect(Collectors.toList()); - } - ExecutionGroupContext executionGroupContext = createExecutionGroupContext(executionContext); - cacheStatements(executionGroupContext.getInputGroups()); - return executor.getRegularExecutor().executeQuery(executionGroupContext, executionContext.getQueryContext(), - new PreparedStatementExecuteQueryCallback(metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType(), - metaDataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData(), sqlStatement, - SQLExecutorExceptionHandler.isExceptionThrown())); - } - private DriverExecutionPrepareEngine createDriverExecutionPrepareEngine(final ShardingSphereDatabase database) { int maxConnectionsSizePerQuery = metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY); return new DriverExecutionPrepareEngine<>(JDBCDriverType.PREPARED_STATEMENT, maxConnectionsSizePerQuery, connection.getDatabaseConnectionManager(), statementManager, statementOption, @@ -382,16 +385,6 @@ public boolean execute() throws SQLException { } } - private boolean hasRawExecutionRule() { - return !metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getAttributes(RawExecutionRuleAttribute.class).isEmpty(); - } - - private ExecutionGroupContext createRawExecutionGroupContext(final ExecutionContext executionContext) throws SQLException { - int maxConnectionsSizePerQuery = metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY); - return new RawExecutionPrepareEngine(maxConnectionsSizePerQuery, metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getRules()) - .prepare(executionContext.getRouteContext(), executionContext.getExecutionUnits(), new ExecutionGroupReportContext(connection.getProcessId(), databaseName, new Grantee("", ""))); - } - private boolean executeWithExecutionContext(final ExecutionContext executionContext) throws SQLException { return isNeedImplicitCommitTransaction(connection, executionContext.getSqlStatementContext().getSqlStatement(), executionContext.getExecutionUnits().size() > 1) ? executeWithImplicitCommitTransaction(() -> useDriverToExecute(executionContext), connection, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()) @@ -494,6 +487,12 @@ private ExecutionContext createExecutionContext(final QueryContext queryContext, return new ExecutionContext(queryContext, Collections.singletonList(executionUnit), new RouteContext()); } + private ExecutionGroupContext createRawExecutionGroupContext(final ExecutionContext executionContext) throws SQLException { + int maxConnectionsSizePerQuery = metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY); + return new RawExecutionPrepareEngine(maxConnectionsSizePerQuery, metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getRules()) + .prepare(executionContext.getRouteContext(), executionContext.getExecutionUnits(), new ExecutionGroupReportContext(connection.getProcessId(), databaseName, new Grantee("", ""))); + } + private QueryContext createQueryContext() { List params = new ArrayList<>(getParameters()); if (sqlStatementContext instanceof ParameterAware) { diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java index 55edd1785d902..ef05eb294cf3f 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java @@ -177,9 +177,9 @@ private ShardingSphereResultSet doExecuteQuery(final ExecutionContext executionC } private List executeQuery0(final ExecutionContext executionContext) throws SQLException { - if (!metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getAttributes(RawExecutionRuleAttribute.class).isEmpty()) { - return executor.getRawExecutor().execute( - createRawExecutionContext(executionContext), executionContext.getQueryContext(), new RawSQLExecutorCallback()).stream().map(QueryResult.class::cast).collect(Collectors.toList()); + if (hasRawExecutionRule()) { + return executor.getRawExecutor().execute(createRawExecutionGroupContext(executionContext), + executionContext.getQueryContext(), new RawSQLExecutorCallback()).stream().map(QueryResult.class::cast).collect(Collectors.toList()); } ExecutionGroupContext executionGroupContext = createExecutionGroupContext(executionContext); cacheStatements(executionGroupContext.getInputGroups()); @@ -189,6 +189,10 @@ private List executeQuery0(final ExecutionContext executionContext) return executor.getRegularExecutor().executeQuery(executionGroupContext, executionContext.getQueryContext(), callback); } + private boolean hasRawExecutionRule() { + return !metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getAttributes(RawExecutionRuleAttribute.class).isEmpty(); + } + private DriverExecutionPrepareEngine createDriverExecutionPrepareEngine(final ShardingSphereDatabase database) { int maxConnectionsSizePerQuery = metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY); return new DriverExecutionPrepareEngine<>(JDBCDriverType.STATEMENT, maxConnectionsSizePerQuery, connection.getDatabaseConnectionManager(), statementManager, statementOption, @@ -276,7 +280,7 @@ private int executeUpdate0(final String sql, final ExecuteUpdateCallback updateC } executionContext = createExecutionContext(queryContext); if (!metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getAttributes(RawExecutionRuleAttribute.class).isEmpty()) { - Collection results = executor.getRawExecutor().execute(createRawExecutionContext(executionContext), executionContext.getQueryContext(), new RawSQLExecutorCallback()); + Collection results = executor.getRawExecutor().execute(createRawExecutionGroupContext(executionContext), executionContext.getQueryContext(), new RawSQLExecutorCallback()); return accumulate(results); } return executeUpdate(updateCallback, queryContext.getSqlStatementContext(), executionContext); @@ -382,7 +386,7 @@ private boolean execute0(final String sql, final ExecuteCallback executeCallback } executionContext = createExecutionContext(queryContext); if (!metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getAttributes(RawExecutionRuleAttribute.class).isEmpty()) { - Collection results = executor.getRawExecutor().execute(createRawExecutionContext(executionContext), executionContext.getQueryContext(), new RawSQLExecutorCallback()); + Collection results = executor.getRawExecutor().execute(createRawExecutionGroupContext(executionContext), executionContext.getQueryContext(), new RawSQLExecutorCallback()); return results.iterator().next() instanceof QueryResult; } return executeWithExecutionContext(executeCallback, executionContext); @@ -441,7 +445,7 @@ private ExecutionGroupContext createExecutionGroupContext(fin new ExecutionGroupReportContext(connection.getProcessId(), databaseName, new Grantee("", ""))); } - private ExecutionGroupContext createRawExecutionContext(final ExecutionContext executionContext) throws SQLException { + private ExecutionGroupContext createRawExecutionGroupContext(final ExecutionContext executionContext) throws SQLException { int maxConnectionsSizePerQuery = metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY); return new RawExecutionPrepareEngine(maxConnectionsSizePerQuery, metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getRules()) .prepare(executionContext.getRouteContext(), executionContext.getExecutionUnits(), new ExecutionGroupReportContext(connection.getProcessId(), databaseName, new Grantee("", "")));