001: /*
002: * Copyright 2004-2007 Gary Bentley
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may
005: * not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.josql.expressions;
016:
017: import java.util.Map;
018: import java.util.Collection;
019:
020: import org.josql.Query;
021: import org.josql.QueryExecutionException;
022: import org.josql.QueryParseException;
023:
024: import org.josql.internal.Utilities;
025:
026: public class SelectItemExpression extends Expression {
027:
028: public static final String KEY = "key";
029: public static final String VALUE = "value";
030:
031: private Expression exp = null;
032: private boolean fixedResult = false;
033: private Class addItemsType = null;
034: private String addItemsMapType = null;
035: private String alias = null;
036:
037: public String getAlias() {
038:
039: return this .alias;
040:
041: }
042:
043: public void setAlias(String a) {
044:
045: this .alias = Utilities.stripQuotes(a);
046:
047: }
048:
049: public boolean hasFixedResult(Query q) {
050:
051: return this .fixedResult;
052:
053: }
054:
055: public Class getExpectedReturnType(Query q)
056: throws QueryParseException {
057:
058: return this .exp.getExpectedReturnType(q);
059:
060: }
061:
062: public void init(Query q) throws QueryParseException {
063:
064: this .exp.init(q);
065:
066: this .fixedResult = this .exp.hasFixedResult(q);
067:
068: /*
069: if (this.isAddItemsFromCollectionOrMap ())
070: {
071:
072: // Get the expected return type.
073: Class expC = this.getExpectedReturnType (q);
074:
075: if (expC.getName ().equals (Object.class.getName ()))
076: {
077:
078: // Defer until later.
079: return;
080:
081: }
082:
083: if (!this.addItemsType.isAssignableFrom (expC))
084: {
085:
086: throw new QueryParseException ("Expected return type from select clause column will be: " +
087: expC.getName () +
088: ", however expecting to add items to results from type: " +
089: this.addItemsType.getName ());
090:
091: }
092:
093: }
094: */
095:
096: }
097:
098: public void setAddMapType(String t) {
099:
100: this .addItemsMapType = t;
101:
102: }
103:
104: public Collection getAddItems(Object v) {
105:
106: if (Map.class.isAssignableFrom(this .addItemsType)) {
107:
108: boolean k = this .addItemsMapType
109: .equals(SelectItemExpression.KEY);
110:
111: Map m = (Map) v;
112:
113: if (k) {
114:
115: return m.keySet();
116:
117: }
118:
119: return m.values();
120:
121: }
122:
123: if (v instanceof Collection) {
124:
125: return (Collection) v;
126:
127: }
128:
129: // This is a nasty hack but is "ok" for now, fix in a later version...
130: java.util.List l = new java.util.ArrayList();
131: l.add(v);
132:
133: return l;
134:
135: }
136:
137: public boolean isAddItemsFromCollectionOrMap() {
138:
139: return this .addItemsType != null;
140:
141: }
142:
143: public void setAddItemsType(Class c) {
144:
145: this .addItemsType = c;
146:
147: }
148:
149: public Expression getExpression() {
150:
151: return this .exp;
152:
153: }
154:
155: public void setExpression(Expression exp) {
156:
157: this .exp = exp;
158:
159: }
160:
161: public boolean isTrue(Object o, Query q)
162: throws QueryExecutionException {
163:
164: return this .exp.isTrue(o, q);
165:
166: }
167:
168: public Object getValue(Object o, Query q)
169: throws QueryExecutionException {
170:
171: return this .exp.getValue(o, q);
172:
173: }
174:
175: public String toString() {
176:
177: StringBuffer b = new StringBuffer();
178:
179: if (this .isAddItemsFromCollectionOrMap()) {
180:
181: b.append("[*");
182:
183: if (this .addItemsMapType != null) {
184:
185: b.append(", ");
186: b.append(this .addItemsMapType);
187:
188: }
189:
190: b.append("] ");
191:
192: }
193:
194: b.append(this .exp);
195:
196: if (this .alias != null) {
197:
198: b.append(" ");
199: b.append(this.alias);
200:
201: }
202:
203: return b.toString();
204:
205: }
206:
207: }
|