001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.db.sql;
031:
032: import com.caucho.log.Log;
033: import com.caucho.util.QDate;
034:
035: import java.io.InputStream;
036: import java.sql.SQLException;
037: import java.util.ArrayList;
038: import java.util.logging.Logger;
039:
040: class ParamExpr extends Expr {
041: private static final Logger log = Log.open(ParamExpr.class);
042:
043: private static final int NULL = 0;
044: private static final int BOOLEAN = NULL + 1;
045: private static final int STRING = BOOLEAN + 1;
046: private static final int LONG = STRING + 1;
047: private static final int DOUBLE = LONG + 1;
048: private static final int DATE = DOUBLE + 1;
049: private static final int BINARY = DATE + 1;
050:
051: private int _index;
052:
053: private int _type = NULL;
054:
055: private String _stringValue;
056: private long _longValue;
057: private double _doubleValue;
058:
059: private InputStream _binaryStream;
060: private int _streamLength;
061:
062: ParamExpr(int index) {
063: _index = index;
064: }
065:
066: /**
067: * Returns the type of the expression.
068: */
069: public Class getType() {
070: switch (_type) {
071: case NULL:
072: return Object.class;
073:
074: case BOOLEAN:
075: return boolean.class;
076:
077: case STRING:
078: return String.class;
079:
080: case LONG:
081: return long.class;
082:
083: case DOUBLE:
084: return double.class;
085:
086: case DATE:
087: return java.util.Date.class;
088:
089: case BINARY:
090: return java.io.InputStream.class;
091:
092: default:
093: return Object.class;
094: }
095: }
096:
097: /**
098: * Returns the subcost based on the given FromList.
099: */
100: public long subCost(ArrayList<FromItem> fromList) {
101: return 0;
102: }
103:
104: /**
105: * Clers the value.
106: */
107: public void clear() {
108: _type = NULL;
109: }
110:
111: /**
112: * Sets the value as a string.
113: */
114: public void setString(String value) {
115: if (value == null)
116: _type = NULL;
117: else {
118: _type = STRING;
119: _stringValue = value;
120: }
121: }
122:
123: /**
124: * Sets the value as a boolean.
125: */
126: public void setBoolean(boolean value) {
127: _type = BOOLEAN;
128: _longValue = value ? 1 : 0;
129: }
130:
131: /**
132: * Sets the value as a long.
133: */
134: public void setLong(long value) {
135: _type = LONG;
136: _longValue = value;
137: }
138:
139: /**
140: * Sets the value as a double.
141: */
142: public void setDouble(double value) {
143: _type = DOUBLE;
144: _doubleValue = value;
145: }
146:
147: /**
148: * Sets the value as a date.
149: */
150: public void setDate(long value) {
151: _type = DATE;
152: _longValue = value;
153: }
154:
155: /**
156: * Sets the value as a stream.
157: */
158: public void setBinaryStream(InputStream is, int length) {
159: _type = BINARY;
160: _binaryStream = is;
161: _streamLength = length;
162: }
163:
164: /**
165: * Evaluates the expression as a string.
166: *
167: * @param rows the current database tuple
168: *
169: * @return the string value
170: */
171: public boolean isNull(QueryContext context) throws SQLException {
172: return _type == NULL;
173: }
174:
175: /**
176: * Evaluates the expression as a string.
177: *
178: * @param rows the current database tuple
179: *
180: * @return the string value
181: */
182: public String evalString(QueryContext context) throws SQLException {
183: switch (_type) {
184: case NULL:
185: return null;
186:
187: case BOOLEAN:
188: return _longValue != 0 ? "1" : "0";
189:
190: case STRING:
191: return _stringValue;
192:
193: case LONG:
194: return String.valueOf(_longValue);
195:
196: case DATE:
197: return QDate.formatISO8601(_longValue);
198:
199: case DOUBLE:
200: return String.valueOf(_doubleValue);
201:
202: default:
203: throw new UnsupportedOperationException(String
204: .valueOf(_type));
205: }
206: }
207:
208: /**
209: * Evaluates the expression as a boolean.
210: *
211: * @param rows the current database tuple
212: *
213: * @return the boolean value
214: */
215: public int evalBoolean(QueryContext context) throws SQLException {
216: switch (_type) {
217: case NULL:
218: return UNKNOWN;
219:
220: case BOOLEAN:
221: case LONG:
222: return _longValue != 0 ? TRUE : FALSE;
223:
224: case DOUBLE:
225: return _doubleValue != 0 ? TRUE : FALSE;
226:
227: default:
228: throw new UnsupportedOperationException();
229: }
230: }
231:
232: /**
233: * Evaluates the expression as a long.
234: *
235: * @param rows the current database tuple
236: *
237: * @return the long value
238: */
239: public long evalLong(QueryContext context) throws SQLException {
240: switch (_type) {
241: case NULL:
242: return 0;
243:
244: case BOOLEAN:
245: case LONG:
246: case DATE:
247: return _longValue;
248:
249: case DOUBLE:
250: return (long) _doubleValue;
251:
252: case STRING:
253: return Long.parseLong(_stringValue);
254:
255: default:
256: throw new UnsupportedOperationException("" + _type);
257: }
258: }
259:
260: /**
261: * Evaluates the expression as a double.
262: *
263: * @param rows the current database tuple
264: *
265: * @return the double value
266: */
267: public double evalDouble(QueryContext context) throws SQLException {
268: switch (_type) {
269: case NULL:
270: return 0;
271:
272: case LONG:
273: case DATE:
274: return _longValue;
275:
276: case DOUBLE:
277: return _doubleValue;
278:
279: case STRING:
280: return Double.parseDouble(_stringValue);
281:
282: default:
283: throw new UnsupportedOperationException();
284: }
285: }
286:
287: /**
288: * Evaluates the expression as a date
289: *
290: * @param rows the current database tuple
291: *
292: * @return the date value
293: */
294: public long evalDate(QueryContext context) throws SQLException {
295: switch (_type) {
296: case NULL:
297: return 0;
298:
299: case LONG:
300: case DATE:
301: return _longValue;
302:
303: case DOUBLE:
304: return (long) _doubleValue;
305:
306: default:
307: throw new UnsupportedOperationException();
308: }
309: }
310:
311: /**
312: * Evaluates the expression as a stream.
313: *
314: * @param rows the current database tuple
315: *
316: * @return the string value
317: */
318: public InputStream evalStream(QueryContext context)
319: throws SQLException {
320: switch (_type) {
321: case NULL:
322: return null;
323:
324: case BINARY:
325: return _binaryStream;
326:
327: default:
328: throw new UnsupportedOperationException();
329: }
330: }
331:
332: public String toString() {
333: return "?" + _index;
334: }
335: }
|