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
|
/*
* Copyright 2007-2012 Amazon Technologies, Inc. or its affiliates.
* Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
* of Amazon Technologies, Inc. or its affiliates. All rights reserved.
*
* 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.
*/
package com.amazon.carbonado.repo.jdbc;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.List;
import com.amazon.carbonado.FetchException;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Storable;
import com.amazon.carbonado.filter.AndFilter;
import com.amazon.carbonado.filter.ClosedFilter;
import com.amazon.carbonado.filter.ExistsFilter;
import com.amazon.carbonado.filter.Filter;
import com.amazon.carbonado.filter.OrFilter;
import com.amazon.carbonado.filter.PropertyFilter;
import com.amazon.carbonado.filter.RelOp;
import com.amazon.carbonado.filter.Visitor;
import com.amazon.carbonado.info.ChainedProperty;
import com.amazon.carbonado.info.StorableProperty;
/**
*
*
* @author Brian S O'Neill
*/
class WhereBuilder<S extends Storable> extends Visitor<S, FetchException, Object> {
private final SQLStatementBuilder mStatementBuilder;
private final JoinNode mJoinNode;
private final TableAliasGenerator mAliasGenerator;
private List<PropertyFilter<S>> mPropertyFilters;
private List<Boolean> mPropertyFilterNullable;
/**
* @param statementBuilder destination for WHERE statement
* @param jn pass null if no alias is to be used
* @param aliasGenerator used for supporting "EXISTS" filter
*/
WhereBuilder(SQLStatementBuilder statementBuilder, JoinNode jn,
TableAliasGenerator aliasGenerator)
{
mStatementBuilder = statementBuilder;
mJoinNode = jn;
mAliasGenerator = aliasGenerator;
mPropertyFilters = new ArrayList<PropertyFilter<S>>();
mPropertyFilterNullable = new ArrayList<Boolean>();
}
public void append(Filter<S> filter) throws FetchException {
mStatementBuilder.append(" WHERE ");
FetchException e = filter.accept(this, null);
if (e != null) {
throw e;
}
}
/**
* Generate SQL of the form "WHERE EXISTS (SELECT * FROM ...)" This is
* necessary for DELETE statements which operate against joined tables.
*/
public void appendExists(Filter<S> filter) throws FetchException {
mStatementBuilder.append(" WHERE ");
mStatementBuilder.append("EXISTS (SELECT * FROM");
JDBCStorableInfo<S> info;
final JDBCRepository repo = mStatementBuilder.getRepository();
try {
info = repo.examineStorable(filter.getStorableType());
} catch (RepositoryException e) {
throw repo.toFetchException(e);
}
JoinNode jn;
try {
JoinNodeBuilder jnb =
new JoinNodeBuilder(repo, info, mAliasGenerator);
filter.accept(jnb, null);
jn = jnb.getRootJoinNode();
} catch (UndeclaredThrowableException e) {
throw repo.toFetchException(e);
}
jn.appendFullJoinTo(mStatementBuilder);
mStatementBuilder.append(" WHERE ");
{
int i = 0;
for (JDBCStorableProperty<S> property : info.getPrimaryKeyProperties().values()) {
if (i > 0) {
mStatementBuilder.append(" AND ");
}
mStatementBuilder.append(mJoinNode.getAlias());
mStatementBuilder.append('.');
mStatementBuilder.append(property.getColumnName());
mStatementBuilder.append('=');
mStatementBuilder.append(jn.getAlias());
mStatementBuilder.append('.');
mStatementBuilder.append(property.getColumnName());
i++;
}
}
mStatementBuilder.append(" AND (");
WhereBuilder<S> wb = new WhereBuilder<S>(mStatementBuilder, jn, mAliasGenerator);
FetchException e = filter.accept(wb, null);
if (e != null) {
throw e;
}
// Transfer property filters so that the values are extracted properly.
mPropertyFilters.addAll(wb.mPropertyFilters);
mPropertyFilterNullable.addAll(wb.mPropertyFilterNullable);
mStatementBuilder.append(')');
mStatementBuilder.append(')');
}
@SuppressWarnings("unchecked")
public PropertyFilter<S>[] getPropertyFilters() {
return mPropertyFilters.toArray(new PropertyFilter[mPropertyFilters.size()]);
}
public boolean[] getPropertyFilterNullable() {
boolean[] array = new boolean[mPropertyFilterNullable.size()];
for (int i=0; i<array.length; i++) {
array[i] = mPropertyFilterNullable.get(i);
}
return array;
}
@Override
public FetchException visit(OrFilter<S> filter, Object param) {
FetchException e;
mStatementBuilder.append('(');
e = filter.getLeftFilter().accept(this, null);
if (e != null) {
return e;
}
mStatementBuilder.append(" OR ");
e = filter.getRightFilter().accept(this, null);
if (e != null) {
return e;
}
mStatementBuilder.append(')');
return null;
}
@Override
public FetchException visit(AndFilter<S> filter, Object param) {
FetchException e;
mStatementBuilder.append('(');
e = filter.getLeftFilter().accept(this, null);
if (e != null) {
return e;
}
mStatementBuilder.append(" AND ");
e = filter.getRightFilter().accept(this, null);
if (e != null) {
return e;
}
mStatementBuilder.append(')');
return null;
}
@Override
public FetchException visit(PropertyFilter<S> filter, Object param) {
try {
mStatementBuilder.appendColumn(mJoinNode, filter.getChainedProperty());
} catch (FetchException e) {
return e;
}
if (!filter.isConstant()) {
addBindParameter(filter);
} else {
RelOp op = filter.getOperator();
Object constant = filter.constant();
if (constant == null) {
if (op == RelOp.EQ) {
mStatementBuilder.append("IS NULL");
} else if (op == RelOp.NE) {
mStatementBuilder.append("IS NOT NULL");
} else {
mStatementBuilder.append(sqlOperatorFor(op));
mStatementBuilder.append("NULL");
}
} else if (filter.getType() == String.class) {
mStatementBuilder.append(sqlOperatorFor(op));
mStatementBuilder.append('\'');
mStatementBuilder.append(String.valueOf(constant).replace("'", "''"));
mStatementBuilder.append('\'');
} else if (Number.class.isAssignableFrom(filter.getBoxedType())) {
mStatementBuilder.append(sqlOperatorFor(op));
mStatementBuilder.append(String.valueOf(constant));
} else {
// Don't try to create literal for special type. Instead,
// fallback to bind parameter and let JDBC driver do the work.
addBindParameter(filter);
}
}
return null;
}
@Override
public FetchException visit(ExistsFilter<S> filter, Object param) {
if (filter.isNotExists()) {
mStatementBuilder.append("NOT ");
}
mStatementBuilder.append("EXISTS (SELECT * FROM");
ChainedProperty<S> chained = filter.getChainedProperty();
JDBCStorableInfo<?> oneToManyInfo;
JDBCStorableProperty<?> oneToMany;
final JDBCRepository repo = mStatementBuilder.getRepository();
try {
StorableProperty<?> lastProp = chained.getLastProperty();
oneToManyInfo = repo.examineStorable(lastProp.getJoinedType());
oneToMany = repo.getJDBCStorableProperty(lastProp);
} catch (RepositoryException e) {
return repo.toFetchException(e);
}
Filter<?> subFilter = filter.getSubFilter();
JoinNode oneToManyNode;
try {
JoinNodeBuilder jnb =
new JoinNodeBuilder(repo, oneToManyInfo, mAliasGenerator);
if (subFilter != null) {
subFilter.accept(jnb, null);
}
oneToManyNode = jnb.getRootJoinNode();
} catch (UndeclaredThrowableException e) {
return repo.toFetchException(e);
}
oneToManyNode.appendFullJoinTo(mStatementBuilder);
mStatementBuilder.append(" WHERE ");
{
String alias = mJoinNode.findAliasFor(chained);
int count = oneToMany.getJoinElementCount();
for (int i=0; i<count; i++) {
if (i > 0) {
mStatementBuilder.append(" AND ");
}
mStatementBuilder.append(alias);
mStatementBuilder.append('.');
mStatementBuilder.append(oneToMany.getInternalJoinElement(i).getColumnName());
mStatementBuilder.append('=');
mStatementBuilder.append(oneToManyNode.getAlias());
mStatementBuilder.append('.');
mStatementBuilder.append(oneToMany.getExternalJoinElement(i).getColumnName());
}
}
if (subFilter != null && !subFilter.isOpen()) {
mStatementBuilder.append(" AND (");
WhereBuilder wb = new WhereBuilder
(mStatementBuilder, oneToManyNode, mAliasGenerator);
FetchException e = (FetchException) subFilter.accept(wb, null);
if (e != null) {
return e;
}
mStatementBuilder.append(')');
// Transfer property filters from sub-builder as joined from exists filter.
int size = wb.mPropertyFilters.size();
for (int i=0; i<size; i++) {
PropertyFilter propFilter = (PropertyFilter) wb.mPropertyFilters.get(i);
mPropertyFilters.add(propFilter.asJoinedFromAny(chained));
}
mPropertyFilterNullable.addAll(wb.mPropertyFilterNullable);
}
mStatementBuilder.append(')');
return null;
}
@Override
public FetchException visit(ClosedFilter<S> filter, Object param) {
mStatementBuilder.append("1=0");
return null;
}
private void addBindParameter(PropertyFilter<S> filter) {
RelOp op = filter.getOperator();
StorableProperty<?> property = filter.getChainedProperty().getLastProperty();
mPropertyFilters.add(filter);
if (property.isNullable() && (op == RelOp.EQ || op == RelOp.NE)) {
mPropertyFilterNullable.add(true);
mStatementBuilder.append(new NullablePropertyStatement<S>(filter, op == RelOp.EQ));
} else {
mPropertyFilterNullable.add(false);
mStatementBuilder.append(sqlOperatorFor(op));
mStatementBuilder.append('?');
}
}
private String sqlOperatorFor(RelOp op) {
if (op == RelOp.NE) {
return "<>";
} else {
return op.toString();
}
}
}
|