diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutor.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutor.java index 69a63292e8f07..4acb9ff047e4f 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutor.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutor.java @@ -29,9 +29,9 @@ import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit; import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor; import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.user.Grantee; import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; import java.sql.SQLException; @@ -50,7 +50,7 @@ */ public final class BatchPreparedStatementExecutor { - private final MetaDataContexts metaDataContexts; + private final ShardingSphereMetaData metaData; private final JDBCExecutor jdbcExecutor; @@ -63,9 +63,9 @@ public final class BatchPreparedStatementExecutor { private final String databaseName; - public BatchPreparedStatementExecutor(final MetaDataContexts metaDataContexts, final JDBCExecutor jdbcExecutor, final String databaseName, final String processId) { + public BatchPreparedStatementExecutor(final ShardingSphereMetaData metaData, final JDBCExecutor jdbcExecutor, final String databaseName, final String processId) { this.databaseName = databaseName; - this.metaDataContexts = metaDataContexts; + this.metaData = metaData; this.jdbcExecutor = jdbcExecutor; executionGroupContext = new ExecutionGroupContext<>(new LinkedList<>(), new ExecutionGroupReportContext(processId, databaseName, new Grantee("", ""))); batchExecutionUnits = new LinkedList<>(); @@ -135,8 +135,8 @@ private void handleNewBatchExecutionUnits(final Collection n */ public int[] executeBatch(final SQLStatementContext sqlStatementContext) throws SQLException { boolean isExceptionThrown = SQLExecutorExceptionHandler.isExceptionThrown(); - JDBCExecutorCallback callback = new JDBCExecutorCallback(metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType(), - metaDataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData(), sqlStatementContext.getSqlStatement(), isExceptionThrown) { + JDBCExecutorCallback callback = new JDBCExecutorCallback(metaData.getDatabase(databaseName).getProtocolType(), + metaData.getDatabase(databaseName).getResourceMetaData(), sqlStatementContext.getSqlStatement(), isExceptionThrown) { @Override protected int[] executeSQL(final String sql, final Statement statement, final ConnectionMode connectionMode, final DatabaseType storageType) throws SQLException { @@ -157,7 +157,7 @@ protected Optional getSaneResult(final SQLStatement sqlStatement, final S } private boolean isNeedAccumulate(final SQLStatementContext sqlStatementContext) { - for (DataNodeRuleAttribute each : metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getAttributes(DataNodeRuleAttribute.class)) { + for (DataNodeRuleAttribute each : metaData.getDatabase(databaseName).getRuleMetaData().getAttributes(DataNodeRuleAttribute.class)) { if (each.isNeedAccumulate(sqlStatementContext.getTablesContext().getTableNames())) { return true; } diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java index d024fc4a6f468..7f52c2fbf47fe 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java @@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; @@ -53,9 +53,9 @@ public abstract class AbstractStatementAdapter extends WrapperAdapter implements private boolean closeOnCompletion; - protected final void handleExceptionInTransaction(final ShardingSphereConnection connection, final MetaDataContexts metaDataContexts) { + protected final void handleExceptionInTransaction(final ShardingSphereConnection connection, final ShardingSphereMetaData metaData) { if (connection.getDatabaseConnectionManager().getConnectionTransaction().isInTransaction()) { - DatabaseType databaseType = metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getProtocolType(); + DatabaseType databaseType = metaData.getDatabase(connection.getDatabaseName()).getProtocolType(); DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData(); if (dialectDatabaseMetaData.getDefaultSchema().isPresent()) { connection.getDatabaseConnectionManager().getConnectionContext().getTransactionContext().setExceptionOccur(true); 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 151ffd1bfc226..0ce53932acce2 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 @@ -58,6 +58,7 @@ import org.apache.shardingsphere.infra.hint.SQLHintUtils; import org.apache.shardingsphere.infra.merge.MergeEngine; import org.apache.shardingsphere.infra.merge.result.MergedResult; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.user.Grantee; @@ -66,7 +67,6 @@ import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute; import org.apache.shardingsphere.infra.rule.attribute.resoure.StorageConnectorReusableRuleAttribute; import org.apache.shardingsphere.infra.session.query.QueryContext; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.parser.rule.SQLParserRule; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement; @@ -96,7 +96,7 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState @Getter private final ShardingSphereConnection connection; - private final MetaDataContexts metaDataContexts; + private final ShardingSphereMetaData metaData; private final String sql; @@ -170,25 +170,25 @@ private ShardingSpherePreparedStatement(final ShardingSphereConnection connectio throw new EmptySQLException().toSQLException(); } this.connection = connection; - metaDataContexts = connection.getContextManager().getMetaDataContexts(); + metaData = connection.getContextManager().getMetaDataContexts().getMetaData(); hintValueContext = SQLHintUtils.extractHint(sql); this.sql = SQLHintUtils.removeHint(sql); statements = new ArrayList<>(); parameterSets = new ArrayList<>(); - SQLParserRule sqlParserRule = metaDataContexts.getMetaData().getGlobalRuleMetaData().getSingleRule(SQLParserRule.class); - SQLParserEngine sqlParserEngine = sqlParserRule.getSQLParserEngine(metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getProtocolType()); + SQLParserRule sqlParserRule = metaData.getGlobalRuleMetaData().getSingleRule(SQLParserRule.class); + SQLParserEngine sqlParserEngine = sqlParserRule.getSQLParserEngine(metaData.getDatabase(connection.getDatabaseName()).getProtocolType()); SQLStatement sqlStatement = sqlParserEngine.parse(this.sql, true); - sqlStatementContext = new SQLBindEngine(metaDataContexts.getMetaData(), connection.getDatabaseName(), hintValueContext).bind(sqlStatement, Collections.emptyList()); + sqlStatementContext = new SQLBindEngine(metaData, connection.getDatabaseName(), hintValueContext).bind(sqlStatement, Collections.emptyList()); databaseName = sqlStatementContext.getTablesContext().getDatabaseName().orElse(connection.getDatabaseName()); connection.getDatabaseConnectionManager().getConnectionContext().setCurrentDatabase(databaseName); parameterMetaData = new ShardingSphereParameterMetaData(sqlStatement); statementOption = returnGeneratedKeys ? new StatementOption(true, columns) : new StatementOption(resultSetType, resultSetConcurrency, resultSetHoldability); executor = new DriverExecutor(connection); JDBCExecutor jdbcExecutor = new JDBCExecutor(connection.getContextManager().getExecutorEngine(), connection.getDatabaseConnectionManager().getConnectionContext()); - batchPreparedStatementExecutor = new BatchPreparedStatementExecutor(metaDataContexts, jdbcExecutor, databaseName, connection.getProcessId()); + batchPreparedStatementExecutor = new BatchPreparedStatementExecutor(metaData, jdbcExecutor, databaseName, connection.getProcessId()); kernelProcessor = new KernelProcessor(); - statementsCacheable = isStatementsCacheable(metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData()); - trafficRule = metaDataContexts.getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class); + statementsCacheable = isStatementsCacheable(metaData.getDatabase(databaseName).getRuleMetaData()); + trafficRule = metaData.getGlobalRuleMetaData().getSingleRule(TrafficRule.class); selectContainsEnhancedTable = sqlStatementContext instanceof SelectStatementContext && ((SelectStatementContext) sqlStatementContext).isContainsEnhancedTable(); statementManager = new StatementManager(); } @@ -207,9 +207,9 @@ public ResultSet executeQuery() throws SQLException { clearPrevious(); QueryContext queryContext = createQueryContext(); handleAutoCommit(queryContext.getSqlStatementContext().getSqlStatement()); - ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName); + ShardingSphereDatabase database = metaData.getDatabase(databaseName); findGeneratedKey().ifPresent(optional -> generatedValues.addAll(optional.getGeneratedValues())); - currentResultSet = executor.executeQuery(metaDataContexts.getMetaData(), database, queryContext, createDriverExecutionPrepareEngine(database), this, columnLabelAndIndexMap, + currentResultSet = executor.executeQuery(metaData, database, queryContext, createDriverExecutionPrepareEngine(database), this, columnLabelAndIndexMap, (StatementReplayCallback) this::replay); if (currentResultSet instanceof ShardingSphereResultSet) { columnLabelAndIndexMap = ((ShardingSphereResultSet) currentResultSet).getColumnLabelAndIndexMap(); @@ -222,8 +222,8 @@ public ResultSet executeQuery() throws SQLException { // CHECKSTYLE:OFF } catch (final RuntimeException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } finally { batchPreparedStatementExecutor.clear(); clearParameters(); @@ -243,7 +243,7 @@ private void resetParameters() throws SQLException { } private DriverExecutionPrepareEngine createDriverExecutionPrepareEngine(final ShardingSphereDatabase database) { - int maxConnectionsSizePerQuery = metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY); + int maxConnectionsSizePerQuery = metaData.getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY); return new DriverExecutionPrepareEngine<>(JDBCDriverType.PREPARED_STATEMENT, maxConnectionsSizePerQuery, connection.getDatabaseConnectionManager(), statementManager, statementOption, database.getRuleMetaData().getRules(), database.getResourceMetaData().getStorageUnits()); } @@ -258,8 +258,8 @@ public int executeUpdate() throws SQLException { clearPrevious(); QueryContext queryContext = createQueryContext(); handleAutoCommit(queryContext.getSqlStatementContext().getSqlStatement()); - ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName); - final int result = executor.executeUpdate(metaDataContexts.getMetaData(), database, queryContext, createDriverExecutionPrepareEngine(database), + ShardingSphereDatabase database = metaData.getDatabase(databaseName); + final int result = executor.executeUpdate(metaData, database, queryContext, createDriverExecutionPrepareEngine(database), (statement, sql) -> ((PreparedStatement) statement).executeUpdate(), null, (StatementReplayCallback) this::replay); for (Statement each : executor.getStatements()) { statements.add((PreparedStatement) each); @@ -270,8 +270,8 @@ public int executeUpdate() throws SQLException { // CHECKSTYLE:OFF } catch (final RuntimeException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } finally { clearBatch(); } @@ -287,9 +287,9 @@ public boolean execute() throws SQLException { clearPrevious(); QueryContext queryContext = createQueryContext(); handleAutoCommit(queryContext.getSqlStatementContext().getSqlStatement()); - ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName); + ShardingSphereDatabase database = metaData.getDatabase(databaseName); final boolean result = executor.executeAdvance( - metaDataContexts.getMetaData(), database, queryContext, createDriverExecutionPrepareEngine(database), (statement, sql) -> ((PreparedStatement) statement).execute(), + metaData, database, queryContext, createDriverExecutionPrepareEngine(database), (statement, sql) -> ((PreparedStatement) statement).execute(), null, (StatementReplayCallback) this::replay); for (Statement each : executor.getStatements()) { statements.add((PreparedStatement) each); @@ -300,8 +300,8 @@ public boolean execute() throws SQLException { // CHECKSTYLE:OFF } catch (final RuntimeException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } finally { clearBatch(); } @@ -351,11 +351,11 @@ private List getQueryResults(final List resultSets) thro } private ExecutionContext createExecutionContext(final QueryContext queryContext) { - RuleMetaData globalRuleMetaData = metaDataContexts.getMetaData().getGlobalRuleMetaData(); - ShardingSphereDatabase currentDatabase = metaDataContexts.getMetaData().getDatabase(databaseName); + RuleMetaData globalRuleMetaData = metaData.getGlobalRuleMetaData(); + ShardingSphereDatabase currentDatabase = metaData.getDatabase(databaseName); SQLAuditEngine.audit(queryContext.getSqlStatementContext(), queryContext.getParameters(), globalRuleMetaData, currentDatabase, null, queryContext.getHintValueContext()); ExecutionContext result = kernelProcessor.generateExecutionContext( - queryContext, currentDatabase, globalRuleMetaData, metaDataContexts.getMetaData().getProps(), connection.getDatabaseConnectionManager().getConnectionContext()); + queryContext, currentDatabase, globalRuleMetaData, metaData.getProps(), connection.getDatabaseConnectionManager().getConnectionContext()); findGeneratedKey().ifPresent(optional -> generatedValues.addAll(optional.getGeneratedValues())); return result; } @@ -374,8 +374,8 @@ private QueryContext createQueryContext() { } private MergedResult mergeQuery(final List queryResults, final SQLStatementContext sqlStatementContext) throws SQLException { - MergeEngine mergeEngine = new MergeEngine(metaDataContexts.getMetaData().getGlobalRuleMetaData(), metaDataContexts.getMetaData().getDatabase(databaseName), - metaDataContexts.getMetaData().getProps(), connection.getDatabaseConnectionManager().getConnectionContext()); + MergeEngine mergeEngine = new MergeEngine(metaData.getGlobalRuleMetaData(), metaData.getDatabase(databaseName), + metaData.getProps(), connection.getDatabaseConnectionManager().getConnectionContext()); return mergeEngine.merge(queryResults, sqlStatementContext); } @@ -424,7 +424,7 @@ public ResultSet getGeneratedKeys() throws SQLException { } private String getGeneratedKeysColumnName(final String columnName) { - return metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType() instanceof MySQLDatabaseType ? "GENERATED_KEY" : columnName; + return metaData.getDatabase(databaseName).getProtocolType() instanceof MySQLDatabaseType ? "GENERATED_KEY" : columnName; } @Override @@ -452,8 +452,8 @@ public int[] executeBatch() throws SQLException { // CHECKSTYLE:OFF } catch (final RuntimeException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } finally { clearBatch(); } @@ -474,10 +474,10 @@ private int[] doExecuteBatch(final BatchPreparedStatementExecutor batchExecutor) } private void initBatchPreparedStatementExecutor(final BatchPreparedStatementExecutor batchExecutor) throws SQLException { - DriverExecutionPrepareEngine prepareEngine = new DriverExecutionPrepareEngine<>(JDBCDriverType.PREPARED_STATEMENT, metaDataContexts.getMetaData().getProps() + DriverExecutionPrepareEngine prepareEngine = new DriverExecutionPrepareEngine<>(JDBCDriverType.PREPARED_STATEMENT, metaData.getProps() .getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY), connection.getDatabaseConnectionManager(), statementManager, statementOption, - metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getRules(), - metaDataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnits()); + metaData.getDatabase(databaseName).getRuleMetaData().getRules(), + metaData.getDatabase(databaseName).getResourceMetaData().getStorageUnits()); List executionUnits = new ArrayList<>(batchExecutor.getBatchExecutionUnits().size()); for (BatchExecutionUnit each : batchExecutor.getBatchExecutionUnits()) { ExecutionUnit executionUnit = each.getExecutionUnit(); @@ -523,7 +523,7 @@ public int getResultSetHoldability() { @Override public boolean isAccumulate() { - for (DataNodeRuleAttribute each : metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getAttributes(DataNodeRuleAttribute.class)) { + for (DataNodeRuleAttribute each : metaData.getDatabase(databaseName).getRuleMetaData().getAttributes(DataNodeRuleAttribute.class)) { if (each.isNeedAccumulate(sqlStatementContext.getTablesContext().getTableNames())) { return true; } 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 6294f9cbe9b39..9c63028f24f95 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 @@ -48,10 +48,10 @@ import org.apache.shardingsphere.infra.hint.SQLHintUtils; import org.apache.shardingsphere.infra.merge.MergeEngine; import org.apache.shardingsphere.infra.merge.result.MergedResult; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute; import org.apache.shardingsphere.infra.session.query.QueryContext; -import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.parser.rule.SQLParserRule; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement; @@ -78,7 +78,7 @@ public final class ShardingSphereStatement extends AbstractStatementAdapter { @Getter private final ShardingSphereConnection connection; - private final MetaDataContexts metaDataContexts; + private final ShardingSphereMetaData metaData; private final List statements; @@ -110,7 +110,7 @@ public ShardingSphereStatement(final ShardingSphereConnection connection, final public ShardingSphereStatement(final ShardingSphereConnection connection, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) { this.connection = connection; - metaDataContexts = connection.getContextManager().getMetaDataContexts(); + metaData = connection.getContextManager().getMetaDataContexts().getMetaData(); statements = new LinkedList<>(); statementOption = new StatementOption(resultSetType, resultSetConcurrency, resultSetHoldability); executor = new DriverExecutor(connection); @@ -127,23 +127,23 @@ public ResultSet executeQuery(final String sql) throws SQLException { handleAutoCommit(queryContext.getSqlStatementContext().getSqlStatement()); databaseName = queryContext.getDatabaseNameFromSQLStatement().orElse(connection.getDatabaseName()); connection.getDatabaseConnectionManager().getConnectionContext().setCurrentDatabase(databaseName); - ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName); + ShardingSphereDatabase database = metaData.getDatabase(databaseName); sqlStatementContext = queryContext.getSqlStatementContext(); - currentResultSet = executor.executeQuery(metaDataContexts.getMetaData(), database, queryContext, createDriverExecutionPrepareEngine(database), this, null, + currentResultSet = executor.executeQuery(metaData, database, queryContext, createDriverExecutionPrepareEngine(database), this, null, (StatementReplayCallback) (statements, parameterSets) -> replay(statements)); statements.addAll(executor.getStatements()); return currentResultSet; // CHECKSTYLE:OFF } catch (final RuntimeException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); + handleExceptionInTransaction(connection, metaData); currentResultSet = null; - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } } private DriverExecutionPrepareEngine createDriverExecutionPrepareEngine(final ShardingSphereDatabase database) { - int maxConnectionsSizePerQuery = metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY); + int maxConnectionsSizePerQuery = metaData.getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY); return new DriverExecutionPrepareEngine<>(JDBCDriverType.STATEMENT, maxConnectionsSizePerQuery, connection.getDatabaseConnectionManager(), statementManager, statementOption, database.getRuleMetaData().getRules(), database.getResourceMetaData().getStorageUnits()); } @@ -155,8 +155,8 @@ public int executeUpdate(final String sql) throws SQLException { // CHECKSTYLE:OFF } catch (final RuntimeException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } finally { currentResultSet = null; } @@ -173,8 +173,8 @@ public int executeUpdate(final String sql, final int autoGeneratedKeys) throws S // CHECKSTYLE:OFF } catch (final RuntimeException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } finally { currentResultSet = null; } @@ -188,8 +188,8 @@ public int executeUpdate(final String sql, final int[] columnIndexes) throws SQL // CHECKSTYLE:OFF } catch (final RuntimeException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } finally { currentResultSet = null; } @@ -203,8 +203,8 @@ public int executeUpdate(final String sql, final String[] columnNames) throws SQ // CHECKSTYLE:OFF } catch (final RuntimeException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } finally { currentResultSet = null; } @@ -215,11 +215,11 @@ private int executeUpdate(final String sql, final ExecuteUpdateCallback updateCa handleAutoCommit(queryContext.getSqlStatementContext().getSqlStatement()); databaseName = queryContext.getDatabaseNameFromSQLStatement().orElse(connection.getDatabaseName()); connection.getDatabaseConnectionManager().getConnectionContext().setCurrentDatabase(databaseName); - ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName); + ShardingSphereDatabase database = metaData.getDatabase(databaseName); sqlStatementContext = queryContext.getSqlStatementContext(); clearStatements(); int result = executor.executeUpdate( - metaDataContexts.getMetaData(), database, queryContext, createDriverExecutionPrepareEngine(database), trafficCallback, updateCallback, + metaData, database, queryContext, createDriverExecutionPrepareEngine(database), trafficCallback, updateCallback, (StatementReplayCallback) (statements, parameterSets) -> replay(statements)); statements.addAll(executor.getStatements()); replay(statements); @@ -233,8 +233,8 @@ public boolean execute(final String sql) throws SQLException { // CHECKSTYLE:OFF } catch (final SQLException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } } @@ -248,8 +248,8 @@ public boolean execute(final String sql, final int autoGeneratedKeys) throws SQL // CHECKSTYLE:OFF } catch (final SQLException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } } @@ -261,8 +261,8 @@ public boolean execute(final String sql, final int[] columnIndexes) throws SQLEx // CHECKSTYLE:OFF } catch (final SQLException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } } @@ -274,8 +274,8 @@ public boolean execute(final String sql, final String[] columnNames) throws SQLE // CHECKSTYLE:OFF } catch (final SQLException ex) { // CHECKSTYLE:ON - handleExceptionInTransaction(connection, metaDataContexts); - throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()); + handleExceptionInTransaction(connection, metaData); + throw SQLExceptionTransformEngine.toSQLException(ex, metaData.getDatabase(databaseName).getProtocolType()); } } @@ -285,10 +285,10 @@ private boolean execute0(final String sql, final ExecuteCallback executeCallback handleAutoCommit(queryContext.getSqlStatementContext().getSqlStatement()); databaseName = queryContext.getDatabaseNameFromSQLStatement().orElse(connection.getDatabaseName()); connection.getDatabaseConnectionManager().getConnectionContext().setCurrentDatabase(databaseName); - ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName); + ShardingSphereDatabase database = metaData.getDatabase(databaseName); sqlStatementContext = queryContext.getSqlStatementContext(); clearStatements(); - boolean result = executor.executeAdvance(metaDataContexts.getMetaData(), database, queryContext, createDriverExecutionPrepareEngine(database), trafficCallback, + boolean result = executor.executeAdvance(metaData, database, queryContext, createDriverExecutionPrepareEngine(database), trafficCallback, executeCallback, (StatementReplayCallback) (statements, parameterSets) -> replay(statements)); statements.addAll(executor.getStatements()); return result; @@ -324,11 +324,11 @@ public int[] executeBatch() throws SQLException { } private QueryContext createQueryContext(final String originSQL) { - SQLParserRule sqlParserRule = metaDataContexts.getMetaData().getGlobalRuleMetaData().getSingleRule(SQLParserRule.class); + SQLParserRule sqlParserRule = metaData.getGlobalRuleMetaData().getSingleRule(SQLParserRule.class); String sql = SQLHintUtils.removeHint(originSQL); HintValueContext hintValueContext = SQLHintUtils.extractHint(originSQL); - SQLStatement sqlStatement = sqlParserRule.getSQLParserEngine(metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType()).parse(sql, false); - SQLStatementContext sqlStatementContext = new SQLBindEngine(metaDataContexts.getMetaData(), databaseName, hintValueContext).bind(sqlStatement, Collections.emptyList()); + SQLStatement sqlStatement = sqlParserRule.getSQLParserEngine(metaData.getDatabase(databaseName).getProtocolType()).parse(sql, false); + SQLStatementContext sqlStatementContext = new SQLBindEngine(metaData, databaseName, hintValueContext).bind(sqlStatement, Collections.emptyList()); return new QueryContext(sqlStatementContext, sql, Collections.emptyList(), hintValueContext); } @@ -380,8 +380,8 @@ private List getQueryResults(final List resultSets) thro } private MergedResult mergeQuery(final List queryResults, final SQLStatementContext sqlStatementContext) throws SQLException { - MergeEngine mergeEngine = new MergeEngine(metaDataContexts.getMetaData().getGlobalRuleMetaData(), metaDataContexts.getMetaData().getDatabase(databaseName), - metaDataContexts.getMetaData().getProps(), connection.getDatabaseConnectionManager().getConnectionContext()); + MergeEngine mergeEngine = new MergeEngine(metaData.getGlobalRuleMetaData(), metaData.getDatabase(databaseName), + metaData.getProps(), connection.getDatabaseConnectionManager().getConnectionContext()); return mergeEngine.merge(queryResults, sqlStatementContext); } @@ -404,7 +404,7 @@ public int getResultSetHoldability() { @Override public boolean isAccumulate() { - for (DataNodeRuleAttribute each : metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getAttributes(DataNodeRuleAttribute.class)) { + for (DataNodeRuleAttribute each : metaData.getDatabase(databaseName).getRuleMetaData().getAttributes(DataNodeRuleAttribute.class)) { if (each.isNeedAccumulate(sqlStatementContext.getTablesContext().getTableNames())) { return true; } @@ -439,6 +439,6 @@ private Optional findGeneratedKey() { } private String getGeneratedKeysColumnName(final String columnName) { - return metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType() instanceof MySQLDatabaseType ? "GENERATED_KEY" : columnName; + return metaData.getDatabase(databaseName).getProtocolType() instanceof MySQLDatabaseType ? "GENERATED_KEY" : columnName; } } diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutorTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutorTest.java index a02c891ee8f6e..25c086062014e 100644 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutorTest.java +++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutorTest.java @@ -92,7 +92,7 @@ void setUp() { SQLExecutorExceptionHandler.setExceptionThrown(true); ShardingSphereConnection connection = new ShardingSphereConnection("foo_db", mockContextManager()); String processId = new UUID(ThreadLocalRandom.current().nextLong(), ThreadLocalRandom.current().nextLong()).toString().replace("-", ""); - executor = new BatchPreparedStatementExecutor(connection.getContextManager().getMetaDataContexts(), + executor = new BatchPreparedStatementExecutor(connection.getContextManager().getMetaDataContexts().getMetaData(), new JDBCExecutor(executorEngine, connection.getDatabaseConnectionManager().getConnectionContext()), "foo_db", processId); when(sqlStatementContext.getTablesContext()).thenReturn(mock(TablesContext.class)); }