1 package com.github.davidmoten.rx.jdbc;
2
3 import com.github.davidmoten.util.Preconditions;
4
5 import rx.Observable;
6
7
8
9
10 final class QueryBuilder {
11
12
13
14
15 private String sql;
16
17
18
19
20 private Observable<Parameter> parameters = Observable.empty();
21
22
23
24
25 private Observable<?> depends = Observable.empty();
26
27
28
29
30 private final Database db;
31
32
33
34
35 private final QueryContext context;
36
37
38
39
40
41
42
43 QueryBuilder(String sql, Database db) {
44 this.sql = sql;
45 this.db = db;
46 this.context = db.queryContext();
47 }
48
49
50
51
52
53
54
55
56 <T> void parameters(Observable<T> params) {
57 this.parameters = Observable.concat(parameters, params.map(Parameter.TO_PARAMETER));
58 }
59
60
61
62
63
64
65
66
67 void parameters(Object... objects) {
68 for (Object object : objects)
69 parameter(object);
70 }
71
72
73
74
75
76
77
78
79 void parameter(Object value) {
80
81 if (value instanceof Observable)
82 throw new IllegalArgumentException(
83 "use parameters() method not the parameter() method for an Observable");
84 parameters(Observable.just(value));
85 }
86
87
88
89
90
91
92
93
94
95
96
97 void parameter(String name, Object value) {
98 Preconditions.checkNotNull(name, "parameter name cannot be null");
99 if (value instanceof Observable)
100 throw new IllegalArgumentException(
101 "use parameters() method not the parameter() method for an Observable");
102 this.parameters = parameters.concatWith(Observable.just(new Parameter(name, value)));
103 }
104
105
106
107
108
109
110
111 void dependsOn(Observable<?> dependency) {
112 depends = Observable.concat(depends, dependency);
113 }
114
115
116
117
118
119
120
121 void dependsOnLastTransaction() {
122 dependsOn(db.lastTransactionResult());
123 }
124
125
126
127
128
129
130 String sql() {
131 return sql;
132 }
133
134
135
136
137
138
139 Observable<Parameter> parameters() {
140 return parameters;
141 }
142
143
144
145
146
147
148 public Observable<?> depends() {
149 return depends;
150 }
151
152
153
154
155
156
157 QueryContext context() {
158 return context;
159 }
160
161 void clearParameters() {
162 this.parameters = Observable.empty();
163 }
164
165 void setSql(String sql) {
166 this.sql = sql;
167 }
168 }