|
4 | 4 | import static org.duckdb.test.Assertions.*;
|
5 | 5 |
|
6 | 6 | import java.sql.*;
|
| 7 | +import java.util.Properties; |
7 | 8 |
|
8 | 9 | public class TestBatch {
|
9 | 10 |
|
@@ -206,4 +207,102 @@ public static void test_statement_batch_rollback() throws Exception {
|
206 | 207 | }
|
207 | 208 | }
|
208 | 209 | }
|
| 210 | + |
| 211 | + public static void test_statement_batch_autocommit_constraint_violation() throws Exception { |
| 212 | + try (Connection conn = DriverManager.getConnection(JDBC_URL)) { |
| 213 | + assertTrue(conn.getAutoCommit()); |
| 214 | + try (Statement stmt = conn.createStatement()) { |
| 215 | + stmt.execute("CREATE TABLE tab1 (col1 VARCHAR NOT NULL)"); |
| 216 | + } |
| 217 | + try (Statement stmt = conn.createStatement()) { |
| 218 | + stmt.addBatch("INSERT INTO tab1 VALUES('foo')"); |
| 219 | + stmt.addBatch("INSERT INTO tab1 VALUES(NULL)"); |
| 220 | + assertThrows(stmt::executeBatch, SQLException.class); |
| 221 | + } |
| 222 | + try (Statement stmt = conn.createStatement(); |
| 223 | + ResultSet rs = stmt.executeQuery("SELECT count(*) FROM tab1")) { |
| 224 | + rs.next(); |
| 225 | + assertEquals(rs.getLong(1), 0L); |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + public static void test_prepared_statement_batch_autocommit_constraint_violation() throws Exception { |
| 231 | + try (Connection conn = DriverManager.getConnection(JDBC_URL)) { |
| 232 | + assertTrue(conn.getAutoCommit()); |
| 233 | + try (Statement stmt = conn.createStatement()) { |
| 234 | + stmt.execute("CREATE TABLE tab1 (col1 VARCHAR NOT NULL)"); |
| 235 | + } |
| 236 | + try (PreparedStatement ps = conn.prepareStatement("INSERT INTO tab1 VALUES(?)")) { |
| 237 | + ps.setString(1, "foo"); |
| 238 | + ps.addBatch(); |
| 239 | + ps.setString(1, null); |
| 240 | + ps.addBatch(); |
| 241 | + assertThrows(ps::executeBatch, SQLException.class); |
| 242 | + } |
| 243 | + try (Statement stmt = conn.createStatement(); |
| 244 | + ResultSet rs = stmt.executeQuery("SELECT count(*) FROM tab1")) { |
| 245 | + rs.next(); |
| 246 | + assertEquals(rs.getLong(1), 0L); |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + public static void test_statement_batch_constraint_violation() throws Exception { |
| 252 | + Properties config = new Properties(); |
| 253 | + config.put(DuckDBDriver.JDBC_AUTO_COMMIT, false); |
| 254 | + try (Connection conn = DriverManager.getConnection(JDBC_URL, config)) { |
| 255 | + assertFalse(conn.getAutoCommit()); |
| 256 | + try (Statement stmt = conn.createStatement()) { |
| 257 | + stmt.execute("CREATE TABLE tab1 (col1 VARCHAR NOT NULL)"); |
| 258 | + conn.commit(); |
| 259 | + } |
| 260 | + boolean thrown = false; |
| 261 | + try (Statement stmt = conn.createStatement()) { |
| 262 | + stmt.addBatch("INSERT INTO tab1 VALUES('foo')"); |
| 263 | + stmt.addBatch("INSERT INTO tab1 VALUES(NULL)"); |
| 264 | + stmt.executeBatch(); |
| 265 | + conn.commit(); |
| 266 | + } catch (SQLException e) { |
| 267 | + thrown = true; |
| 268 | + conn.rollback(); |
| 269 | + } |
| 270 | + assertTrue(thrown); |
| 271 | + try (Statement stmt = conn.createStatement(); |
| 272 | + ResultSet rs = stmt.executeQuery("SELECT count(*) FROM tab1")) { |
| 273 | + rs.next(); |
| 274 | + assertEquals(rs.getLong(1), 0L); |
| 275 | + } |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + public static void test_prepared_statement_batch_constraint_violation() throws Exception { |
| 280 | + Properties config = new Properties(); |
| 281 | + config.put(DuckDBDriver.JDBC_AUTO_COMMIT, false); |
| 282 | + try (Connection conn = DriverManager.getConnection(JDBC_URL, config)) { |
| 283 | + assertFalse(conn.getAutoCommit()); |
| 284 | + try (Statement stmt = conn.createStatement()) { |
| 285 | + stmt.execute("CREATE TABLE tab1 (col1 VARCHAR NOT NULL)"); |
| 286 | + conn.commit(); |
| 287 | + } |
| 288 | + boolean thrown = false; |
| 289 | + try (PreparedStatement ps = conn.prepareStatement("INSERT INTO tab1 VALUES(?)")) { |
| 290 | + ps.setString(1, "foo"); |
| 291 | + ps.addBatch(); |
| 292 | + ps.setString(1, null); |
| 293 | + ps.addBatch(); |
| 294 | + ps.executeBatch(); |
| 295 | + conn.commit(); |
| 296 | + } catch (SQLException e) { |
| 297 | + thrown = true; |
| 298 | + conn.rollback(); |
| 299 | + } |
| 300 | + assertTrue(thrown); |
| 301 | + try (Statement stmt = conn.createStatement(); |
| 302 | + ResultSet rs = stmt.executeQuery("SELECT count(*) FROM tab1")) { |
| 303 | + rs.next(); |
| 304 | + assertEquals(rs.getLong(1), 0L); |
| 305 | + } |
| 306 | + } |
| 307 | + } |
209 | 308 | }
|
0 commit comments