Skip to content

Commit

Permalink
[CONJ-996] ensure BatchUpdateException inherit SQLState & vendorCode …
Browse files Browse the repository at this point in the history
…from the cause SQL exception
  • Loading branch information
rusher committed Sep 5, 2022
1 parent ea4f435 commit dfba0ad
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/mariadb/jdbc/Statement.java
Expand Up @@ -1545,7 +1545,8 @@ public List<Completion> executeInternalBatchStandard() throws SQLException {
updateCounts[i] =
completion instanceof OkPacket ? (int) ((OkPacket) completion).getAffectedRows() : 0;
}
throw new BatchUpdateException(sqle.getMessage(), updateCounts, sqle);
throw new BatchUpdateException(
sqle.getMessage(), sqle.getSQLState(), sqle.getErrorCode(), updateCounts, sqle);
} finally {
localInfileInputStream = null;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/mariadb/jdbc/export/ExceptionFactory.java
Expand Up @@ -177,7 +177,8 @@ public BatchUpdateException createBatchUpdate(
updateCounts[i] = org.mariadb.jdbc.Statement.EXECUTE_FAILED;
}
}
return new BatchUpdateException(updateCounts, sqle);
return new BatchUpdateException(
sqle.getMessage(), sqle.getSQLState(), sqle.getErrorCode(), updateCounts, sqle);
}

/**
Expand Down Expand Up @@ -210,7 +211,8 @@ public BatchUpdateException createBatchUpdate(
updateCounts[i] = Statement.SUCCESS_NO_INFO;
}
}
return new BatchUpdateException(updateCounts, sqle);
return new BatchUpdateException(
sqle.getMessage(), sqle.getSQLState(), sqle.getErrorCode(), updateCounts, sqle);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/org/mariadb/jdbc/integration/FailoverTest.java
Expand Up @@ -292,7 +292,10 @@ private void execute(Connection con, boolean transactionReplay, long threadId)
p.executeBatch();
Assertions.fail();
} catch (SQLException e) {
Throwable ee = (e instanceof BatchUpdateException) ? e.getCause() : e;
SQLException ee = (SQLException) ((e instanceof BatchUpdateException) ? e.getCause() : e);
assertEquals(ee.getMessage(), e.getMessage());
assertEquals(ee.getSQLState(), e.getSQLState());
assertEquals(ee.getErrorCode(), e.getErrorCode());
assertTrue(ee.getMessage().contains("In progress transaction was lost"));
}
}
Expand Down
20 changes: 16 additions & 4 deletions src/test/java/org/mariadb/jdbc/integration/LocalInfileTest.java
Expand Up @@ -177,10 +177,22 @@ public void throwExceptions() throws Exception {
stmt.addBatch(
"LOAD DATA LOCAL INFILE 'someFile' INTO TABLE LocalInfileInputStreamTest2 (id, test)");
stmt.addBatch("SET UNIQUE_CHECKS=1");
Common.assertThrowsContains(
BatchUpdateException.class,
stmt::executeBatch,
"Local infile is disabled by connector. Enable `allowLocalInfile` to allow local infile commands");

try {
stmt.executeBatch();
fail();
} catch (SQLException e) {
assertEquals(e.getClass(), BatchUpdateException.class);
assertTrue(
e.getMessage()
.contains(
"Local infile is disabled by connector. Enable `allowLocalInfile` to allow local infile commands"));
assertNotNull(e.getCause());
assertEquals(e.getCause().getMessage(), e.getMessage());
assertEquals(((SQLException) e.getCause()).getSQLState(), e.getSQLState());
assertEquals(((SQLException) e.getCause()).getErrorCode(), e.getErrorCode());
}

try (PreparedStatement prep =
con.prepareStatement(
"LOAD DATA LOCAL INFILE ? INTO TABLE LocalInfileInputStreamTest2 (id, test)")) {
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/mariadb/jdbc/integration/StatementTest.java
Expand Up @@ -780,6 +780,10 @@ private void executeBatchBasic(Connection con) throws SQLException {
assertTrue(
e.getMessage().contains("You have an error in your SQL syntax")
|| e.getMessage().contains("syntax error"));
assertNotNull(e.getCause());
assertEquals(e.getCause().getMessage(), e.getMessage());
assertEquals(((SQLException) e.getCause()).getSQLState(), e.getSQLState());
assertEquals(((SQLException) e.getCause()).getErrorCode(), e.getErrorCode());
}
}

Expand Down Expand Up @@ -821,6 +825,10 @@ private void executeLargeBatchBasic(Connection con) throws SQLException {
assertTrue(
e.getMessage().contains("You have an error in your SQL syntax")
|| e.getMessage().contains("syntax error"));
assertNotNull(e.getCause());
assertEquals(e.getCause().getMessage(), e.getMessage());
assertEquals(((SQLException) e.getCause()).getSQLState(), e.getSQLState());
assertEquals(((SQLException) e.getCause()).getErrorCode(), e.getErrorCode());
}
}

Expand Down

0 comments on commit dfba0ad

Please sign in to comment.