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
209 lines (181 loc) · 8.98 KB
/
TestBatch.java
File metadata and controls
209 lines (181 loc) · 8.98 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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"));
}
}
}
public static void test_prepared_statement_batch_autocommit() throws Exception {
long count = 1 << 10;
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
assertTrue(conn.getAutoCommit());
try (Statement stmt = conn.createStatement()) {
stmt.execute("CREATE TABLE tab1 (col1 BIGINT, col2 VARCHAR)");
}
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO tab1 VALUES(?, ?)")) {
for (long i = 0; i < count; i++) {
ps.setLong(1, i);
ps.setString(2, i + "foo");
ps.addBatch();
}
ps.executeBatch();
}
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM tab1")) {
rs.next();
assertEquals(rs.getLong(1), count);
}
}
}
public static void test_statement_batch_autocommit() throws Exception {
long count = 1 << 10;
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement()) {
assertTrue(conn.getAutoCommit());
stmt.execute("CREATE TABLE tab1 (col1 BIGINT, col2 VARCHAR)");
for (long i = 0; i < count; i++) {
stmt.addBatch("INSERT INTO tab1 VALUES(" + i + ", '" + i + "foo')");
}
stmt.executeBatch();
try (ResultSet rs = stmt.executeQuery("SELECT count(*) FROM tab1")) {
rs.next();
assertEquals(rs.getLong(1), count);
}
}
}
public static void test_prepared_statement_batch_rollback() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
try (Statement stmt = conn.createStatement()) {
stmt.execute("CREATE TABLE tab1 (col1 BIGINT, col2 VARCHAR)");
}
conn.setAutoCommit(false);
try (Statement stmt = conn.createStatement()) {
stmt.execute("INSERT INTO tab1 VALUES(-1, 'bar')");
}
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO tab1 VALUES(?, ?)")) {
for (long i = 0; i < 1 << 10; i++) {
ps.setLong(1, i);
ps.setString(2, i + "foo");
ps.addBatch();
}
ps.executeBatch();
}
conn.rollback();
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM tab1")) {
rs.next();
assertEquals(rs.getLong(1), 0L);
}
}
}
public static void test_statement_batch_rollback() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement()) {
stmt.execute("CREATE TABLE tab1 (col1 BIGINT, col2 VARCHAR)");
conn.setAutoCommit(false);
stmt.execute("INSERT INTO tab1 VALUES(-1, 'bar')");
for (long i = 0; i < 1 << 10; i++) {
stmt.addBatch("INSERT INTO tab1 VALUES(" + i + ", '" + i + "foo')");
}
stmt.executeBatch();
conn.rollback();
try (ResultSet rs = stmt.executeQuery("SELECT count(*) FROM tab1")) {
rs.next();
assertEquals(rs.getLong(1), 0L);
}
}
}
}