KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > sql > ParamExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.db.sql;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.QDate;
34
35 import java.io.InputStream JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 class ParamExpr extends Expr {
41   private static final Logger JavaDoc log = Log.open(ParamExpr.class);
42   
43   private static final int NULL = 0;
44   private static final int BOOLEAN = NULL + 1;
45   private static final int STRING = BOOLEAN + 1;
46   private static final int LONG = STRING + 1;
47   private static final int DOUBLE = LONG + 1;
48   private static final int DATE = DOUBLE + 1;
49   private static final int BINARY = DATE + 1;
50
51   private int _index;
52   
53   private int _type = NULL;
54   
55   private String JavaDoc _stringValue;
56   private long _longValue;
57   private double _doubleValue;
58   
59   private InputStream JavaDoc _binaryStream;
60   private int _streamLength;
61
62   ParamExpr(int index)
63   {
64     _index = index;
65   }
66
67   /**
68    * Returns the type of the expression.
69    */

70   public Class JavaDoc getType()
71   {
72     switch (_type) {
73     case NULL:
74       return Object JavaDoc.class;
75
76     case BOOLEAN:
77       return boolean.class;
78
79     case STRING:
80       return String JavaDoc.class;
81
82     case LONG:
83       return long.class;
84
85     case DOUBLE:
86       return double.class;
87
88     case DATE:
89       return java.util.Date JavaDoc.class;
90
91     case BINARY:
92       return java.io.InputStream JavaDoc.class;
93       
94     default:
95       return Object JavaDoc.class;
96     }
97   }
98
99   /**
100    * Returns the subcost based on the given FromList.
101    */

102   public long subCost(ArrayList JavaDoc<FromItem> fromList)
103   {
104     return 0;
105   }
106
107   /**
108    * Clers the value.
109    */

110   public void clear()
111   {
112     _type = NULL;
113   }
114
115   /**
116    * Sets the value as a string.
117    */

118   public void setString(String JavaDoc value)
119   {
120     if (value == null)
121       _type = NULL;
122     else {
123       _type = STRING;
124       _stringValue = value;
125     }
126   }
127
128   /**
129    * Sets the value as a boolean.
130    */

131   public void setBoolean(boolean value)
132   {
133     _type = BOOLEAN;
134     _longValue = value ? 1 : 0;
135   }
136
137   /**
138    * Sets the value as a long.
139    */

140   public void setLong(long value)
141   {
142     _type = LONG;
143     _longValue = value;
144   }
145
146   /**
147    * Sets the value as a double.
148    */

149   public void setDouble(double value)
150   {
151     _type = DOUBLE;
152     _doubleValue = value;
153   }
154
155   /**
156    * Sets the value as a date.
157    */

158   public void setDate(long value)
159   {
160     _type = DATE;
161     _longValue = value;
162   }
163
164   /**
165    * Sets the value as a stream.
166    */

167   public void setBinaryStream(InputStream JavaDoc is, int length)
168   {
169     _type = BINARY;
170     _binaryStream = is;
171     _streamLength = length;
172   }
173
174   /**
175    * Evaluates the expression as a string.
176    *
177    * @param rows the current database tuple
178    *
179    * @return the string value
180    */

181   public boolean isNull(QueryContext context)
182     throws SQLException JavaDoc
183   {
184     return _type == NULL;
185   }
186
187   /**
188    * Evaluates the expression as a string.
189    *
190    * @param rows the current database tuple
191    *
192    * @return the string value
193    */

194   public String JavaDoc evalString(QueryContext context)
195     throws SQLException JavaDoc
196   {
197     switch (_type) {
198     case NULL:
199       return null;
200       
201     case BOOLEAN:
202       return _longValue != 0 ? "1" : "0";
203       
204     case STRING:
205       return _stringValue;
206       
207     case LONG:
208       return String.valueOf(_longValue);
209       
210     case DATE:
211       return QDate.formatISO8601(_longValue);
212       
213     case DOUBLE:
214       return String.valueOf(_doubleValue);
215
216     default:
217       throw new UnsupportedOperationException JavaDoc(String.valueOf(_type));
218     }
219   }
220
221   /**
222    * Evaluates the expression as a boolean.
223    *
224    * @param rows the current database tuple
225    *
226    * @return the boolean value
227    */

228   public int evalBoolean(QueryContext context)
229     throws SQLException JavaDoc
230   {
231     switch (_type) {
232     case NULL:
233       return UNKNOWN;
234       
235     case BOOLEAN:
236     case LONG:
237       return _longValue != 0 ? TRUE : FALSE;
238       
239     case DOUBLE:
240       return _doubleValue != 0 ? TRUE : FALSE;
241
242     default:
243       throw new UnsupportedOperationException JavaDoc();
244     }
245   }
246
247   /**
248    * Evaluates the expression as a long.
249    *
250    * @param rows the current database tuple
251    *
252    * @return the long value
253    */

254   public long evalLong(QueryContext context)
255     throws SQLException JavaDoc
256   {
257     switch (_type) {
258     case NULL:
259       return 0;
260       
261     case BOOLEAN:
262     case LONG:
263     case DATE:
264       return _longValue;
265
266     case DOUBLE:
267       return (long) _doubleValue;
268
269     case STRING:
270       return Long.parseLong(_stringValue);
271
272     default:
273       throw new UnsupportedOperationException JavaDoc("" + _type);
274     }
275   }
276
277   /**
278    * Evaluates the expression as a double.
279    *
280    * @param rows the current database tuple
281    *
282    * @return the double value
283    */

284   public double evalDouble(QueryContext context)
285     throws SQLException JavaDoc
286   {
287     switch (_type) {
288     case NULL:
289       return 0;
290       
291     case LONG:
292     case DATE:
293       return _longValue;
294
295     case DOUBLE:
296       return _doubleValue;
297
298     case STRING:
299       return Double.parseDouble(_stringValue);
300
301     default:
302       throw new UnsupportedOperationException JavaDoc();
303     }
304   }
305
306   /**
307    * Evaluates the expression as a date
308    *
309    * @param rows the current database tuple
310    *
311    * @return the date value
312    */

313   public long evalDate(QueryContext context)
314     throws SQLException JavaDoc
315   {
316     switch (_type) {
317     case NULL:
318       return 0;
319       
320     case LONG:
321     case DATE:
322       return _longValue;
323
324     case DOUBLE:
325       return (long) _doubleValue;
326
327     default:
328       throw new UnsupportedOperationException JavaDoc();
329     }
330   }
331
332   /**
333    * Evaluates the expression as a stream.
334    *
335    * @param rows the current database tuple
336    *
337    * @return the string value
338    */

339   public InputStream JavaDoc evalStream(QueryContext context)
340     throws SQLException JavaDoc
341   {
342     switch (_type) {
343     case NULL:
344       return null;
345       
346     case BINARY:
347       return _binaryStream;
348
349     default:
350       throw new UnsupportedOperationException JavaDoc();
351     }
352   }
353
354   public String JavaDoc toString()
355   {
356     return "?" + _index;
357   }
358 }
359
Popular Tags