From 41fbf0f67d05542152e84d35ebc24ebc0eb9d347 Mon Sep 17 00:00:00 2001 From: jamie12221 Date: Thu, 21 Jan 2021 16:32:40 +0800 Subject: [PATCH] MycatUpdateExecuter supports batch params --- hbt/src/main/java/io/mycat/util/SQL.java | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/hbt/src/main/java/io/mycat/util/SQL.java b/hbt/src/main/java/io/mycat/util/SQL.java index 6117d99bd..fd1419e67 100644 --- a/hbt/src/main/java/io/mycat/util/SQL.java +++ b/hbt/src/main/java/io/mycat/util/SQL.java @@ -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; @@ -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 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); + } + } /**