forked from crawshaw/sqlitejdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatementTest.java
More file actions
328 lines (291 loc) · 12.1 KB
/
StatementTest.java
File metadata and controls
328 lines (291 loc) · 12.1 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package test;
import java.sql.*;
import org.junit.*;
import static org.junit.Assert.*;
/** These tests are designed to stress Statements on memory databases. */
public class StatementTest
{
private Connection conn;
private Statement stat;
@BeforeClass public static void forName() throws Exception {
Class.forName("org.sqlite.JDBC");
}
@Before public void connect() throws Exception {
conn = DriverManager.getConnection("jdbc:sqlite:");
stat = conn.createStatement();
}
@After public void close() throws SQLException {
stat.close();
conn.close();
}
@Test public void stmtUpdate() throws SQLException {
assertEquals(stat.executeUpdate("create table s1 (c1);"), 0);
assertEquals(stat.executeUpdate("insert into s1 values (0);"), 1);
assertEquals(stat.executeUpdate("insert into s1 values (1);"), 1);
assertEquals(stat.executeUpdate("insert into s1 values (2);"), 1);
ResultSet rs = stat.executeQuery("select count(c1) from s1;");
assertTrue(rs.next());
assertEquals(rs.getInt(1), 3);
rs.close();
assertEquals(stat.executeUpdate("update s1 set c1 = 5;"), 3);
assertEquals(stat.executeUpdate("delete from s1;"), 3);
// TODO: assertEquals(stat.executeUpdate("drop table s1;"), 0);
}
@Test public void emptyRS() throws SQLException {
ResultSet rs = stat.executeQuery("select null limit 0;");
assertFalse(rs.next());
rs.close();
}
@Test public void singleRowRS() throws SQLException {
ResultSet rs = stat.executeQuery("select " + Integer.MAX_VALUE + ";");
assertTrue(rs.next());
assertEquals(rs.getInt(1), Integer.MAX_VALUE);
assertEquals(rs.getString(1), Integer.toString(Integer.MAX_VALUE));
assertEquals(rs.getDouble(1),
new Integer(Integer.MAX_VALUE).doubleValue());
assertFalse(rs.next());
rs.close();
}
@Test public void twoRowRS() throws SQLException {
ResultSet rs = stat.executeQuery("select 9 union all select 7;");
assertTrue(rs.next());
assertEquals(rs.getInt(1), 9);
assertTrue(rs.next());
assertEquals(rs.getInt(1), 7);
assertFalse(rs.next());
rs.close();
}
@Test public void autoClose() throws SQLException {
conn.createStatement().executeQuery("select 1;");
}
@Test public void stringRS() throws SQLException {
ResultSet rs = stat.executeQuery("select \"Russell\";");
assertTrue(rs.next());
assertEquals(rs.getString(1), "Russell");
assertFalse(rs.next());
rs.close();
}
@Test public void execute() throws SQLException {
assertTrue(stat.execute("select null;"));
ResultSet rs = stat.getResultSet();
assertNotNull(rs);
assertTrue(rs.next());
assertNull(rs.getString(1));
assertTrue(rs.wasNull());
assertFalse(stat.getMoreResults());
assertEquals(stat.getUpdateCount(), -1);
assertTrue(stat.execute("select null;"));
assertFalse(stat.getMoreResults());
assertEquals(stat.getUpdateCount(), -1);
assertFalse(stat.execute("create table test (c1);"));
assertEquals(stat.getUpdateCount(), 0);
assertFalse(stat.getMoreResults());
assertEquals(stat.getUpdateCount(), -1);
}
@Test public void colNameAccess() throws SQLException {
assertEquals(stat.executeUpdate(
"create table tab (id, firstname, surname);"), 0);
assertEquals(stat.executeUpdate(
"insert into tab values (0, 'Bob', 'Builder');"), 1);
assertEquals(stat.executeUpdate(
"insert into tab values (1, 'Fred', 'Blogs');"), 1);
assertEquals(stat.executeUpdate(
"insert into tab values (2, 'John', 'Smith');"), 1);
ResultSet rs = stat.executeQuery("select * from tab;");
assertTrue(rs.next());
assertEquals(rs.getInt("id"), 0);
assertEquals(rs.getString("firstname"), "Bob");
assertEquals(rs.getString("surname"), "Builder");
assertTrue(rs.next());
assertEquals(rs.getInt("id"), 1);
assertEquals(rs.getString("firstname"), "Fred");
assertEquals(rs.getString("surname"), "Blogs");
assertTrue(rs.next());
assertEquals(rs.getInt("id"), 2);
assertEquals( rs.getString("id"), "2");
assertEquals(rs.getString("firstname"), "John");
assertEquals(rs.getString("surname"), "Smith");
assertFalse(rs.next());
rs.close();
assertEquals(stat.executeUpdate("drop table tab;"), 1);
}
@Test public void nulls() throws SQLException {
ResultSet rs = stat.executeQuery("select null union all select null;");
assertTrue(rs.next());
assertNull(rs.getString(1));
assertTrue(rs.wasNull());
assertTrue(rs.next());
assertNull(rs.getString(1));
assertTrue(rs.wasNull());
assertFalse(rs.next());
rs.close();
}
@Test public void tempTable() throws SQLException {
assertEquals(stat.executeUpdate("create temp table myTemp (a);"), 0);
assertEquals(stat.executeUpdate("insert into myTemp values (2);"), 1);
}
@Test public void insert1000() throws SQLException {
assertEquals(stat.executeUpdate("create table in1000 (a);"), 0);
conn.setAutoCommit(false);
for (int i=0; i < 1000; i++)
assertEquals(stat.executeUpdate(
"insert into in1000 values ("+i+");"), 1);
conn.commit();
ResultSet rs = stat.executeQuery("select count(a) from in1000;");
assertTrue(rs.next());
assertEquals(rs.getInt(1), 1000);
rs.close();
assertEquals(stat.executeUpdate("drop table in1000;"), 1);
}
private void assertArrayEq(int[] a, int[] b) {
assertNotNull(a);
assertNotNull(b);
assertEquals(a.length, b.length);
for (int i=0; i < a.length; i++)
assertEquals(a[i], b[i]);
}
@Test public void batch() throws SQLException {
stat.addBatch("create table batch (c1);");
stat.addBatch("insert into batch values (1);");
stat.addBatch("insert into batch values (1);");
stat.addBatch("insert into batch values (2);");
stat.addBatch("insert into batch values (3);");
stat.addBatch("insert into batch values (4);");
stat.addBatch("insert into batch values (5);");
stat.addBatch("insert into batch values (6);");
stat.addBatch("insert into batch values (7);");
stat.addBatch("insert into batch values (8);");
stat.addBatch("insert into batch values (9);");
stat.addBatch("insert into batch values (10);");
stat.addBatch("insert into batch values (11);");
stat.addBatch("insert into batch values (12);");
assertArrayEq(new int[] { 0,1,1,1,1,1,1,1,1,1,1,1,1,1 },
stat.executeBatch());
assertArrayEq(new int[] { }, stat.executeBatch());
stat.clearBatch();
stat.addBatch("insert into batch values (9);");
assertArrayEq(new int[] { 1 }, stat.executeBatch());
assertArrayEq(new int[] {}, stat.executeBatch());
stat.clearBatch();
stat.addBatch("insert into batch values (7);");
stat.addBatch("insert into batch values (7);");
assertArrayEq(new int[] { 1, 1 }, stat.executeBatch());
stat.clearBatch();
ResultSet rs = stat.executeQuery("select count(*) from batch;");
assertTrue(rs.next());
assertEquals(16, rs.getInt(1));
rs.close();
}
@Test public void closeOnFalseNext() throws SQLException {
stat.executeUpdate("create table t1 (c1);");
conn.createStatement().executeQuery("select * from t1;").next();
stat.executeUpdate("drop table t1;");
}
@Test public void getGeneratedKeys() throws SQLException {
ResultSet rs;
stat.executeUpdate("create table t1 (c1 integer primary key, v);");
stat.executeUpdate("insert into t1 (v) values ('red');");
rs = stat.getGeneratedKeys();
assertTrue(rs.next());
assertEquals(rs.getInt(1), 1);
rs.close();
stat.executeUpdate("insert into t1 (v) values ('blue');");
rs = stat.getGeneratedKeys();
assertTrue(rs.next());
assertEquals(rs.getInt(1), 2);
rs.close();
}
@Test public void isBeforeFirst() throws SQLException {
ResultSet rs = stat.executeQuery("select 1 union all select 2;");
assertTrue(rs.isBeforeFirst());
assertTrue(rs.next());
assertTrue(rs.isFirst());
assertEquals(rs.getInt(1), 1);
assertTrue(rs.next());
assertFalse(rs.isBeforeFirst());
assertFalse(rs.isFirst());
assertEquals(rs.getInt(1), 2);
assertFalse(rs.next());
assertFalse(rs.isBeforeFirst());
rs.close();
}
@Test public void columnNaming() throws SQLException {
stat.executeUpdate("create table t1 (c1 integer);");
stat.executeUpdate("create table t2 (c1 integer);");
stat.executeUpdate("insert into t1 values (1);");
stat.executeUpdate("insert into t2 values (1);");
ResultSet rs = stat.executeQuery(
"select a.c1 AS c1 from t1 a, t2 where a.c1=t2.c1;");
assertTrue(rs.next());
assertEquals(rs.getInt("c1"), 1);
rs.close();
}
@Test public void maxRows() throws SQLException {
stat.setMaxRows(1);
assertEquals(stat.getMaxRows(), 1);
ResultSet rs = stat.executeQuery("select 1 union select 2;");
assertTrue(rs.next());
assertEquals(rs.getInt(1), 1);
assertFalse(rs.next());
rs.close();
}
@Test public void nullDate() throws SQLException {
ResultSet rs = stat.executeQuery("select null;");
assertTrue(rs.next());
assertEquals(rs.getDate(1), null);
assertEquals(rs.getTime(1), null);
assertEquals(rs.getTimestamp(1), null);
rs.close();
}
@Test(expected= SQLException.class)
public void ambiguousColumnNaming() throws SQLException {
stat.executeUpdate("create table t1 (c1 int);");
stat.executeUpdate("create table t2 (c1 int, c2 int);");
stat.executeUpdate("insert into t1 values (1);");
stat.executeUpdate("insert into t2 values (2, 1);");
ResultSet rs = stat.executeQuery(
"select a.c1, b.c1 from t1 a, t2 b where a.c1=b.c2;");
assertTrue(rs.next());
assertEquals(rs.getInt("c1"), 1);
rs.close();
}
@Test(expected= SQLException.class)
public void failToDropWhenRSOpen() throws SQLException {
stat.executeUpdate("create table t1 (c1);");
stat.executeUpdate("insert into t1 values (4);");
stat.executeUpdate("insert into t1 values (4);");
conn.createStatement().executeQuery("select * from t1;").next();
stat.executeUpdate("drop table t1;");
}
@Test(expected= SQLException.class)
public void executeNoRS() throws SQLException {
assertFalse(stat.execute("insert into test values (8);"));
stat.getResultSet();
}
@Test(expected= SQLException.class)
public void executeClearRS() throws SQLException {
assertTrue(stat.execute("select null;"));
assertNotNull(stat.getResultSet());
assertFalse(stat.getMoreResults());
stat.getResultSet();
}
@Test(expected= BatchUpdateException.class)
public void batchReturnsResults() throws SQLException {
stat.addBatch("select null;");
stat.executeBatch();
}
@Test(expected= SQLException.class)
public void noSuchTable() throws SQLException {
stat.executeQuery("select * from doesnotexist;");
}
@Test(expected= SQLException.class)
public void noSuchCol() throws SQLException {
stat.executeQuery("select notacol from (select 1);");
}
@Test(expected= SQLException.class)
public void noSuchColName() throws SQLException {
ResultSet rs = stat.executeQuery("select 1;");
assertTrue(rs.next());
rs.getInt("noSuchColName");
}
}