1 package com.github.davidmoten.rx.jdbc;
2
3 import static com.github.davidmoten.rx.jdbc.Conditions.checkNotNull;
4 import static com.github.davidmoten.rx.jdbc.Queries.bufferedParameters;
5
6 import java.sql.ResultSet;
7 import java.util.List;
8
9 import rx.Observable;
10 import rx.Observable.Operator;
11 import rx.functions.Func1;
12
13 import com.github.davidmoten.rx.jdbc.NamedParameters.JdbcQuery;
14 import com.github.davidmoten.rx.jdbc.tuple.Tuple2;
15 import com.github.davidmoten.rx.jdbc.tuple.Tuple3;
16 import com.github.davidmoten.rx.jdbc.tuple.Tuple4;
17 import com.github.davidmoten.rx.jdbc.tuple.Tuple5;
18 import com.github.davidmoten.rx.jdbc.tuple.Tuple6;
19 import com.github.davidmoten.rx.jdbc.tuple.Tuple7;
20 import com.github.davidmoten.rx.jdbc.tuple.TupleN;
21 import com.github.davidmoten.rx.jdbc.tuple.Tuples;
22
23
24
25
26 final public class QuerySelect implements Query {
27
28
29 static final String RETURN_GENERATED_KEYS = "RETURN_GENERATED_KEYS?";
30
31 private final Observable<Parameter> parameters;
32 private final QueryContext context;
33 private Observable<?> depends = Observable.empty();
34 private final JdbcQuery jdbcQuery;
35
36
37
38
39
40
41
42
43
44
45
46
47 QuerySelect(String sql, Observable<Parameter> parameters, Observable<?> depends,
48 QueryContext context) {
49 checkNotNull(sql);
50 checkNotNull(parameters);
51 checkNotNull(depends);
52 checkNotNull(context);
53 this.jdbcQuery = NamedParameters.parse(sql);
54 this.parameters = parameters;
55 this.depends = depends;
56 this.context = context;
57 }
58
59 @Override
60 public String sql() {
61 return jdbcQuery.sql();
62 }
63
64 @Override
65 public QueryContext context() {
66 return context;
67 }
68
69 @Override
70 public Observable<Parameter> parameters() {
71 return parameters;
72 }
73
74
75 @Override
76 public List<String> names() {
77 return jdbcQuery.names();
78 }
79
80 @Override
81 public String toString() {
82 return "QuerySelect [sql=" + sql() + "]";
83 }
84
85 @Override
86 public Observable<?> depends() {
87 return depends;
88 }
89
90
91
92
93
94
95
96 public <T> Observable<T> execute(ResultSetMapper<? extends T> function) {
97 return bufferedParameters(this)
98
99 .concatMap(executeOnce(function));
100 }
101
102
103
104
105
106
107
108
109 private <T> Func1<List<Parameter>, Observable<T>> executeOnce(
110 final ResultSetMapper<? extends T> function) {
111 return new Func1<List<Parameter>, Observable<T>>() {
112 @Override
113 public Observable<T> call(List<Parameter> params) {
114 return executeOnce(params, function);
115 }
116 };
117 }
118
119
120
121
122
123
124
125
126
127 private <T> Observable<T> executeOnce(final List<Parameter> params,
128 ResultSetMapper<? extends T> function) {
129 return QuerySelectOnSubscribe.execute(this, params, function).subscribeOn(
130 context.scheduler());
131 }
132
133
134
135
136 public static final class Builder {
137
138
139
140
141 private final QueryBuilder builder;
142
143
144
145
146
147
148
149 public Builder(String sql, Database db) {
150 builder = new QueryBuilder(sql, db);
151 }
152
153
154
155
156
157
158
159
160
161 public <T> Builder parameters(Observable<T> parameters) {
162 builder.parameters(parameters);
163 return this;
164 }
165
166
167
168
169
170
171
172
173
174 public Builder parameters(Object... objects) {
175 builder.parameters(objects);
176 return this;
177 }
178
179
180
181
182
183
184
185
186
187 public Builder parameter(Object value) {
188 builder.parameter(value);
189 return this;
190 }
191
192
193
194
195
196
197
198
199
200
201
202 public Builder parameter(String name, Object value) {
203 builder.parameter(name, value);
204 return this;
205 }
206
207
208
209
210
211
212
213
214 public Builder dependsOn(Observable<?> dependency) {
215 builder.dependsOn(dependency);
216 return this;
217 }
218
219
220
221
222
223
224
225
226
227 public Builder dependsOnLastTransaction() {
228 builder.dependsOnLastTransaction();
229 return this;
230 }
231
232
233
234
235
236
237
238 public <T> Observable<T> get(ResultSetMapper<? extends T> function) {
239 return get(function, builder);
240 }
241
242 static <T> Observable<T> get(ResultSetMapper<? extends T> function, QueryBuilder builder) {
243 return new QuerySelect(builder.sql(), builder.parameters(), builder.depends(),
244 builder.context()).execute(function);
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 public <T> Observable<T> autoMap(Class<T> cls) {
278 return autoMap(cls, builder);
279 }
280
281 static <T> Observable<T> autoMap(Class<T> cls, QueryBuilder builder) {
282 Util.setSqlFromQueryAnnotation(cls, builder);
283 return get(Util.autoMap(cls), builder);
284 }
285
286
287
288
289
290
291
292
293 public <S> Observable<S> getAs(Class<S> cls) {
294 return get(Tuples.single(cls));
295 }
296
297
298
299
300
301
302
303
304 public <S> Observable<TupleN<S>> getTupleN(Class<S> cls) {
305 return get(Tuples.tupleN(cls));
306 }
307
308
309
310
311
312
313
314
315 public <S> Observable<TupleN<Object>> getTupleN() {
316 return get(Tuples.tupleN(Object.class));
317 }
318
319
320
321
322
323
324
325
326
327 public <T1, T2> Observable<Tuple2<T1, T2>> getAs(Class<T1> cls1, Class<T2> cls2) {
328 return get(Tuples.tuple(cls1, cls2));
329 }
330
331
332
333
334
335
336
337
338
339
340 public <T1, T2, T3> Observable<Tuple3<T1, T2, T3>> getAs(Class<T1> cls1, Class<T2> cls2,
341 Class<T3> cls3) {
342 return get(Tuples.tuple(cls1, cls2, cls3));
343 }
344
345
346
347
348
349
350
351
352
353
354
355 public <T1, T2, T3, T4> Observable<Tuple4<T1, T2, T3, T4>> getAs(Class<T1> cls1,
356 Class<T2> cls2, Class<T3> cls3, Class<T4> cls4) {
357 return get(Tuples.tuple(cls1, cls2, cls3, cls4));
358 }
359
360
361
362
363
364
365
366
367
368
369
370
371 public <T1, T2, T3, T4, T5> Observable<Tuple5<T1, T2, T3, T4, T5>> getAs(Class<T1> cls1,
372 Class<T2> cls2, Class<T3> cls3, Class<T4> cls4, Class<T5> cls5) {
373 return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5));
374 }
375
376
377
378
379
380
381
382
383
384
385
386
387
388 public <T1, T2, T3, T4, T5, T6> Observable<Tuple6<T1, T2, T3, T4, T5, T6>> getAs(
389 Class<T1> cls1, Class<T2> cls2, Class<T3> cls3, Class<T4> cls4, Class<T5> cls5,
390 Class<T6> cls6) {
391 return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5, cls6));
392 }
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407 public <T1, T2, T3, T4, T5, T6, T7> Observable<Tuple7<T1, T2, T3, T4, T5, T6, T7>> getAs(
408 Class<T1> cls1, Class<T2> cls2, Class<T3> cls3, Class<T4> cls4, Class<T5> cls5,
409 Class<T6> cls6, Class<T7> cls7) {
410 return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5, cls6, cls7));
411 }
412
413 public Observable<Integer> count() {
414 return get(Util.toOne()).count();
415 }
416
417
418
419
420
421
422
423 public OperatorBuilder<Object> parameterOperator() {
424 return new OperatorBuilder<Object>(this, OperatorType.PARAMETER);
425 }
426
427
428
429
430
431
432
433 public OperatorBuilder<Object> dependsOnOperator() {
434 return new OperatorBuilder<Object>(this, OperatorType.DEPENDENCY);
435 }
436
437
438
439
440
441
442
443 public OperatorBuilder<Observable<Object>> parameterListOperator() {
444 return new OperatorBuilder<Observable<Object>>(this, OperatorType.PARAMETER_LIST);
445 }
446
447 }
448
449
450
451
452 public static class OperatorBuilder<R> {
453
454 private final Builder builder;
455 private final OperatorType operatorType;
456
457
458
459
460
461
462
463 public OperatorBuilder(Builder builder, OperatorType operatorType) {
464 this.builder = builder;
465 this.operatorType = operatorType;
466 }
467
468
469
470
471
472
473
474 public <T> Operator<T, R> get(ResultSetMapper<? extends T> function) {
475 return new QuerySelectOperator<T, R>(builder, function, operatorType);
476 }
477
478
479
480
481
482
483
484 public <S> Operator<S, R> autoMap(Class<S> cls) {
485 return get(Util.autoMap(cls));
486 }
487
488
489
490
491
492
493
494
495 public <S> Operator<S, R> getAs(Class<S> cls) {
496 return get(Tuples.single(cls));
497 }
498
499
500
501
502
503
504
505
506 public <S> Operator<TupleN<S>, R> getTupleN(Class<S> cls) {
507 return get(Tuples.tupleN(cls));
508 }
509
510
511
512
513
514
515
516
517 public <S> Operator<TupleN<Object>, R> getTupleN() {
518 return get(Tuples.tupleN(Object.class));
519 }
520
521
522
523
524
525
526
527
528
529 public <T1, T2> Operator<Tuple2<T1, T2>, R> getAs(Class<T1> cls1, Class<T2> cls2) {
530 return get(Tuples.tuple(cls1, cls2));
531 }
532
533
534
535
536
537
538
539
540
541
542 public <T1, T2, T3> Operator<Tuple3<T1, T2, T3>, R> getAs(Class<T1> cls1, Class<T2> cls2,
543 Class<T3> cls3) {
544 return get(Tuples.tuple(cls1, cls2, cls3));
545 }
546
547
548
549
550
551
552
553
554
555
556
557 public <T1, T2, T3, T4> Operator<Tuple4<T1, T2, T3, T4>, R> getAs(Class<T1> cls1,
558 Class<T2> cls2, Class<T3> cls3, Class<T4> cls4) {
559 return get(Tuples.tuple(cls1, cls2, cls3, cls4));
560 }
561
562
563
564
565
566
567
568
569
570
571
572
573 public <T1, T2, T3, T4, T5> Operator<Tuple5<T1, T2, T3, T4, T5>, R> getAs(Class<T1> cls1,
574 Class<T2> cls2, Class<T3> cls3, Class<T4> cls4, Class<T5> cls5) {
575 return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5));
576 }
577
578
579
580
581
582
583
584
585
586
587
588
589
590 public <T1, T2, T3, T4, T5, T6> Operator<Tuple6<T1, T2, T3, T4, T5, T6>, R> getAs(
591 Class<T1> cls1, Class<T2> cls2, Class<T3> cls3, Class<T4> cls4, Class<T5> cls5,
592 Class<T6> cls6) {
593 return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5, cls6));
594 }
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609 public <T1, T2, T3, T4, T5, T6, T7> Operator<Tuple7<T1, T2, T3, T4, T5, T6, T7>, R> getAs(
610 Class<T1> cls1, Class<T2> cls2, Class<T3> cls3, Class<T4> cls4, Class<T5> cls5,
611 Class<T6> cls6, Class<T7> cls7) {
612 return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5, cls6, cls7));
613 }
614
615 }
616
617 }