Skip to content

Commit

Permalink
Add DialectDatabaseMetaData.isDDLNeedImplicitCommit() to decouple Fir…
Browse files Browse the repository at this point in the history
…ebrid database type in driver module (#33783)
  • Loading branch information
terrymanu authored Nov 24, 2024
1 parent 6baa244 commit d8e0c48
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,13 @@ default boolean isSupportThreeTierStorageStructure() {
default boolean isSupportGlobalCSN() {
return false;
}

/**
* Whether DDL need implicit commit.
*
* @return need or not
*/
default boolean isDDLNeedImplicitCommit() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public String formatTableNamePattern(final String tableNamePattern) {
return tableNamePattern.toUpperCase();
}

@Override
public boolean isDDLNeedImplicitCommit() {
return true;
}

@Override
public String getDatabaseType() {
return "Firebird";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResultSet;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroup;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupReportContext;
Expand All @@ -43,10 +45,8 @@
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.metadata.refresher.MetaDataRefreshEngine;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.DALStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
import org.apache.shardingsphere.sql.parser.statement.firebird.FirebirdStatement;

import java.sql.Connection;
import java.sql.ResultSet;
Expand Down Expand Up @@ -104,7 +104,7 @@ private boolean doExecute(final ShardingSphereDatabase database, final Execution
processEngine.executeSQL(executionGroupContext, executionContext.getQueryContext());
List<Boolean> results = jdbcExecutor.execute(executionGroupContext,
new ExecuteCallbackFactory(prepareEngine.getType()).newInstance(database, executeCallback, executionContext.getSqlStatementContext().getSqlStatement()));
if (isNeedImplicitCommit(executionContext.getQueryContext().getSqlStatementContext().getSqlStatement())) {
if (isNeedImplicitCommit(executionContext.getQueryContext().getSqlStatementContext())) {
connection.commit();
}
if (MetaDataRefreshEngine.isRefreshMetaDataRequired(executionContext.getSqlStatementContext())) {
Expand All @@ -125,10 +125,6 @@ private Collection<Statement> getStatements(final ExecutionGroup<JDBCExecutionUn
return result;
}

private boolean isNeedImplicitCommit(final SQLStatement sqlStatement) {
return !connection.getAutoCommit() && sqlStatement instanceof DDLStatement && sqlStatement instanceof FirebirdStatement;
}

private Collection<List<Object>> getParameterSets(final ExecutionGroup<JDBCExecutionUnit> executionGroup) {
Collection<List<Object>> result = new LinkedList<>();
for (JDBCExecutionUnit each : executionGroup.getInputs()) {
Expand All @@ -137,6 +133,11 @@ private Collection<List<Object>> getParameterSets(final ExecutionGroup<JDBCExecu
return result;
}

private boolean isNeedImplicitCommit(final SQLStatementContext sqlStatementContext) {
DialectDatabaseMetaData dialectDatabaseMetaData = DatabaseTypedSPILoader.getService(DialectDatabaseMetaData.class, sqlStatementContext.getDatabaseType());
return !connection.getAutoCommit() && sqlStatementContext.getSqlStatement() instanceof DDLStatement && dialectDatabaseMetaData.isDDLNeedImplicitCommit();
}

/**
* Get result set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroup;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupReportContext;
Expand All @@ -41,9 +43,7 @@
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
import org.apache.shardingsphere.mode.metadata.refresher.MetaDataRefreshEngine;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
import org.apache.shardingsphere.sql.parser.statement.firebird.FirebirdStatement;

import java.sql.Connection;
import java.sql.SQLException;
Expand Down Expand Up @@ -109,7 +109,7 @@ private int doExecuteUpdate(final ShardingSphereDatabase database, final Executi
.newInstance(database, executionContext.getQueryContext().getSqlStatementContext().getSqlStatement(), updateCallback);
List<Integer> updateCounts = jdbcExecutor.execute(executionGroupContext, callback);
if (MetaDataRefreshEngine.isRefreshMetaDataRequired(executionContext.getQueryContext().getSqlStatementContext())) {
if (isNeedImplicitCommit(executionContext.getQueryContext().getSqlStatementContext().getSqlStatement())) {
if (isNeedImplicitCommit(executionContext.getQueryContext().getSqlStatementContext())) {
connection.commit();
}
new MetaDataRefreshEngine(connection.getContextManager().getPersistServiceFacade().getMetaDataManagerPersistService(), database, props)
Expand All @@ -129,10 +129,6 @@ private Collection<Statement> getStatements(final ExecutionGroup<JDBCExecutionUn
return result;
}

private boolean isNeedImplicitCommit(final SQLStatement sqlStatement) {
return !connection.getAutoCommit() && sqlStatement instanceof DDLStatement && sqlStatement instanceof FirebirdStatement;
}

private Collection<List<Object>> getParameterSets(final ExecutionGroup<JDBCExecutionUnit> executionGroup) {
Collection<List<Object>> result = new LinkedList<>();
for (JDBCExecutionUnit each : executionGroup.getInputs()) {
Expand All @@ -141,6 +137,11 @@ private Collection<List<Object>> getParameterSets(final ExecutionGroup<JDBCExecu
return result;
}

private boolean isNeedImplicitCommit(final SQLStatementContext sqlStatementContext) {
DialectDatabaseMetaData dialectDatabaseMetaData = DatabaseTypedSPILoader.getService(DialectDatabaseMetaData.class, sqlStatementContext.getDatabaseType());
return !connection.getAutoCommit() && sqlStatementContext.getSqlStatement() instanceof DDLStatement && dialectDatabaseMetaData.isDDLNeedImplicitCommit();
}

private boolean isNeedAccumulate(final Collection<ShardingSphereRule> rules, final SQLStatementContext sqlStatementContext) {
if (!(sqlStatementContext instanceof TableAvailable)) {
return false;
Expand Down

0 comments on commit d8e0c48

Please sign in to comment.