KickJava   Java API By Example, From Geeks To Geeks.

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


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 SoftwareFoundation, 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
34 import java.sql.SQLException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashSet JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 class InExpr extends Expr {
40   private static final Logger JavaDoc log = Log.open(InExpr.class);
41
42   private Expr _expr;
43   private final HashSet JavaDoc<String JavaDoc> _values;
44   private final boolean _isNot;
45
46   InExpr(Expr expr, HashSet JavaDoc<String JavaDoc> values, boolean isNot)
47   {
48     _expr = expr;
49     _values = values;
50     _isNot = isNot;
51   }
52
53   /**
54    * Binds the expression to the actual tables.
55    */

56   protected Expr bind(Query query)
57     throws SQLException JavaDoc
58   {
59     _expr = _expr.bind(query);
60
61     return this;
62   }
63
64   /**
65    * Returns the type of the expression.
66    */

67   public Class JavaDoc getType()
68   {
69     return boolean.class;
70   }
71
72   /**
73    * Returns the cost based on the given FromList.
74    */

75   public long subCost(ArrayList JavaDoc<FromItem> fromList)
76   {
77     return _expr.subCost(fromList);
78   }
79
80   /**
81    * Evaluates the expression as a boolean
82    *
83    * @param rows the current tuple being evaluated
84    *
85    * @return the boolean value
86    */

87   public int evalBoolean(QueryContext context)
88     throws SQLException JavaDoc
89   {
90     if (_values.contains(_expr.evalString(context)))
91       return _isNot ? FALSE : TRUE;
92     else
93       return _isNot ? TRUE : FALSE;
94   }
95
96   /**
97    * Evaluates the expression as a string.
98    *
99    * @param rows the current tuple being evaluated
100    *
101    * @return the string value
102    */

103   public String JavaDoc evalString(QueryContext context)
104     throws SQLException JavaDoc
105   {
106     return (evalBoolean(context) == TRUE) ? "1" : "0";
107   }
108
109   /**
110    * Evaluates aggregate functions during the group phase.
111    *
112    * @param state the current database tuple
113    */

114   public void evalGroup(QueryContext context)
115     throws SQLException JavaDoc
116   {
117     _expr.evalGroup(context);
118   }
119
120   public String JavaDoc toString()
121   {
122     return _expr + " IN " + _values;
123   }
124 }
125
Popular Tags