forked from duckdb/duckdb-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBatch.java
More file actions
127 lines (103 loc) · 5.51 KB
/
TestBatch.java
File metadata and controls
127 lines (103 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package org.duckdb;
import static org.duckdb.TestDuckDBJDBC.JDBC_URL;
import static org.duckdb.test.Assertions.*;
import java.sql.*;
public class TestBatch {
public static void test_batch_prepared_statement() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
try (Statement s = conn.createStatement()) {
s.execute("CREATE TABLE test (x INT, y INT, z INT)");
}
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO test (x, y, z) VALUES (?, ?, ?);")) {
ps.setObject(1, 1);
ps.setObject(2, 2);
ps.setObject(3, 3);
ps.addBatch();
ps.setObject(1, 4);
ps.setObject(2, 5);
ps.setObject(3, 6);
ps.addBatch();
ps.executeBatch();
}
try (Statement s = conn.createStatement(); ResultSet rs = s.executeQuery("SELECT * FROM test ORDER BY x")) {
rs.next();
assertEquals(rs.getInt(1), rs.getObject(1, Integer.class));
assertEquals(rs.getObject(1, Integer.class), 1);
assertEquals(rs.getInt(2), rs.getObject(2, Integer.class));
assertEquals(rs.getObject(2, Integer.class), 2);
assertEquals(rs.getInt(3), rs.getObject(3, Integer.class));
assertEquals(rs.getObject(3, Integer.class), 3);
rs.next();
assertEquals(rs.getInt(1), rs.getObject(1, Integer.class));
assertEquals(rs.getObject(1, Integer.class), 4);
assertEquals(rs.getInt(2), rs.getObject(2, Integer.class));
assertEquals(rs.getObject(2, Integer.class), 5);
assertEquals(rs.getInt(3), rs.getObject(3, Integer.class));
assertEquals(rs.getObject(3, Integer.class), 6);
}
}
}
public static void test_batch_statement() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
try (Statement s = conn.createStatement()) {
s.execute("CREATE TABLE test (x INT, y INT, z INT)");
s.addBatch("INSERT INTO test (x, y, z) VALUES (1, 2, 3);");
s.addBatch("INSERT INTO test (x, y, z) VALUES (4, 5, 6);");
s.executeBatch();
}
try (Statement s2 = conn.createStatement();
ResultSet rs = s2.executeQuery("SELECT * FROM test ORDER BY x")) {
rs.next();
assertEquals(rs.getInt(1), rs.getObject(1, Integer.class));
assertEquals(rs.getObject(1, Integer.class), 1);
assertEquals(rs.getInt(2), rs.getObject(2, Integer.class));
assertEquals(rs.getObject(2, Integer.class), 2);
assertEquals(rs.getInt(3), rs.getObject(3, Integer.class));
assertEquals(rs.getObject(3, Integer.class), 3);
rs.next();
assertEquals(rs.getInt(1), rs.getObject(1, Integer.class));
assertEquals(rs.getObject(1, Integer.class), 4);
assertEquals(rs.getInt(2), rs.getObject(2, Integer.class));
assertEquals(rs.getObject(2, Integer.class), 5);
assertEquals(rs.getInt(3), rs.getObject(3, Integer.class));
assertEquals(rs.getObject(3, Integer.class), 6);
}
}
}
public static void test_execute_while_batch() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
try (Statement s = conn.createStatement()) {
s.execute("CREATE TABLE test (id INT)");
}
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO test (id) VALUES (?)")) {
ps.setObject(1, 1);
ps.addBatch();
String msg =
assertThrows(() -> { ps.execute("INSERT INTO test (id) VALUES (1);"); }, SQLException.class);
assertTrue(msg.contains("Batched queries must be executed with executeBatch."));
String msg2 =
assertThrows(() -> { ps.executeUpdate("INSERT INTO test (id) VALUES (1);"); }, SQLException.class);
assertTrue(msg2.contains("Batched queries must be executed with executeBatch."));
String msg3 = assertThrows(() -> { ps.executeQuery("SELECT * FROM test"); }, SQLException.class);
assertTrue(msg3.contains("Batched queries must be executed with executeBatch."));
}
}
}
public static void test_prepared_statement_batch_exception() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
try (Statement s = conn.createStatement()) {
s.execute("CREATE TABLE test (id INT)");
}
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO test (id) VALUES (?)")) {
String msg = assertThrows(() -> { ps.addBatch("DUMMY SQL"); }, SQLException.class);
assertTrue(msg.contains("Cannot add batched SQL statement to PreparedStatement"));
}
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO test (id) VALUES (?)")) {
ps.setString(1, "foo");
ps.addBatch();
String msg = assertThrows(ps::executeBatch, SQLException.class);
assertTrue(msg.contains("Conversion Error: Could not convert string 'foo' to INT32"));
}
}
}
}