Skip to content

Commit

Permalink
MycatUpdateExecuter supports batch params
Browse files Browse the repository at this point in the history
  • Loading branch information
junwen12221 committed Jan 21, 2021
1 parent f65c555 commit 41fbf0f
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions hbt/src/main/java/io/mycat/util/SQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -59,11 +60,24 @@ public UpdateResult executeUpdate(Connection connection) throws SQLException {
}

public UpdateResult executeUpdate(Connection connection,boolean autoGeneratedKeys) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(parameterizedSql, autoGeneratedKeys? RETURN_GENERATED_KEYS : NO_GENERATED_KEYS);
MycatPreparedStatementUtil.setParams(preparedStatement, parameters);
int affectedRow = preparedStatement.executeUpdate();
Long lastInsertId = autoGeneratedKeys? getInSingleSqlLastInsertId(preparedStatement) : null;
return new UpdateResult(affectedRow,lastInsertId);
List<Object> parameters = getParameters();
if (parameters instanceof List){
PreparedStatement preparedStatement = connection.prepareStatement(parameterizedSql, autoGeneratedKeys? RETURN_GENERATED_KEYS : NO_GENERATED_KEYS);
for (Object parameter : parameters) {
MycatPreparedStatementUtil.setParams(preparedStatement, (List)parameter);
preparedStatement.addBatch();
}
int[] affectedRow = preparedStatement.executeBatch();
Long lastInsertId = autoGeneratedKeys? getInSingleSqlLastInsertId(preparedStatement) : null;
return new UpdateResult(Arrays.stream(affectedRow).sum(),lastInsertId);
}else {
PreparedStatement preparedStatement = connection.prepareStatement(parameterizedSql, autoGeneratedKeys? RETURN_GENERATED_KEYS : NO_GENERATED_KEYS);
MycatPreparedStatementUtil.setParams(preparedStatement, this.parameters);
int affectedRow = preparedStatement.executeUpdate();
Long lastInsertId = autoGeneratedKeys? getInSingleSqlLastInsertId(preparedStatement) : null;
return new UpdateResult(affectedRow,lastInsertId);
}

}

/**
Expand Down

0 comments on commit 41fbf0f

Please sign in to comment.