1 package org.davidmoten.rx.jdbc;
2
3 import java.sql.Connection;
4 import java.util.List;
5
6 import javax.annotation.Nonnull;
7
8 import com.github.davidmoten.guavamini.Preconditions;
9
10 import io.reactivex.Flowable;
11 import io.reactivex.Single;
12
13 public final class SelectBuilder extends ParametersBuilder<SelectBuilder>
14 implements Getter, DependsOn<SelectBuilder> {
15
16 final String sql;
17 final Single<Connection> connection;
18 private final Database db;
19
20 int fetchSize = 0;
21 int queryTimeoutSec = Util.QUERY_TIMEOUT_NOT_SET;
22 private Flowable<?> dependsOn;
23
24 SelectBuilder(String sql, Single<Connection> connection, Database db) {
25 super(sql);
26 Preconditions.checkNotNull(sql);
27 Preconditions.checkNotNull(connection);
28 this.sql = sql;
29 this.connection = connection;
30 this.db = db;
31 }
32
33
34
35
36
37
38
39
40
41 public SelectBuilder fetchSize(int size) {
42 Preconditions.checkArgument(size >= 0);
43 this.fetchSize = size;
44 return this;
45 }
46
47 public SelectBuilder queryTimeoutSec(int timeoutSec) {
48 Preconditions.checkArgument(timeoutSec >= 0);
49 this.queryTimeoutSec = timeoutSec;
50 return this;
51 }
52
53 public TransactedSelectBuilder transacted() {
54 return new TransactedSelectBuilder(this, db);
55 }
56
57 public TransactedSelectBuilder transactedValuesOnly() {
58 return transacted().transactedValuesOnly();
59 }
60
61 @Override
62 public <T> Flowable<T> get(@Nonnull ResultSetMapper<? extends T> mapper) {
63 Preconditions.checkNotNull(mapper, "mapper cannot be null");
64 Flowable<List<Object>> pg = super.parameterGroupsToFlowable();
65 Flowable<T> f = Select.<T>create(connection, pg, sql, fetchSize, mapper, true, queryTimeoutSec);
66 if (dependsOn != null) {
67 return dependsOn.ignoreElements().andThen(f);
68 } else {
69 return f;
70 }
71 }
72
73 @Override
74 public SelectBuilder dependsOn(@Nonnull Flowable<?> flowable) {
75 Preconditions.checkArgument(dependsOn == null, "can only set dependsOn once");
76 Preconditions.checkNotNull(flowable);
77 dependsOn = flowable;
78 return this;
79 }
80
81 }