99
99
List<String> batchQueries;
100
100
Queue<Object> cachedResultSets;
101
private boolean isRewriteable = true;
102
private String firstRewrite = null;
103
105
public boolean isStreaming() {
1086
1088
batchQueries = new ArrayList<String>();
1088
1090
batchQueries.add(sql);
1091
isInsertRewriteable(sql);
1095
* Parses the sql string to understand whether it is compatible with rewritten batches.
1096
* @param sql the sql string
1098
private void isInsertRewriteable(String sql) {
1099
if (!isRewriteable) {
1102
int index = getInsertIncipit(sql);
1104
isRewriteable = false;
1107
if (firstRewrite == null) {
1108
firstRewrite = sql.substring(0, index);
1110
boolean isRewrite = sql.startsWith(firstRewrite);
1112
isRewriteable = isRewriteable && true;
1117
* Parses the input string to understand if it is an INSERT statement.
1118
* Returns the position of the round bracket after the VALUE(S) SQL keyword,
1119
* or -1 if it cannot understand it is an INSERT statement.
1120
* Multiple statements cannot be parsed.
1121
* @param sql the input SQL statement
1122
* @return the position of the round bracket after the VALUE(S) SQL keyword,
1123
* or -1 if it cannot be parsed as an INSERT statement
1125
protected int getInsertIncipit(String sql) {
1126
String sqlUpper = sql.toUpperCase();
1127
if (! sqlUpper.startsWith("INSERT")
1128
|| sqlUpper.indexOf(";") != -1) {
1131
int idx = sqlUpper.indexOf(" VALUE");
1132
int index = sqlUpper.indexOf("(", idx);
1137
* If the batch array contains only rewriteable sql strings, returns the rewritten statement.
1138
* @return the rewritten statement
1140
private String rewrittenBatch() {
1141
StringBuilder result = null;
1143
result = new StringBuilder("");
1144
result.append(firstRewrite);
1145
for (String query : batchQueries) {
1146
result.append(query.substring(getInsertIncipit(query)));
1149
result.deleteCharAt(result.length() - 1);
1151
return (result == null ? null : result.toString());
1147
1212
int[] ret = new int[batchQueries.size()];
1150
synchronized (this.protocol) {
1151
for(; i < batchQueries.size(); i++) {
1152
execute(batchQueries.get(i));
1153
int updateCount = getUpdateCount();
1154
if (updateCount == -1) {
1155
ret[i] = SUCCESS_NO_INFO;
1157
ret[i] = updateCount;
1215
synchronized (this.protocol) {
1216
if (getProtocol().getInfo().getProperty("rewriteBatchedStatements") != null
1217
&& "true".equalsIgnoreCase(getProtocol().getInfo().getProperty("rewriteBatchedStatements"))) {
1218
ret = executeBatchAsMultiQueries();
1220
for(; i < batchQueries.size(); i++) {
1221
execute(batchQueries.get(i));
1222
int updateCount = getUpdateCount();
1223
if (updateCount == -1) {
1224
ret[i] = SUCCESS_NO_INFO;
1226
ret[i] = updateCount;
1161
1231
} catch (SQLException sqle) {
1162
1232
throw new BatchUpdateException(sqle.getMessage(), sqle.getSQLState(),Arrays.copyOf(ret, i), sqle);
1240
* Builds a new statement which contains the batched Statements and executes it.
1241
* @return an array of update counts containing one element for each command in the batch.
1242
* The elements of the array are ordered according to the order in which commands were added to the batch.
1243
* @throws SQLException
1245
private int[] executeBatchAsMultiQueries() throws SQLException {
1247
StringBuilder stringBuilder = new StringBuilder();
1248
String rewrite = rewrittenBatch();
1249
if (rewrite != null) {
1250
stringBuilder.append(rewrite);
1253
for (; i < batchQueries.size(); i++) {
1254
stringBuilder.append(batchQueries.get(i) + ";");
1257
Statement ps = connection.createStatement();
1258
ps.execute(stringBuilder.toString());
1259
return getUpdateCounts(ps, i);
1262
* Retrieves the update counts for the batched statements rewritten as
1263
* a multi query. The rewritten statement must have been executed already.
1264
* @param statement the rewritten statement
1265
* @return an array of update counts containing one element for each command in the batch.
1266
* The elements of the array are ordered according to the order in which commands were added to the batch.
1267
* @throws SQLException
1269
protected int[] getUpdateCounts(Statement statement, int size) throws SQLException {
1270
int[] result = new int[size];
1272
for (int count=0; count<size; count++) {
1273
updateCount = statement.getUpdateCount();
1274
if (updateCount == -1) {
1275
result[count] = SUCCESS_NO_INFO;
1277
result[count] = updateCount;
1279
statement.getMoreResults();
1170
1285
* Returns an object that implements the given interface to allow access to non-standard methods, or standard