KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.caucho.util.L10N;
33
34 import java.sql.SQLException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 public class SubSelectExpr extends Expr {
39   protected static final L10N L = new L10N(SubSelectExpr.class);
40   private static final Logger JavaDoc log = Log.open(SubSelectExpr.class);
41
42   private int _groupIndex;
43   private SelectQuery _subselect;
44
45   private Query _parentQuery;
46   
47   SubSelectExpr(SelectQuery query)
48   {
49     _subselect = query;
50   }
51
52   /**
53    * Returns the query.
54    */

55   public SelectQuery getSubSelect()
56   {
57     return _subselect;
58   }
59
60   /**
61    * Binds the expression to the query.
62    */

63   protected Expr bind(Query query)
64     throws SQLException JavaDoc
65   {
66     if (_parentQuery != null)
67       return this;
68     
69     _parentQuery = query;
70     _groupIndex = query.getDataFields();
71     
72     query.setDataFields(_groupIndex + 1);
73
74     _subselect.bind();
75     
76     return this;
77   }
78
79   /**
80    * Returns the expected result type of the expression.
81    */

82   public Class JavaDoc getType()
83   {
84     return _subselect.getType();
85   }
86   
87   ArrayList JavaDoc<SubSelectParamExpr> getParamExprs()
88   {
89     return _subselect.getParamExprs();
90   }
91
92   /**
93    * Returns the cost based on the given FromList.
94    */

95   public long subCost(ArrayList JavaDoc<FromItem> fromList)
96   {
97     ArrayList JavaDoc<SubSelectParamExpr> paramExprs = getParamExprs();
98     
99     long cost = 10;
100
101     for (int i = 0; i < paramExprs.size(); i++)
102       cost += paramExprs.get(i).getExpr().cost(fromList);
103
104     return 2 * cost;
105   }
106
107   /**
108    * Evaluates the subselect.
109    */

110   void evaluate(QueryContext context)
111     throws SQLException JavaDoc
112   {
113     QueryContext subcontext = QueryContext.allocate();
114
115     ArrayList JavaDoc<SubSelectParamExpr> paramExprs = getParamExprs();
116     
117     for (int i = 0; i < paramExprs.size(); i++) {
118       paramExprs.get(i).eval(context, subcontext);
119     }
120     
121     _subselect.execute(subcontext, context.getTransaction());
122
123     SelectResult result = subcontext.getResult();
124
125     Data data = context.getGroupData(_groupIndex);
126
127     if (result.next()) {
128       Class JavaDoc type = _subselect.getType();
129
130       if (long.class.equals(type))
131     data.setLong(result.getLong(0));
132       else
133     data.setString(result.getString(0));
134     }
135     else {
136       data.clear();
137     }
138
139     result.close();
140     QueryContext.free(subcontext);
141   }
142
143   /**
144    * Evaluates the expression as a string.
145    *
146    * @param rows the current database tuple
147    *
148    * @return the string value
149    */

150   public String JavaDoc evalString(QueryContext context)
151     throws SQLException JavaDoc
152   {
153     Data data = context.getGroupData(_groupIndex);
154
155     return data.getString();
156   }
157
158   public String JavaDoc toString()
159   {
160     return "SubSelectExpr[" + _subselect + "]";
161   }
162 }
163
Popular Tags