/
SelectTests.java
639 lines (561 loc) · 23.7 KB
/
SelectTests.java
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/**
* Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq.test._.testcases;
import static java.util.Arrays.asList;
// ...
import static org.jooq.impl.DSL.count;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.selectOne;
import static org.jooq.impl.DSL.selectZero;
import static org.jooq.impl.DSL.trim;
import static org.jooq.impl.DSL.val;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.sql.Date;
import java.util.Vector;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.Record2;
import org.jooq.Record3;
import org.jooq.Record6;
import org.jooq.Record8;
import org.jooq.Result;
import org.jooq.Select;
import org.jooq.SelectQuery;
import org.jooq.Table;
import org.jooq.TableRecord;
import org.jooq.UpdatableRecord;
import org.jooq.exception.DataAccessException;
import org.jooq.impl.DefaultConnectionProvider;
import org.jooq.test.BaseTest;
import org.jooq.test.jOOQAbstractTest;
import org.junit.Test;
public class SelectTests<
A extends UpdatableRecord<A> & Record6<Integer, String, String, Date, Integer, ?>,
AP,
B extends UpdatableRecord<B>,
S extends UpdatableRecord<S> & Record1<String>,
B2S extends UpdatableRecord<B2S> & Record3<String, Integer, Integer>,
BS extends UpdatableRecord<BS>,
L extends TableRecord<L> & Record2<String, String>,
X extends TableRecord<X>,
DATE extends UpdatableRecord<DATE>,
BOOL extends UpdatableRecord<BOOL>,
D extends UpdatableRecord<D>,
T extends UpdatableRecord<T>,
U extends TableRecord<U>,
UU extends UpdatableRecord<UU>,
I extends TableRecord<I>,
IPK extends UpdatableRecord<IPK>,
T725 extends UpdatableRecord<T725>,
T639 extends UpdatableRecord<T639>,
T785 extends TableRecord<T785>,
CASE extends UpdatableRecord<CASE>>
extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T725, T639, T785, CASE> {
public SelectTests(jOOQAbstractTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T725, T639, T785, CASE> delegate) {
super(delegate);
}
@Test
public void testSelectSimpleQuery() throws Exception {
SelectQuery<?> q = create().selectQuery();
Field<Integer> f1 = val(1).as("f1");
Field<Double> f2 = val(2d).as("f2");
Field<String> f3 = val("test").as("f3");
q.addSelect(f1);
q.addSelect(f2);
q.addSelect(f3);
int i = q.execute();
Result<?> result = q.getResult();
assertEquals(1, i);
assertEquals(1, result.size());
assertEquals(3, result.fieldsRow().size());
assertTrue(asList(result.fields()).contains(f1));
assertTrue(asList(result.fields()).contains(f2));
assertTrue(asList(result.fields()).contains(f3));
assertEquals(3, result.get(0).fieldsRow().size());
assertTrue(asList(result.get(0).fields()).contains(f1));
assertTrue(asList(result.get(0).fields()).contains(f2));
assertTrue(asList(result.get(0).fields()).contains(f3));
assertEquals(Integer.valueOf(1), result.get(0).getValue(f1));
assertEquals(2d, result.get(0).getValue(f2), 0.0);
assertEquals("test", result.get(0).getValue(f3));
}
@Test
public void testSelectQuery() throws Exception {
SelectQuery<?> q = create().selectQuery();
q.addFrom(TAuthor());
q.addSelect(TAuthor().fields());
q.addOrderBy(TAuthor_LAST_NAME());
int rows = q.execute();
Result<?> result = q.getResult();
assertEquals(2, rows);
assertEquals(2, result.size());
assertEquals("Coelho", result.get(0).getValue(TAuthor_LAST_NAME()));
assertEquals("Orwell", result.get(1).getValue(TAuthor_LAST_NAME()));
}
@Test
public void testDistinctQuery() throws Exception {
Result<Record1<Integer>> result = create()
.selectDistinct(TBook_AUTHOR_ID())
.from(TBook())
.orderBy(TBook_AUTHOR_ID())
.fetch();
assertEquals(2, result.size());
assertEquals(Integer.valueOf(1), result.get(0).getValue(TBook_AUTHOR_ID()));
assertEquals(Integer.valueOf(2), result.get(1).getValue(TBook_AUTHOR_ID()));
assertEquals(2, create()
.selectDistinct(TBook_AUTHOR_ID())
.from(TBook())
.fetch()
.size());
}
@Test
public void testSubSelect() throws Exception {
// ---------------------------------------------------------------------
// [#493, #632] Testing filtering by a select's outcome
// ---------------------------------------------------------------------
// TODO [#632] Handle this for Sybase
assertEquals("Coelho",
create().select(TAuthor_LAST_NAME())
.from(TAuthor())
.where(val(0).equal(
select(count(TBook_ID()))
.from(TBook())
.where(TBook_AUTHOR_ID().equal(TAuthor_ID()))
.and(TBook_TITLE().equal("1984"))))
// SQLite doesn't support {=|<>|<|>|<=|>=} {ANY|ALL|SOME}, which
// is checked in PredicateTests. But do check simpler subqueries
.and(val(100).notEqual(selectOne()))
.and(val(0).lessThan(selectOne()))
.and(val(1).lessOrEqual(selectOne()))
.and(val(1).greaterThan(selectZero()))
.and(val(0).greaterOrEqual(selectZero()))
.limit(1)
.fetchOne(TAuthor_LAST_NAME()));
}
@Test
public void testSelectWithINPredicate() throws Exception {
assertEquals(3,
create().selectFrom(TBook())
.where(TBook_TITLE().notIn(
select(TBook_TITLE())
.from(TBook())
.where(TBook_TITLE().in("1984"))))
.execute());
}
@Test
public void testSelectWithExistsPredicate() throws Exception {
assertEquals(3,
create()
.selectFrom(TBook())
.whereNotExists(
selectOne()
.from(TAuthor())
.where(TAuthor_YEAR_OF_BIRTH().greaterOrEqual(TBook_PUBLISHED_IN())))
// Add additional useless queries to check query correctness
.orNotExists(select())
.andExists(select()).execute());
}
@Test
public void testSelectFromSelect() throws Exception {
Table<Record2<Integer, Integer>> nested = create().select(TBook_AUTHOR_ID(), count().as("books"))
.from(TBook())
.groupBy(TBook_AUTHOR_ID()).asTable("nested");
Result<Record> records = create().select(nested.fields())
.from(nested)
.orderBy(nested.field("books"), nested.field(TBook_AUTHOR_ID())).fetch();
assertEquals(2, records.size());
assertEquals(Integer.valueOf(1), records.getValue(0, nested.field(TBook_AUTHOR_ID())));
assertEquals(Integer.valueOf(2), records.getValue(0, nested.field("books")));
assertEquals(Integer.valueOf(2), records.getValue(1, nested.field(TBook_AUTHOR_ID())));
assertEquals(Integer.valueOf(2), records.getValue(1, nested.field("books")));
}
@Test
public void testSelectWithSubselectProjection() throws Exception {
Field<Object> books = create().select(count())
.from(TBook())
.where(TBook_AUTHOR_ID().equal(TAuthor_ID())).asField("books");
Result<Record2<Integer, Object>> records = create().select(TAuthor_ID(), books)
.from(TAuthor())
.orderBy(books, TAuthor_ID()).fetch();
assertEquals(2, records.size());
assertEquals(Integer.valueOf(1), records.getValue(0, TAuthor_ID()));
assertEquals(Integer.valueOf(2), records.getValue(0, books));
assertEquals(Integer.valueOf(2), records.getValue(1, TAuthor_ID()));
assertEquals(Integer.valueOf(2), records.getValue(1, books));
}
@Test
public void testUnaliasedSubqueryProjections() throws Exception {
// TODO [#579] re-enable this test when fixing this bug
// Test whether unaliased literals in subquery projections are correctly
// handled
Result<Record> result1 =
create().select()
.from(
selectOne(),
select(val(2)),
select(val(2)),
select(val(2)),
select(val(3).add(4)),
select(val(3).add(4)),
select(trim(" test ")),
select(trim(" test ")))
.fetch();
assertEquals(1, result1.size());
assertEquals(Integer.valueOf(1), result1.getValue(0, 0));
assertEquals(Integer.valueOf(2), result1.getValue(0, 1));
assertEquals(Integer.valueOf(2), result1.getValue(0, val(2)));
assertEquals(Integer.valueOf(2), result1.getValue(0, 3));
assertEquals(Integer.valueOf(7), result1.getValue(0, val(3).add(4)));
assertEquals(Integer.valueOf(7), result1.getValue(0, 5));
assertEquals("test", result1.getValue(0, trim(" test ")));
assertEquals("test", result1.getValue(0, 7));
Result<Record8<Object, Object, Object, Object, Object, Object, Object, Object>> result2 =
create().select(
create().selectOne().asField(),
create().select(val(2)).asField(),
create().select(val(2)).asField(),
create().select(val(2)).asField(),
create().select(val(3).add(4)).asField(),
create().select(val(3).add(4)).asField(),
create().select(trim(" test ")).asField(),
create().select(trim(" test ")).asField())
.fetch();
assertEquals(1, result2.size());
assertEquals(1, result2.getValue(0, 0));
assertEquals(2, result2.getValue(0, 1));
assertEquals(2, result2.getValue(0, 2));
assertEquals(2, result2.getValue(0, 3));
assertEquals(7, result2.getValue(0, 4));
assertEquals(7, result2.getValue(0, 5));
assertEquals("test", result2.getValue(0, 6));
assertEquals("test", result2.getValue(0, 7));
}
@Test
public void testCombinedSelectQuery() throws Exception {
SelectQuery<B> q1 = create().selectQuery(TBook());
SelectQuery<B> q2 = create().selectQuery(TBook());
q1.addConditions(TBook_AUTHOR_ID().equal(1));
q2.addConditions(TBook_TITLE().equal("Brida"));
// Use union all because of clob's
Select<?> union = q1.unionAll(q2);
int rows = union.execute();
assertEquals(3, rows);
// Use union all because of clob's
rows = create().selectDistinct(union.field(TBook_AUTHOR_ID()), TAuthor_FIRST_NAME())
.from(union)
.join(TAuthor())
.on(union.field(TBook_AUTHOR_ID()).equal(TAuthor_ID()))
.orderBy(TAuthor_FIRST_NAME())
.execute();
assertEquals(2, rows);
}
@Test
public void testComplexUnions() throws Exception {
Select<Record1<String>> s1 = create().select(TBook_TITLE()).from(TBook()).where(TBook_ID().equal(1));
Select<Record1<String>> s2 = create().select(TBook_TITLE()).from(TBook()).where(TBook_ID().equal(2));
Select<Record1<String>> s3 = create().select(TBook_TITLE()).from(TBook()).where(TBook_ID().equal(3));
Select<Record1<String>> s4 = create().select(TBook_TITLE()).from(TBook()).where(TBook_ID().equal(4));
Result<Record> result = create().select().from(s1.union(s2).union(s3).union(s4)).fetch();
assertEquals(4, result.size());
result = create().select().from(s1.union(s2).union(s3.union(s4))).fetch();
assertEquals(4, result.size());
assertEquals(4, create().selectFrom(s1.union(
create().selectFrom(s2.unionAll(
create().selectFrom(s3.union(s4).asTable())
).asTable())
).asTable())
.fetch().size());
// [#289] Handle bad syntax scenario provided by user Gunther
Select<Record1<Integer>> q = create().select(val(2008).as("y"));
for (int year = 2009; year <= 2011; year++) {
q = q.union(create().select(val(year).as("y")));
}
assertEquals(4, q.execute());
}
@Test
public void testForUpdateClauses() throws Exception {
switch (dialect().family()) {
/* [pro] xx
xxxx xxxxxxx
xx [/pro] */
case SQLITE:
log.info("SKIPPING", "FOR UPDATE tests");
return;
}
// Checking for syntax correctness and locking behaviour
// -----------------------------------------------------
final DSLContext create1 = create();
final DSLContext create2 = create();
((DefaultConnectionProvider) create2.configuration().connectionProvider()).setConnection(getNewConnection());
create2.configuration().connectionProvider().acquire().setAutoCommit(false);
final Vector<String> execOrder = new Vector<String>();
try {
final Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
sleep(2000);
execOrder.add("t1-block");
try {
create1
.select(TAuthor_ID())
.from(TAuthor())
.forUpdate()
.fetch();
}
// Some databases fail on locking, others lock for a while
catch (DataAccessException ignore) {
}
finally {
execOrder.add("t1-fail-or-t2-commit");
}
}
});
final Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
execOrder.add("t2-exec");
Result<A> result2 = create2
.selectFrom(TAuthor())
.forUpdate()
.fetch();
assertEquals(2, result2.size());
execOrder.add("t2-signal");
sleep(4000);
execOrder.add("t1-fail-or-t2-commit");
try {
create2.configuration().connectionProvider().acquire().commit();
create2.configuration().connectionProvider().acquire().close();
}
catch (Exception e) {}
}
});
// This is the test case:
// 0.0s: Both threads start
// 0.0s: t1 sleeps for 2s
// 0.0s: t2 locks the T_AUTHOR table
// 0.1s: t2 sleeps for 4s
// 2.0s: t1 blocks on the T_AUTHOR table
// ???s: t1 fails
// 4.0s: t2 commits and unlocks T_AUTHOR
t1.start();
t2.start();
t1.join();
t2.join();
assertEquals(asList("t2-exec", "t2-signal", "t1-block", "t1-fail-or-t2-commit", "t1-fail-or-t2-commit"), execOrder);
}
finally {
try {
create2.configuration().connectionProvider().acquire().close();
}
catch (Exception e) {}
}
// Check again with limit / offset clauses
// ---------------------------------------
switch (dialect().family()) {
/* [pro] xx
xxxx xxxxxxx
xxxx xxxxxxx
xxxxxxxxxxxxxxxxxxxx xxxxxx xx xxxxxx xx xxx xxxxxxxxx
xxxxxx
xx [/pro] */
default: {
Result<Record1<Integer>> result3 = create()
.select(TAuthor_ID())
.from(TAuthor())
.limit(5)
.offset(0)
.forUpdate()
.fetch();
assertEquals(2, result3.size());
Result<A> result4 = create().selectFrom(TAuthor())
.limit(5)
.offset(0)
.forUpdate()
.fetch();
assertEquals(2, result4.size());
}
}
switch (dialect().family()) {
/* [pro] xx
xxxx xxxx
xxxx xxxx
xxxx xxxxxxx
xxxx xxxxxxx
xx [/pro] */
case DERBY:
case FIREBIRD:
case HSQLDB:
case H2:
case MARIADB:
case MYSQL:
log.info("SKIPPING", "FOR UPDATE .. WAIT/NOWAIT tests");
break;
/* [pro] xx
xxxx xxxxxxx
xx [/pro] */
case POSTGRES:
default: {
Result<Record1<Integer>> r1a = create()
.select(TAuthor_ID())
.from(TAuthor())
.forUpdate()
.noWait()
.fetch();
assertEquals(2, r1a.size());
/* [pro] xx
xx xxxxxxxxxxxxxxxxxxx xx xxxxxxx x
xxxxxxxxxxxxxxxxxxxxxxxx xxx x xxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxx
xxxxxxxxx
xxxxxxxxxxxxxxx xxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx xxx x xxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxxx
xxxxxxxxx
xxxxxxxxxxxxxxx xxxxxxxxxxxx
xxxxxxxxx xxx x xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxx
xxxxxxxx
xxxxxxxxx
xxxxxxxxxxxxxxx xxxxxxxxxxxx
xxxxxxxxx xxx x xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxx
xxxxxxxxx
xxxxxxxxxxxxxxx xxxxxxxxxxxx
xxxxxxxxx xxx x xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxx
xxxxxxxxx
xxxxxxxxxxxxxxx xxxxxxxxxxxx
x
xx [/pro] */
}
}
switch (dialect().family()) {
case MARIADB:
case MYSQL:
case POSTGRES:
log.info("SKIPPING", "FOR UPDATE OF tests");
break;
// Most dialects support the OF clause
default: {
Result<Record1<Integer>> result =
create().select(TAuthor_ID())
.from(TAuthor())
.forUpdate()
// DB2 requires a key column to be contained in the
// FOR UPDATE OF ... clause
.of(TAuthor_ID(),
TAuthor_LAST_NAME(),
TAuthor_FIRST_NAME())
.fetch();
assertEquals(2, result.size());
Result<A> result2 =
create().selectFrom(TAuthor())
.forUpdate()
.of(TAuthor_ID(),
TAuthor_LAST_NAME(),
TAuthor_FIRST_NAME())
.fetch();
assertEquals(2, result2.size());
// NO BREAK: Fall through to POSTGRES
}
}
switch (dialect()) {
/* [pro] xx
xxxx xxxx xx xxxx xxxxxx xxxxxxxx xxxxx xxx xxxxxxx xxx
xx [/pro] */
case MARIADB:
case MYSQL:
log.info("SKIPPING", "FOR UPDATE OF tests");
break;
// Postgres only supports the OF clause with tables as parameters
case POSTGRES:
default: {
Result<Record1<Integer>> result = create()
.select(TAuthor_ID())
.from(TAuthor())
.forUpdate()
.of(TAuthor())
.fetch();
assertEquals(2, result.size());
Result<A> result2 = create().selectFrom(TAuthor())
.forUpdate()
.of(TAuthor())
.fetch();
assertEquals(2, result2.size());
break;
}
}
// Only few dialects support the FOR SHARE clause:
switch (dialect()) {
case MARIADB:
case MYSQL:
case POSTGRES: {
Result<Record1<Integer>> result = create()
.select(TAuthor_ID())
.from(TAuthor())
.forShare()
.fetch();
assertEquals(2, result.size());
Result<A> result2 = create().selectFrom(TAuthor())
.forShare()
.fetch();
assertEquals(2, result2.size());
break;
}
default:
log.info("SKIPPING", "FOR SHARE clause tests");
}
}
}