KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.db.sql;
30
31 import com.caucho.log.Log;
32
33 import java.sql.SQLException JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 class SubSelectParamExpr extends Expr {
38   private static final Logger JavaDoc log = Log.open(SubSelectParamExpr.class);
39
40   private SelectQuery _subselect;
41   private Expr _expr;
42   private int _index;
43
44   SubSelectParamExpr(Query subselect, Expr expr, int index)
45   {
46     _subselect = (SelectQuery) subselect;
47     _expr = expr;
48     _index = index;
49   }
50
51   /**
52    * Returns the type of the expression.
53    */

54   public Class JavaDoc getType()
55   {
56     return _expr.getType();
57   }
58
59   /**
60    * Returns the expr.
61    */

62   public Expr getExpr()
63   {
64     return _expr;
65   }
66
67   /**
68    * Returns the cost based on the given FromList.
69    */

70   public long subCost(ArrayList JavaDoc<FromItem> fromList)
71   {
72     return _subselect.getSubSelect().cost(fromList) + 1;
73   }
74
75   /**
76    * Binds the expression.
77    */

78   public Expr bind(Query parent)
79     throws SQLException JavaDoc
80   {
81     _expr = _expr.bind(parent);
82
83     return this;
84   }
85
86   /**
87    * Sets the value.
88    */

89   public void eval(QueryContext parent, QueryContext context)
90     throws SQLException JavaDoc
91   {
92     Class JavaDoc type = getType();
93
94     if (_expr.isNull(parent))
95       context.setNull(_index);
96     else if (long.class.equals(type))
97       context.setLong(_index, _expr.evalLong(parent));
98     else if (int.class.equals(type))
99       context.setLong(_index, _expr.evalLong(parent));
100     else {
101       context.setString(_index, _expr.evalString(parent));
102     }
103   }
104
105   /**
106    * Evaluates the expression as a string.
107    *
108    * @param rows the current database tuple
109    *
110    * @return the string value
111    */

112   public boolean isNull(QueryContext context)
113     throws SQLException JavaDoc
114   {
115     return context.isNull(_index);
116   }
117
118   /**
119    * Evaluates the expression as a string.
120    *
121    * @param rows the current database tuple
122    *
123    * @return the string value
124    */

125   public String JavaDoc evalString(QueryContext context)
126     throws SQLException JavaDoc
127   {
128     return context.getString(_index);
129   }
130
131   /**
132    * Evaluates the expression as a boolean.
133    *
134    * @param rows the current database tuple
135    *
136    * @return the boolean value
137    */

138   public int evalBoolean(QueryContext context)
139     throws SQLException JavaDoc
140   {
141     return context.getBoolean(_index);
142   }
143
144   /**
145    * Evaluates the expression as a long.
146    *
147    * @param rows the current database tuple
148    *
149    * @return the long value
150    */

151   public long evalLong(QueryContext context)
152     throws SQLException JavaDoc
153   {
154     return context.getLong(_index);
155   }
156
157   /**
158    * Evaluates the expression as a double.
159    *
160    * @param rows the current database tuple
161    *
162    * @return the double value
163    */

164   public double evalDouble(QueryContext context)
165     throws SQLException JavaDoc
166   {
167     return context.getDouble(_index);
168   }
169
170   /**
171    * Evaluates the expression as a date
172    *
173    * @param rows the current database tuple
174    *
175    * @return the date value
176    */

177   public long evalDate(QueryContext context)
178     throws SQLException JavaDoc
179   {
180     return context.getDate(_index);
181   }
182
183   public String JavaDoc toString()
184   {
185     return "SubSelectParamExpr[" + _expr + "]";
186   }
187 }
188
Popular Tags