75
75
private boolean useFractionalSeconds;
76
76
boolean parametersCleared;
77
77
List<MySQLPreparedStatement> batchPreparedStatements;
78
private boolean isRewriteable = true;
79
private String firstRewrite = null;
80
82
public MySQLPreparedStatement(MySQLConnection connection,
194
196
batchPreparedStatements = new ArrayList<MySQLPreparedStatement>();
196
198
batchPreparedStatements.add(new MySQLPreparedStatement(connection,sql, dQuery, useFractionalSeconds));
199
isInsertRewriteable(sql);
198
201
public void addBatch(final String sql) throws SQLException {
199
202
if (batchPreparedStatements == null) {
200
203
batchPreparedStatements = new ArrayList<MySQLPreparedStatement>();
202
205
batchPreparedStatements.add(new MySQLPreparedStatement(connection, sql));
206
isInsertRewriteable(sql);
205
209
public void clearBatch() {
206
210
if (batchPreparedStatements != null) {
207
211
batchPreparedStatements.clear();
214
isRewriteable = true;
218
* Parses the sql string to understand whether it is compatible with rewritten batches.
219
* @param sql the sql string
221
private void isInsertRewriteable(String sql) {
222
if (!isRewriteable) {
225
int index = getInsertIncipit(sql);
227
isRewriteable = false;
230
if (firstRewrite == null) {
231
firstRewrite = sql.substring(0, index);
233
boolean isRewrite = sql.startsWith(firstRewrite);
235
isRewriteable = isRewriteable && true;
240
* If the batch array contains only rewriteable sql strings, returns the rewritten statement.
241
* @return the rewritten statement
243
private String rewrittenBatch() {
244
StringBuilder result = null;
246
result = new StringBuilder("");
247
result.append(firstRewrite);
248
for (MySQLPreparedStatement mySQLPS : batchPreparedStatements) {
249
String query = mySQLPS.dQuery.toSQL();
250
result.append(query.substring(getInsertIncipit(query)));
253
result.deleteCharAt(result.length() - 1);
255
return (result == null ? null : result.toString());
216
263
int[] ret = new int[batchPreparedStatements.size()];
219
synchronized (this.getProtocol()) {
220
for(; i < batchPreparedStatements.size(); i++) {
221
PreparedStatement ps = batchPreparedStatements.get(i);
223
int updateCount = ps.getUpdateCount();
224
if (updateCount == -1) {
225
ret[i] = SUCCESS_NO_INFO;
227
ret[i] = updateCount;
266
synchronized (this.getProtocol()) {
267
if (getProtocol().getInfo().getProperty("rewriteBatchedStatements") != null
268
&& "true".equalsIgnoreCase(getProtocol().getInfo().getProperty("rewriteBatchedStatements"))) {
269
ret = executeBatchAsMultiQueries();
271
for (; i < batchPreparedStatements.size(); i++) {
272
PreparedStatement ps = batchPreparedStatements.get(i);
274
int updateCount = ps.getUpdateCount();
275
if (updateCount == -1) {
276
ret[i] = SUCCESS_NO_INFO;
278
ret[i] = updateCount;
231
283
} catch (SQLException sqle) {
232
284
throw new BatchUpdateException(sqle.getMessage(), sqle.getSQLState(), Arrays.copyOf(ret, i), sqle);
292
* Builds a new statement which contains the batched Statements and executes it.
293
* @return an array of update counts containing one element for each command in the batch.
294
* The elements of the array are ordered according to the order in which commands were added to the batch.
295
* @throws SQLException
297
private int[] executeBatchAsMultiQueries() throws SQLException {
298
StringBuilder stringBuilder = new StringBuilder();
300
String rewrite = rewrittenBatch();
301
if (rewrite != null) {
302
stringBuilder.append(rewrite);
305
for (; i < batchPreparedStatements.size(); i++) {
306
stringBuilder.append(batchPreparedStatements.get(i).dQuery.toSQL() + ";");
309
Statement ps = connection.createStatement();
310
ps.execute(stringBuilder.toString());
311
return getUpdateCounts(ps, i);