KickJava   Java API By Example, From Geeks To Geeks.

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


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 ExistsExpr extends SubSelectExpr {
39   protected static final L10N L = new L10N(ExistsExpr.class);
40   private static final Logger JavaDoc log = Log.open(ExistsExpr.class);
41
42   private int _groupIndex;
43   private ExistsQuery _exists;
44
45   private Query _parentQuery;
46   
47   ExistsExpr(ExistsQuery query)
48   {
49     super(query);
50     
51     _exists = query;
52   }
53
54   /**
55    * Binds the expression to the query.
56    */

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

79   public Class JavaDoc getType()
80   {
81     return boolean.class;
82   }
83   
84   ArrayList JavaDoc<SubSelectParamExpr> getParamExprs()
85   {
86     return _exists.getParamExprs();
87   }
88
89   /**
90    * Returns the cost based on the given FromList.
91    */

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

107   void evaluate(QueryContext context)
108     throws SQLException JavaDoc
109   {
110     QueryContext subcontext = QueryContext.allocate();
111
112     ArrayList JavaDoc<SubSelectParamExpr> paramExprs = getParamExprs();
113     
114     for (int i = 0; i < paramExprs.size(); i++) {
115       paramExprs.get(i).eval(context, subcontext);
116     }
117
118     boolean exists = _exists.exists(subcontext, context.getTransaction());
119     Data data = context.getGroupData(_groupIndex);
120
121     data.setBoolean(exists);
122     
123     QueryContext.free(subcontext);
124   }
125
126   /**
127    * Evaluates the expression to check for null
128    *
129    * @param rows the current database tuple
130    *
131    * @return the null value
132    */

133   public boolean isNull(QueryContext context)
134     throws SQLException JavaDoc
135   {
136     Data data = context.getGroupData(_groupIndex);
137
138     return data.isNull();
139   }
140
141   /**
142    * Evaluates the expression as a string.
143    *
144    * @param rows the current database tuple
145    *
146    * @return the string value
147    */

148   public int evalBoolean(QueryContext context)
149     throws SQLException JavaDoc
150   {
151     Data data = context.getGroupData(_groupIndex);
152
153     return data.getBoolean();
154   }
155
156   public String JavaDoc toString()
157   {
158     return "ExistsExpr[" + _exists + "]";
159   }
160 }
161
Popular Tags