1 | package org.sqlorm.querybuilder; |
2 | |
3 | import org.sqlorm.metadatadumper.IColumnName; |
4 | import org.sqlorm.metadatadumper.ITableName; |
5 | |
6 | /** |
7 | * Represents at the same time an {@link AndOrList} and a {@link SelectBuilder} |
8 | * |
9 | * @author Kasper B. Graversen, (c) 2007-2008 |
10 | * @since 1.0.02 |
11 | */ |
12 | public class SurogateAndOrListSelectBuilder implements ItfAndOrList, ItfSelectBuilder { |
13 | ItfSelectBuilder builder = null; |
14 | AndOrList list = null; |
15 | |
16 | /** |
17 | * One of the two must be set. Denote what the sourogate is a sourogate for. |
18 | * |
19 | * @param builder |
20 | * non-null if surogate for a selectBuilder |
21 | * @param list |
22 | * non-null if surogate for an AndOrList |
23 | */ |
24 | public SurogateAndOrListSelectBuilder(ItfSelectBuilder builder, AndOrList list) { |
25 | this.builder = builder; |
26 | this.list = list; |
27 | if(builder == null && list == null) { |
28 | throw new IllegalArgumentException("At least one argument must be non-null"); |
29 | } |
30 | } |
31 | |
32 | /** |
33 | * @param andOr |
34 | * @return |
35 | * @see org.sqlorm.querybuilder.IAndOrList#and(org.sqlorm.querybuilder.IPositionAndOr) |
36 | */ |
37 | public ItfAndOrList and(IPositionAndOr andOr) { |
38 | return list.and(andOr); |
39 | } |
40 | |
41 | /** |
42 | * @param sqlString |
43 | * @return |
44 | * @see org.sqlorm.querybuilder.IAndOrList#and(java.lang.String) |
45 | */ |
46 | public SurogateAndOrListSelectBuilder and(String sqlString) { |
47 | list.and(sqlString); |
48 | return this; |
49 | } |
50 | |
51 | /** |
52 | * @param fmtString |
53 | * @param args |
54 | * @return |
55 | * @see org.sqlorm.querybuilder.IAndOrList#and(java.lang.String, java.lang.Object[]) |
56 | */ |
57 | public SurogateAndOrListSelectBuilder and(String fmtString, Object... args) { |
58 | list.and(fmtString, args); |
59 | return this; |
60 | } |
61 | |
62 | public MultiJoinOnExpr crossJoin(ITableName table) { |
63 | return crossJoin(table.toString()); |
64 | } |
65 | |
66 | public MultiJoinOnExpr crossJoin(ITableName table, String alias) { |
67 | return crossJoin(table.toString(), alias); |
68 | } |
69 | |
70 | /** |
71 | * @param table |
72 | * @return |
73 | * @see org.sqlorm.querybuilder.SelectBuilder#crossJoin(java.lang.String) |
74 | */ |
75 | public MultiJoinOnExpr crossJoin(String table) { |
76 | return builder.crossJoin(table); |
77 | } |
78 | |
79 | /** |
80 | * @param table |
81 | * @param alias |
82 | * @return |
83 | * @see org.sqlorm.querybuilder.SelectBuilder#crossJoin(java.lang.String, java.lang.String) |
84 | */ |
85 | public MultiJoinOnExpr crossJoin(String table, String alias) { |
86 | return builder.crossJoin(table, alias); |
87 | } |
88 | |
89 | /** |
90 | * @return |
91 | * @see org.sqlorm.querybuilder.SelectBuilder#distinct() |
92 | */ |
93 | public ItfSelectBuilder distinct() { |
94 | return builder.distinct(); |
95 | } |
96 | |
97 | public ItfSelectBuilder from(ITableName from) { |
98 | return from(from.toString()); |
99 | } |
100 | |
101 | public ItfSelectBuilder from(ITableName from, String alias) { |
102 | return from(from.toString(), alias); |
103 | } |
104 | |
105 | /** |
106 | * @param subSelect |
107 | * @param alias |
108 | * @return |
109 | * @see org.sqlorm.querybuilder.SelectBuilder#from(org.sqlorm.querybuilder.SelectBuilder, java.lang.String) |
110 | */ |
111 | public ItfSelectBuilder from(ItfSelectBuilder subSelect, String alias) { |
112 | return builder.from(subSelect, alias); |
113 | } |
114 | |
115 | /** |
116 | * @param from |
117 | * @return |
118 | * @see org.sqlorm.querybuilder.SelectBuilder#from(java.lang.String) |
119 | */ |
120 | public ItfSelectBuilder from(String from) { |
121 | return builder.from(from); |
122 | } |
123 | |
124 | /** |
125 | * @param from |
126 | * @param alias |
127 | * @return |
128 | * @see org.sqlorm.querybuilder.SelectBuilder#from(java.lang.String, java.lang.String) |
129 | */ |
130 | public ItfSelectBuilder from(String from, String alias) { |
131 | return builder.from(from, alias); |
132 | } |
133 | |
134 | public ItfSelectBuilder groupBy(ITableName table) { |
135 | return groupBy(table.toString()); |
136 | } |
137 | |
138 | /** |
139 | * @param tableNames |
140 | * @return |
141 | * @see org.sqlorm.querybuilder.SelectBuilder#groupBy(java.lang.String[]) |
142 | */ |
143 | public ItfSelectBuilder groupBy(String... tableNames) { |
144 | return builder.groupBy(tableNames); |
145 | } |
146 | |
147 | /** |
148 | * @return |
149 | * @see org.sqlorm.querybuilder.SelectBuilder#having() |
150 | */ |
151 | public SurogateAndOrListSelectBuilder having() { |
152 | return builder.having(); |
153 | } |
154 | |
155 | public MultiJoinOnExpr innerJoin(ITableName table) { |
156 | return innerJoin(table.toString()); |
157 | } |
158 | |
159 | public MultiJoinOnExpr innerJoin(ITableName table, String alias) { |
160 | return innerJoin(table.toString(), alias); |
161 | } |
162 | |
163 | /** |
164 | * @param table |
165 | * @return |
166 | * @see org.sqlorm.querybuilder.SelectBuilder#innerJoin(java.lang.String) |
167 | */ |
168 | public MultiJoinOnExpr innerJoin(String table) { |
169 | return builder.innerJoin(table); |
170 | } |
171 | |
172 | /** |
173 | * @param table |
174 | * @param alias |
175 | * @return |
176 | * @see org.sqlorm.querybuilder.SelectBuilder#innerJoin(java.lang.String, java.lang.String) |
177 | */ |
178 | public MultiJoinOnExpr innerJoin(String table, String alias) { |
179 | return builder.innerJoin(table, alias); |
180 | } |
181 | |
182 | /** |
183 | * @param alias |
184 | * @return |
185 | * @see org.sqlorm.querybuilder.SelectBuilder#internal_setAlias(java.lang.String) |
186 | */ |
187 | public ItfSelectBuilder internal_setAlias(String alias) { |
188 | return builder.internal_setAlias(alias); |
189 | } |
190 | |
191 | /** |
192 | * @return |
193 | * @see org.sqlorm.querybuilder.SelectBuilder#internal_setIsNestedSelect() |
194 | */ |
195 | public ItfSelectBuilder internal_setIsNestedSelect() { |
196 | return builder.internal_setIsNestedSelect(); |
197 | } |
198 | |
199 | /** |
200 | * @see org.sqlorm.querybuilder.IAndOrList#internal_setIsOutermostList() |
201 | */ |
202 | public void internal_setIsOutermostList() { |
203 | list.internal_setIsOutermostList(); |
204 | } |
205 | |
206 | /** |
207 | * internal method! don't use it. |
208 | * |
209 | * @return |
210 | * @see org.sqlorm.querybuilder.IAndOrList#internal_size() |
211 | */ |
212 | public int internal_size() { |
213 | return list.internal_size(); |
214 | } |
215 | |
216 | public MultiJoinOnExpr leftJoin(ITableName table) { |
217 | return leftJoin(table.toString()); |
218 | } |
219 | |
220 | public MultiJoinOnExpr leftJoin(ITableName table, String alias) { |
221 | return leftJoin(table.toString(), alias); |
222 | } |
223 | |
224 | /** |
225 | * @param table |
226 | * @return |
227 | * @see org.sqlorm.querybuilder.SelectBuilder#leftJoin(java.lang.String) |
228 | */ |
229 | public MultiJoinOnExpr leftJoin(String table) { |
230 | return builder.leftJoin(table); |
231 | } |
232 | |
233 | /** |
234 | * @param table |
235 | * @param alias |
236 | * @return |
237 | * @see org.sqlorm.querybuilder.SelectBuilder#leftJoin(java.lang.String, java.lang.String) |
238 | */ |
239 | public MultiJoinOnExpr leftJoin(String table, String alias) { |
240 | return builder.leftJoin(table, alias); |
241 | } |
242 | |
243 | /** |
244 | * @param andOr |
245 | * @return |
246 | * @see org.sqlorm.querybuilder.IAndOrList#or(org.sqlorm.querybuilder.IPositionAndOr) |
247 | */ |
248 | public SurogateAndOrListSelectBuilder or(IPositionAndOr andOr) { |
249 | list.or(andOr); |
250 | return this; |
251 | } |
252 | |
253 | /** |
254 | * @param sqlString |
255 | * @return |
256 | * @see org.sqlorm.querybuilder.IAndOrList#or(java.lang.String) |
257 | */ |
258 | public SurogateAndOrListSelectBuilder or(String sqlString) { |
259 | list.or(sqlString); |
260 | return this; |
261 | } |
262 | |
263 | /** |
264 | * @param fmtString |
265 | * @param args |
266 | * @return |
267 | * @see org.sqlorm.querybuilder.IAndOrList#or(java.lang.String, java.lang.Object[]) |
268 | */ |
269 | public SurogateAndOrListSelectBuilder or(String fmtString, Object... args) { |
270 | list.or(fmtString, args); |
271 | return this; |
272 | } |
273 | |
274 | public ItfSelectBuilder orderBy(ITableName table) { |
275 | return orderBy(table.toString()); |
276 | } |
277 | |
278 | /** |
279 | * @param tableName |
280 | * @return |
281 | * @see org.sqlorm.querybuilder.SelectBuilder#orderBy(java.lang.String) |
282 | */ |
283 | public ItfSelectBuilder orderBy(String tableName) { |
284 | return builder.orderBy(tableName); |
285 | } |
286 | |
287 | public ItfSelectBuilder orderByAsc(ITableName table) { |
288 | return orderByAsc(table.toString()); |
289 | } |
290 | |
291 | /** |
292 | * @param tableName |
293 | * @return |
294 | * @see org.sqlorm.querybuilder.SelectBuilder#orderByAsc(java.lang.String) |
295 | */ |
296 | public ItfSelectBuilder orderByAsc(String tableName) { |
297 | return builder.orderByAsc(tableName); |
298 | } |
299 | |
300 | public ItfSelectBuilder orderByDesc(ITableName table) { |
301 | return orderByDesc(table.toString()); |
302 | |
303 | } |
304 | |
305 | /** |
306 | * @param tableName |
307 | * @return |
308 | * @see org.sqlorm.querybuilder.SelectBuilder#orderByDesc(java.lang.String) |
309 | */ |
310 | public ItfSelectBuilder orderByDesc(String tableName) { |
311 | return builder.orderByDesc(tableName); |
312 | } |
313 | |
314 | public MultiJoinOnExpr outerJoin(ITableName table) { |
315 | return outerJoin(table.toString()); |
316 | } |
317 | |
318 | public MultiJoinOnExpr outerJoin(ITableName table, String alias) { |
319 | return outerJoin(table.toString(), alias); |
320 | } |
321 | |
322 | /** |
323 | * @param table |
324 | * @return |
325 | * @see org.sqlorm.querybuilder.SelectBuilder#outerJoin(java.lang.String) |
326 | */ |
327 | public MultiJoinOnExpr outerJoin(String table) { |
328 | return builder.outerJoin(table); |
329 | } |
330 | |
331 | /** |
332 | * @param table |
333 | * @param alias |
334 | * @return |
335 | * @see org.sqlorm.querybuilder.SelectBuilder#outerJoin(java.lang.String, java.lang.String) |
336 | */ |
337 | public MultiJoinOnExpr outerJoin(String table, String alias) { |
338 | return builder.outerJoin(table, alias); |
339 | } |
340 | |
341 | public MultiJoinOnExpr rightJoin(ITableName table) { |
342 | return rightJoin(table.toString()); |
343 | } |
344 | |
345 | public MultiJoinOnExpr rightJoin(ITableName table, String alias) { |
346 | return rightJoin(table.toString(), alias); |
347 | } |
348 | |
349 | /** |
350 | * @param table |
351 | * @return |
352 | * @see org.sqlorm.querybuilder.SelectBuilder#rightJoin(java.lang.String) |
353 | */ |
354 | public MultiJoinOnExpr rightJoin(String table) { |
355 | return builder.rightJoin(table); |
356 | } |
357 | |
358 | /** |
359 | * @param table |
360 | * @param alias |
361 | * @return |
362 | * @see org.sqlorm.querybuilder.SelectBuilder#rightJoin(java.lang.String, java.lang.String) |
363 | */ |
364 | public MultiJoinOnExpr rightJoin(String table, String alias) { |
365 | return builder.rightJoin(table, alias); |
366 | } |
367 | |
368 | public ItfSelectBuilder select(IColumnName column) { |
369 | return select(column.toString()); |
370 | } |
371 | |
372 | public ItfSelectBuilder select(IColumnName column, String alias) { |
373 | return select(column.toString(), alias); |
374 | } |
375 | |
376 | /** |
377 | * @param column |
378 | * @return |
379 | * @see org.sqlorm.querybuilder.SelectBuilder#select(java.lang.String) |
380 | */ |
381 | public ItfSelectBuilder select(String column) { |
382 | return builder.select(column); |
383 | } |
384 | |
385 | /** |
386 | * @param column |
387 | * @param alias |
388 | * @return |
389 | * @see org.sqlorm.querybuilder.SelectBuilder#select(java.lang.String, java.lang.String) |
390 | */ |
391 | public ItfSelectBuilder select(String column, String alias) { |
392 | return builder.select(column, alias); |
393 | } |
394 | |
395 | /** |
396 | * @return |
397 | * @see org.sqlorm.querybuilder.SelectBuilder#toSql() |
398 | */ |
399 | public String toSql() { |
400 | return builder.toSql(); |
401 | } |
402 | |
403 | /** |
404 | * @param sb |
405 | * @param indent |
406 | * @see org.sqlorm.querybuilder.IExpr#toSql(java.lang.StringBuilder, java.lang.String) |
407 | */ |
408 | public void toSql(StringBuilder sb, String indent) { |
409 | list.toSql(sb, indent); |
410 | } |
411 | |
412 | /** |
413 | * @return |
414 | * @see org.sqlorm.querybuilder.SelectBuilder#where() |
415 | */ |
416 | public SurogateAndOrListSelectBuilder where() { |
417 | return builder.where(); |
418 | } |
419 | |
420 | } |