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: AbstractParametrizedQuery.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.queries.QueryParameterType;
012: import java.util.List;
013:
014: abstract class AbstractParametrizedQuery extends AbstractQuery
015: implements Query, Cloneable {
016: private QueryParameters mParameters = null;
017:
018: protected AbstractParametrizedQuery(Datasource datasource) {
019: super (datasource);
020: }
021:
022: public void clear() {
023: super .clear();
024:
025: if (mParameters != null) {
026: mParameters.clear();
027: }
028: }
029:
030: private void addTypedParameters(QueryParameterType type,
031: QueryParameters parameters) {
032: if (null == parameters) {
033: return;
034: }
035:
036: addTypedParameters(type, parameters.getOrderedNames());
037: }
038:
039: private void addTypedParameters(QueryParameterType type,
040: List<String> parameters) {
041: if (null == mParameters) {
042: mParameters = new QueryParameters(this );
043: }
044:
045: mParameters.addTypedParameters(type, parameters);
046: }
047:
048: private void addTypedParameter(QueryParameterType type,
049: String parameter) {
050: if (null == mParameters) {
051: mParameters = new QueryParameters(this );
052: }
053:
054: mParameters.addTypedParameter(type, parameter);
055: }
056:
057: private <T> T getTypedParameters(QueryParameterType type) {
058: if (null == mParameters) {
059: return null;
060: }
061:
062: return (T) mParameters.getTypedParameters(type);
063: }
064:
065: private void clearTypedParameters(QueryParameterType type) {
066: if (null == mParameters) {
067: return;
068: }
069:
070: mParameters.clearTypedParameters(type);
071: if (0 == mParameters.getNumberOfTypes()) {
072: mParameters = null;
073: }
074: }
075:
076: protected void _fieldSubselect(Select query) {
077: if (null == query)
078: throw new IllegalArgumentException("query can't be null.");
079:
080: addTypedParameters(QueryParameterType.FIELD, query
081: .getParameters());
082: }
083:
084: protected void _tableSubselect(Select query) {
085: if (null == query)
086: throw new IllegalArgumentException("query can't be null.");
087:
088: addTypedParameters(QueryParameterType.TABLE, query
089: .getParameters());
090: }
091:
092: protected void _whereSubselect(Select query) {
093: if (null == query)
094: throw new IllegalArgumentException("query can't be null.");
095:
096: addTypedParameters(QueryParameterType.WHERE, query
097: .getParameters());
098: }
099:
100: protected void _unionSubselect(Select query) {
101: if (null == query)
102: throw new IllegalArgumentException("query can't be null.");
103:
104: addTypedParameters(QueryParameterType.UNION, query
105: .getParameters());
106: }
107:
108: public QueryParameters getParameters() {
109: return mParameters;
110: }
111:
112: protected void addFieldParameter(String field) {
113: addTypedParameter(QueryParameterType.FIELD, field);
114: }
115:
116: protected void clearWhereParameters() {
117: clearTypedParameters(QueryParameterType.WHERE);
118: }
119:
120: protected void addWhereParameter(String field) {
121: addTypedParameter(QueryParameterType.WHERE, field);
122: }
123:
124: protected List<String> getWhereParameters() {
125: return getTypedParameters(QueryParameterType.WHERE);
126: }
127:
128: public void addWhereParameters(List<String> parameters) {
129: addTypedParameters(QueryParameterType.WHERE, parameters);
130: }
131:
132: protected void setLimitParameter(String limitParameter) {
133: addTypedParameter(QueryParameterType.LIMIT, limitParameter);
134: }
135:
136: public String getLimitParameter() {
137: return getTypedParameters(QueryParameterType.LIMIT);
138: }
139:
140: protected void setOffsetParameter(String offsetParameter) {
141: addTypedParameter(QueryParameterType.OFFSET, offsetParameter);
142: }
143:
144: public String getOffsetParameter() {
145: return getTypedParameters(QueryParameterType.OFFSET);
146: }
147:
148: protected boolean isLimitBeforeOffset() {
149: return true;
150: }
151:
152: public AbstractParametrizedQuery clone() {
153: AbstractParametrizedQuery new_instance = (AbstractParametrizedQuery) super
154: .clone();
155:
156: if (new_instance != null && mParameters != null) {
157: new_instance.mParameters = mParameters.clone();
158: }
159:
160: return new_instance;
161: }
162: }
|