001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: AbstractWhereQuery.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.database.queries;
009:
010: import com.uwyn.rife.database.Datasource;
011: import com.uwyn.rife.database.exceptions.DbQueryException;
012: import com.uwyn.rife.site.Constrained;
013: import com.uwyn.rife.site.ConstrainedUtils;
014: import com.uwyn.rife.tools.StringUtils;
015: import java.util.ArrayList;
016: import java.util.Map;
017: import java.util.Set;
018:
019: public abstract class AbstractWhereQuery<QueryType extends AbstractWhereQuery>
020: extends AbstractParametrizedQuery implements
021: WhereQuery<QueryType>, Cloneable {
022: protected StringBuilder mWhere = null;
023:
024: AbstractWhereQuery(Datasource datasource) {
025: super (datasource);
026:
027: if (null == datasource)
028: throw new IllegalArgumentException(
029: "datasource can't be null.");
030:
031: clear();
032: }
033:
034: public void clear() {
035: super .clear();
036:
037: mWhere = new StringBuilder();
038: }
039:
040: public String getWhere() {
041: return mWhere.toString();
042: }
043:
044: public QueryType whereSubselect(Select query) {
045: _whereSubselect(query);
046:
047: return (QueryType) this ;
048: }
049:
050: public QueryType where(String where) {
051: if (null == where)
052: throw new IllegalArgumentException("where can't be null.");
053: if (0 == where.length())
054: throw new IllegalArgumentException("where can't be empty.");
055:
056: clearGenerated();
057: clearWhereParameters();
058:
059: mWhere = new StringBuilder(where);
060:
061: return (QueryType) this ;
062: }
063:
064: public WhereGroup<QueryType> startWhere() {
065: return new WhereGroup<QueryType>(getDatasource(), this );
066: }
067:
068: public QueryType whereAnd(String where) {
069: if (null == where)
070: throw new IllegalArgumentException("where can't be null.");
071: if (0 == where.length())
072: throw new IllegalArgumentException("where can't be empty.");
073: if (0 == mWhere.length())
074: throw new IllegalArgumentException(
075: "can't perform whereAnd as initial where operation.");
076:
077: clearGenerated();
078:
079: mWhere.append(" AND ");
080: mWhere.append(where);
081:
082: return (QueryType) this ;
083: }
084:
085: public WhereGroupAnd<QueryType> startWhereAnd() {
086: return new WhereGroupAnd<QueryType>(getDatasource(), this );
087: }
088:
089: public QueryType whereOr(String where) {
090: if (null == where)
091: throw new IllegalArgumentException("where can't be null.");
092: if (0 == where.length())
093: throw new IllegalArgumentException("where can't be empty.");
094: if (0 == mWhere.length())
095: throw new IllegalArgumentException(
096: "can't perform whereOr as initial where operation.");
097:
098: clearGenerated();
099:
100: mWhere.append(" OR ");
101: mWhere.append(where);
102:
103: return (QueryType) this ;
104: }
105:
106: public WhereGroupOr<QueryType> startWhereOr() {
107: return new WhereGroupOr<QueryType>(getDatasource(), this );
108: }
109:
110: public QueryType where(String field, String operator, char value) {
111: return where(field, operator, new Character(value));
112: }
113:
114: public QueryType where(String field, String operator, boolean value) {
115: return where(field, operator, Boolean.valueOf(value));
116: }
117:
118: public QueryType where(String field, String operator, byte value) {
119: return where(field, operator, new Byte(value));
120: }
121:
122: public QueryType where(String field, String operator, double value) {
123: return where(field, operator, new Double(value));
124: }
125:
126: public QueryType where(String field, String operator, float value) {
127: return where(field, operator, new Float(value));
128: }
129:
130: public QueryType where(String field, String operator, int value) {
131: return where(field, operator, new Integer(value));
132: }
133:
134: public QueryType where(String field, String operator, long value) {
135: return where(field, operator, new Long(value));
136: }
137:
138: public QueryType where(String field, String operator, short value) {
139: return where(field, operator, new Short(value));
140: }
141:
142: public QueryType where(String field, String operator, Select query) {
143: if (null == query)
144: throw new IllegalArgumentException("query can't be null.");
145:
146: mWhere.append(field);
147: mWhere.append(" ");
148: mWhere.append(operator);
149: mWhere.append(" ");
150: mWhere.append("(");
151: mWhere.append(query.toString());
152: mWhere.append(")");
153:
154: whereSubselect(query);
155:
156: return (QueryType) this ;
157: }
158:
159: public QueryType where(String field, String operator, Object value) {
160: if (null == field)
161: throw new IllegalArgumentException("field can't be null.");
162: if (0 == field.length())
163: throw new IllegalArgumentException("field can't be empty.");
164: if (null == operator)
165: throw new IllegalArgumentException(
166: "operator can't be null.");
167: if (0 == operator.length())
168: throw new IllegalArgumentException(
169: "operator can't be empty.");
170:
171: clearGenerated();
172: clearWhereParameters();
173:
174: mWhere = new StringBuilder();
175: _where(field, operator, value);
176:
177: return (QueryType) this ;
178: }
179:
180: public QueryType whereAnd(String field, String operator, char value) {
181: return whereAnd(field, operator, new Character(value));
182: }
183:
184: public QueryType whereAnd(String field, String operator,
185: boolean value) {
186: return whereAnd(field, operator, Boolean.valueOf(value));
187: }
188:
189: public QueryType whereAnd(String field, String operator, byte value) {
190: return whereAnd(field, operator, new Byte(value));
191: }
192:
193: public QueryType whereAnd(String field, String operator,
194: double value) {
195: return whereAnd(field, operator, new Double(value));
196: }
197:
198: public QueryType whereAnd(String field, String operator, float value) {
199: return whereAnd(field, operator, new Float(value));
200: }
201:
202: public QueryType whereAnd(String field, String operator, int value) {
203: return whereAnd(field, operator, new Integer(value));
204: }
205:
206: public QueryType whereAnd(String field, String operator, long value) {
207: return whereAnd(field, operator, new Long(value));
208: }
209:
210: public QueryType whereAnd(String field, String operator, short value) {
211: return whereAnd(field, operator, new Short(value));
212: }
213:
214: public QueryType whereAnd(String field, String operator,
215: Select query) {
216: if (null == query)
217: throw new IllegalArgumentException("query can't be null.");
218:
219: mWhere.append(" AND ");
220: mWhere.append(field);
221: mWhere.append(" ");
222: mWhere.append(operator);
223: mWhere.append(" ");
224: mWhere.append("(");
225: mWhere.append(query.toString());
226: mWhere.append(")");
227:
228: whereSubselect(query);
229:
230: return (QueryType) this ;
231: }
232:
233: public QueryType whereAnd(String field, String operator,
234: Object value) {
235: if (null == field)
236: throw new IllegalArgumentException("field can't be null.");
237: if (0 == field.length())
238: throw new IllegalArgumentException("field can't be empty.");
239: if (null == operator)
240: throw new IllegalArgumentException(
241: "operator can't be null.");
242: if (0 == operator.length())
243: throw new IllegalArgumentException(
244: "operator can't be empty.");
245:
246: clearGenerated();
247:
248: mWhere.append(" AND ");
249: _where(field, operator, value);
250:
251: return (QueryType) this ;
252: }
253:
254: public QueryType whereOr(String field, String operator, char value) {
255: return whereOr(field, operator, new Character(value));
256: }
257:
258: public QueryType whereOr(String field, String operator,
259: boolean value) {
260: return whereOr(field, operator, Boolean.valueOf(value));
261: }
262:
263: public QueryType whereOr(String field, String operator, byte value) {
264: return whereOr(field, operator, new Byte(value));
265: }
266:
267: public QueryType whereOr(String field, String operator, double value) {
268: return whereOr(field, operator, new Double(value));
269: }
270:
271: public QueryType whereOr(String field, String operator, float value) {
272: return whereOr(field, operator, new Float(value));
273: }
274:
275: public QueryType whereOr(String field, String operator, int value) {
276: return whereOr(field, operator, new Integer(value));
277: }
278:
279: public QueryType whereOr(String field, String operator, long value) {
280: return whereOr(field, operator, new Long(value));
281: }
282:
283: public QueryType whereOr(String field, String operator, short value) {
284: return whereOr(field, operator, new Short(value));
285: }
286:
287: public QueryType whereOr(String field, String operator, Select query) {
288: if (null == query)
289: throw new IllegalArgumentException("query can't be null.");
290:
291: mWhere.append(" OR ");
292: mWhere.append(field);
293: mWhere.append(" ");
294: mWhere.append(operator);
295: mWhere.append(" ");
296: mWhere.append("(");
297: mWhere.append(query.toString());
298: mWhere.append(")");
299:
300: whereSubselect(query);
301:
302: return (QueryType) this ;
303: }
304:
305: public QueryType whereOr(String field, String operator, Object value) {
306: if (null == field)
307: throw new IllegalArgumentException("field can't be null.");
308: if (0 == field.length())
309: throw new IllegalArgumentException("field can't be empty.");
310: if (null == operator)
311: throw new IllegalArgumentException(
312: "operator can't be null.");
313: if (0 == operator.length())
314: throw new IllegalArgumentException(
315: "operator can't be empty.");
316:
317: clearGenerated();
318:
319: mWhere.append(" OR ");
320: _where(field, operator, value);
321:
322: return (QueryType) this ;
323: }
324:
325: private void _where(String field, String operator, Object value) {
326: mWhere.append(field);
327: mWhere.append(" ");
328: mWhere.append(operator);
329: mWhere.append(" ");
330: mWhere
331: .append(mDatasource.getSqlConversion().getSqlValue(
332: value));
333: }
334:
335: public QueryType whereParameter(String field, String operator) {
336: return whereParameter(field, field, operator);
337: }
338:
339: public QueryType whereParameter(String field, String alias,
340: String operator) {
341: if (null == field)
342: throw new IllegalArgumentException("field can't be null.");
343: if (0 == field.length())
344: throw new IllegalArgumentException("field can't be empty.");
345: if (null == alias)
346: throw new IllegalArgumentException("alias can't be null.");
347: if (0 == alias.length())
348: throw new IllegalArgumentException("alias can't be empty.");
349: if (null == operator)
350: throw new IllegalArgumentException(
351: "operator can't be null.");
352: if (0 == operator.length())
353: throw new IllegalArgumentException(
354: "operator can't be empty.");
355:
356: clearGenerated();
357: clearWhereParameters();
358:
359: mWhere = new StringBuilder(field);
360: mWhere.append(" ");
361: mWhere.append(operator);
362: mWhere.append(" ?");
363:
364: addWhereParameter(alias);
365:
366: return (QueryType) this ;
367: }
368:
369: public QueryType whereParameterAnd(String field, String operator) {
370: return whereParameterAnd(field, field, operator);
371: }
372:
373: public QueryType whereParameterAnd(String field, String alias,
374: String operator) {
375: if (null == field)
376: throw new IllegalArgumentException("field can't be null.");
377: if (0 == field.length())
378: throw new IllegalArgumentException("field can't be empty.");
379: if (null == alias)
380: throw new IllegalArgumentException("alias can't be null.");
381: if (0 == alias.length())
382: throw new IllegalArgumentException("alias can't be empty.");
383: if (null == operator)
384: throw new IllegalArgumentException(
385: "operator can't be null.");
386: if (0 == operator.length())
387: throw new IllegalArgumentException(
388: "operator can't be empty.");
389: if (0 == mWhere.length())
390: throw new IllegalArgumentException(
391: "can't perform whereParameterAnd as initial where operation.");
392:
393: clearGenerated();
394:
395: mWhere.append(" AND ");
396: mWhere.append(field);
397: mWhere.append(" ");
398: mWhere.append(operator);
399: mWhere.append(" ?");
400:
401: addWhereParameter(alias);
402:
403: return (QueryType) this ;
404: }
405:
406: public QueryType whereParameterOr(String field, String operator) {
407: return whereParameterOr(field, field, operator);
408: }
409:
410: public QueryType whereParameterOr(String field, String alias,
411: String operator) {
412: if (null == field)
413: throw new IllegalArgumentException("field can't be null.");
414: if (0 == field.length())
415: throw new IllegalArgumentException("field can't be empty.");
416: if (null == alias)
417: throw new IllegalArgumentException("alias can't be null.");
418: if (0 == alias.length())
419: throw new IllegalArgumentException("alias can't be empty.");
420: if (null == operator)
421: throw new IllegalArgumentException(
422: "operator can't be null.");
423: if (0 == operator.length())
424: throw new IllegalArgumentException(
425: "operator can't be empty.");
426: if (0 == mWhere.length())
427: throw new IllegalArgumentException(
428: "can't perform whereParameterOr as initial where operation.");
429:
430: clearGenerated();
431:
432: mWhere.append(" OR ");
433: mWhere.append(field);
434: mWhere.append(" ");
435: mWhere.append(operator);
436: mWhere.append(" ?");
437:
438: addWhereParameter(alias);
439:
440: return (QueryType) this ;
441: }
442:
443: public QueryType where(Object bean) throws DbQueryException {
444: return whereFiltered(bean, null, null);
445: }
446:
447: public QueryType whereIncluded(Object bean, String[] includedFields)
448: throws DbQueryException {
449: return whereFiltered(bean, includedFields, null);
450: }
451:
452: public QueryType whereExcluded(Object bean, String[] excludedFields)
453: throws DbQueryException {
454: return whereFiltered(bean, null, excludedFields);
455: }
456:
457: public QueryType whereFiltered(Object bean,
458: String[] includedFields, String[] excludedFields)
459: throws DbQueryException {
460: if (null == bean)
461: throw new IllegalArgumentException("bean can't be null.");
462:
463: Constrained constrained = ConstrainedUtils
464: .makeConstrainedInstance(bean);
465:
466: ArrayList<String> where_parts = new ArrayList<String>();
467: Map<String, String> property_values = QueryHelper
468: .getBeanPropertyValues(bean, includedFields,
469: excludedFields, getDatasource());
470:
471: for (String property_name : property_values.keySet()) {
472: if (!ConstrainedUtils.persistConstrainedProperty(
473: constrained, property_name, null)) {
474: continue;
475: }
476:
477: where_parts.add(property_name + " = "
478: + property_values.get(property_name));
479: }
480:
481: where(StringUtils.join(where_parts, " AND "));
482:
483: return (QueryType) this ;
484: }
485:
486: public QueryType whereParameters(Class beanClass)
487: throws DbQueryException {
488: return whereParametersExcluded(beanClass, null);
489: }
490:
491: public QueryType whereParametersExcluded(Class beanClass,
492: String[] excludedFields) throws DbQueryException {
493: if (null == beanClass)
494: throw new IllegalArgumentException(
495: "beanClass can't be null.");
496:
497: clearGenerated();
498:
499: Constrained constrained = ConstrainedUtils
500: .getConstrainedInstance(beanClass);
501:
502: Set<String> property_names = QueryHelper.getBeanPropertyNames(
503: beanClass, excludedFields);
504:
505: for (String property_name : property_names) {
506: if (!ConstrainedUtils.persistConstrainedProperty(
507: constrained, property_name, null)) {
508: continue;
509: }
510:
511: if (null == getWhereParameters()) {
512: whereParameter(property_name, "=");
513: } else {
514: whereParameterAnd(property_name, "=");
515: }
516: }
517:
518: return (QueryType) this ;
519: }
520:
521: public QueryType clone() {
522: AbstractWhereQuery new_instance = (AbstractWhereQuery) super
523: .clone();
524: if (new_instance != null) {
525: if (mWhere != null) {
526: new_instance.mWhere = new StringBuilder(mWhere
527: .toString());
528: }
529: }
530:
531: return (QueryType) new_instance;
532: }
533: }
|