Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/112' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Sep 20, 2017
2 parents a0e487c + 755d162 commit 90da571
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public interface CmdInformation {

void addErrorStat();

void clearErrorStat();

void addResultSetStat();

ResultSet getGeneratedKeys(Protocol protocol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ public void addErrorStat() {
this.updateCounts.add((long) Statement.EXECUTE_FAILED);
}

/**
* Clear error state, used for clear exception after first batch query, when fall back to per-query execution.
*
*/
@Override
public void clearErrorStat() {
hasException = false;
this.updateCounts.remove((long) Statement.EXECUTE_FAILED);
}

public void addResultSetStat() {
this.updateCounts.add((long) RESULT_SET_VALUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public void addErrorStat() {
this.updateCounts.add((long) Statement.EXECUTE_FAILED);
}

/**
* Clear error state, used for clear exception after first batch query, when fall back to per-query execution.
*
*/
@Override
public void clearErrorStat() {
hasException = false;
this.updateCounts.remove((long) Statement.EXECUTE_FAILED);
}


public void addResultSetStat() {
this.updateCounts.add((long) RESULT_SET_VALUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public void addErrorStat() {
//not expected
}

@Override
public void clearErrorStat() {
//not expected
}

@Override
public void addResultSetStat() {
//not expected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@ private boolean executeBulkBatch(Results results, String sql, ServerPrepareResul
getResult(results);
} catch (SQLException sqle) {
if ("HY000".equals(sqle.getSQLState()) && sqle.getErrorCode() == 1295) {
//query contain SELECT. cannot be handle by BULK protocol
//query contain SELECT or DELETE. cannot be handle by BULK protocol
// clear error and special error code, so it won't leak anywhere
// and wouldn't be misinterpreted as an additional update count
results.getCmdInformation().clearErrorStat();
return false;
}
if (exception == null) {
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/mariadb/jdbc/StatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,41 @@ public void testFractionalTimeBatch() throws SQLException {
}
}
}

@Test
public void testFallbackBatchUpdate() throws SQLException {
Assume.assumeTrue(doPrecisionTest);

createTable("testFallbackBatchUpdate", "col int");
int[] results;
int queriesInBatch = 2;
try (PreparedStatement preparedStatement = sharedConnection.prepareStatement(
"DELETE FROM testFallbackBatchUpdate WHERE col = ? ")) {
for (int i = 0; i < queriesInBatch; i++) {
preparedStatement.setInt(1, 0);
preparedStatement.addBatch();
}
results = preparedStatement.executeBatch();
}
assertEquals(results.length, queriesInBatch);
}

@Test
public void testProperBatchUpdate() throws SQLException {
Assume.assumeTrue(doPrecisionTest);

createTable("testProperBatchUpdate", "col int, col2 int");
int[] results;
int queriesInBatch = 3;
try (PreparedStatement preparedStatement = sharedConnection.prepareStatement(
"UPDATE testProperBatchUpdate set col2 = ? WHERE col = ? ")) {
for (int i = 0; i < queriesInBatch; i++) {
preparedStatement.setInt(1, i);
preparedStatement.setInt(2, i);
preparedStatement.addBatch();
}
results = preparedStatement.executeBatch();
}
assertEquals(results.length, queriesInBatch);
}
}

0 comments on commit 90da571

Please sign in to comment.