Skip to content

Commit

Permalink
fix #1205, remove isReturnGeneratedKeys from SQLExecutePrepareCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 1, 2018
1 parent 37c264d commit 6ef1a0f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ public interface SQLExecutePrepareCallback {
* Create statement execute unit.
*
* @param connection connection
* @param isReturnGeneratedKeys is return generated keys
* @param sqlExecutionUnit SQL execution unit
* @return statement execute unit
* @throws SQLException SQL exception
*/
StatementExecuteUnit createStatementExecuteUnit(Connection connection, boolean isReturnGeneratedKeys, SQLExecutionUnit sqlExecutionUnit) throws SQLException;
StatementExecuteUnit createStatementExecuteUnit(Connection connection, SQLExecutionUnit sqlExecutionUnit) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,33 @@ public final class SQLExecutePrepareTemplate {
* Get statement execute units.
*
* @param sqlUnitGroups SQL unit groups
* @param isReturnGeneratedKeys is return generated keys
* @param callback SQL execute prepare callback
* @return key is data source name, value is statement execute unit groups
* @throws SQLException SQL exception
*/
public Map<String, List<List<StatementExecuteUnit>>> getStatementExecuteUnits(
final Map<String, List<SQLUnit>> sqlUnitGroups, final boolean isReturnGeneratedKeys, final SQLExecutePrepareCallback callback) throws SQLException {
public Map<String, List<List<StatementExecuteUnit>>> getStatementExecuteUnits(final Map<String, List<SQLUnit>> sqlUnitGroups, final SQLExecutePrepareCallback callback) throws SQLException {
Map<String, List<List<StatementExecuteUnit>>> result = new HashMap<>(sqlUnitGroups.size(), 1);
for (Entry<String, List<SQLUnit>> entry : sqlUnitGroups.entrySet()) {
result.put(entry.getKey(), partitionSQLUnits(entry.getKey(), entry.getValue(), isReturnGeneratedKeys, callback));
result.put(entry.getKey(), partitionSQLUnits(entry.getKey(), entry.getValue(), callback));
}
return result;
}

private List<List<StatementExecuteUnit>> partitionSQLUnits(
final String dataSourceName, final List<SQLUnit> sqlUnits, final boolean isReturnGeneratedKeys, final SQLExecutePrepareCallback callback) throws SQLException {
private List<List<StatementExecuteUnit>> partitionSQLUnits(final String dataSourceName, final List<SQLUnit> sqlUnits, final SQLExecutePrepareCallback callback) throws SQLException {
List<List<StatementExecuteUnit>> result = new LinkedList<>();
int desiredPartitionSize = Math.max(sqlUnits.size() / maxConnectionsSizePerQuery, 1);
for (List<SQLUnit> each : Lists.partition(sqlUnits, desiredPartitionSize)) {
// TODO get connection sync to prevent dead lock
result.add(getStatementExecuteUnitGroup(callback.getConnection(dataSourceName), dataSourceName, isReturnGeneratedKeys, each, callback));
result.add(getStatementExecuteUnitGroup(callback.getConnection(dataSourceName), dataSourceName, each, callback));
}
return result;
}

private List<StatementExecuteUnit> getStatementExecuteUnitGroup(final Connection connection, final String dataSourceName, final boolean isReturnGeneratedKeys,
final List<SQLUnit> sqlUnitGroup, final SQLExecutePrepareCallback callback) throws SQLException {
private List<StatementExecuteUnit> getStatementExecuteUnitGroup(
final Connection connection, final String dataSourceName, final List<SQLUnit> sqlUnitGroup, final SQLExecutePrepareCallback callback) throws SQLException {
List<StatementExecuteUnit> result = new LinkedList<>();
for (SQLUnit each : sqlUnitGroup) {
result.add(callback.createStatementExecuteUnit(connection, isReturnGeneratedKeys, new SQLExecutionUnit(dataSourceName, each)));
result.add(callback.createStatementExecuteUnit(connection, new SQLExecutionUnit(dataSourceName, each)));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,15 @@ private Collection<PreparedStatementUnit> getExecuteUnitsForMemoryStrictly() thr
@SuppressWarnings("unchecked")
private Map<String, List<List<PreparedStatementUnit>>> getExecuteUnitsForConnectionStrictly() throws SQLException {
SQLExecutePrepareTemplate sqlExecutePrepareTemplate = new SQLExecutePrepareTemplate(connection.getShardingDataSource().getShardingContext().getMaxConnectionsSizePerQuery());
return (Map) sqlExecutePrepareTemplate.getStatementExecuteUnits(routeResult.getSQLUnitGroups(), returnGeneratedKeys, new SQLExecutePrepareCallback() {
return (Map) sqlExecutePrepareTemplate.getStatementExecuteUnits(routeResult.getSQLUnitGroups(), new SQLExecutePrepareCallback() {

@Override
public Connection getConnection(final String dataSourceName) throws SQLException {
return ShardingPreparedStatement.this.connection.getConnection(dataSourceName);
}

@Override
public StatementExecuteUnit createStatementExecuteUnit(final Connection connection, final boolean isReturnGeneratedKeys, final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
public StatementExecuteUnit createStatementExecuteUnit(final Connection connection, final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
return getPreparedStatementUnit(connection, sqlExecutionUnit);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,15 @@ private Collection<StatementUnit> getExecuteUnitsForMemoryStrictly() throws SQLE
@SuppressWarnings("unchecked")
private Map<String, List<List<StatementUnit>>> getExecuteUnitsForConnectionStrictly() throws SQLException {
SQLExecutePrepareTemplate sqlExecutePrepareTemplate = new SQLExecutePrepareTemplate(connection.getShardingDataSource().getShardingContext().getMaxConnectionsSizePerQuery());
return (Map) sqlExecutePrepareTemplate.getStatementExecuteUnits(routeResult.getSQLUnitGroups(), returnGeneratedKeys, new SQLExecutePrepareCallback() {
return (Map) sqlExecutePrepareTemplate.getStatementExecuteUnits(routeResult.getSQLUnitGroups(), new SQLExecutePrepareCallback() {

@Override
public Connection getConnection(final String dataSourceName) throws SQLException {
return ShardingStatement.this.connection.getConnection(dataSourceName);
}

@Override
public StatementExecuteUnit createStatementExecuteUnit(final Connection connection, final boolean isReturnGeneratedKeys, final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
public StatementExecuteUnit createStatementExecuteUnit(final Connection connection, final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
return getStatementUnit(connection, sqlExecutionUnit);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.shardingsphere.proxy.backend.jdbc.wrapper.JDBCExecutorWrapper;
import io.shardingsphere.proxy.config.RuleRegistry;
import io.shardingsphere.proxy.transport.mysql.packet.command.query.QueryResponsePackets;
import lombok.RequiredArgsConstructor;

import java.sql.Connection;
import java.sql.ResultSet;
Expand Down Expand Up @@ -76,7 +77,7 @@ public ExecuteResponse execute(final SQLRouteResult routeResult) throws SQLExcep
boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
Map<String, List<List<StatementExecuteUnit>>> statementExecuteUnits =
sqlExecutePrepareTemplate.getStatementExecuteUnits(routeResult.getSQLUnitGroups(), isReturnGeneratedKeys, new ConnectionStrictlySQLExecutePrepareCallback());
sqlExecutePrepareTemplate.getStatementExecuteUnits(routeResult.getSQLUnitGroups(), new ConnectionStrictlySQLExecutePrepareCallback(isReturnGeneratedKeys));
Collection<ExecuteResponseUnit> executeResponseUnits = sqlExecuteTemplate.execute((Map) statementExecuteUnits,
new FirstConnectionStrictlySQLExecuteCallback(sqlType, isExceptionThrown, dataMap, isReturnGeneratedKeys),
new ConnectionStrictlySQLExecuteCallback(sqlType, isExceptionThrown, dataMap, isReturnGeneratedKeys));
Expand All @@ -98,15 +99,18 @@ protected QueryResult createQueryResult(final ResultSet resultSet) throws SQLExc
return new MemoryQueryResult(resultSet);
}

@RequiredArgsConstructor
private final class ConnectionStrictlySQLExecutePrepareCallback implements SQLExecutePrepareCallback {

private final boolean isReturnGeneratedKeys;

@Override
public Connection getConnection(final String dataSourceName) throws SQLException {
return getBackendConnection().getConnection(dataSourceName);
}

@Override
public StatementExecuteUnit createStatementExecuteUnit(final Connection connection, final boolean isReturnGeneratedKeys, final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
public StatementExecuteUnit createStatementExecuteUnit(final Connection connection, final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
return new ProxyStatementExecuteUnit(sqlExecutionUnit, getJdbcExecutorWrapper().createStatement(connection, sqlExecutionUnit.getSqlUnit().getSql(), isReturnGeneratedKeys));
}
}
Expand Down

0 comments on commit 6ef1a0f

Please sign in to comment.