KickJava   Java API By Example, From Geeks To Geeks.

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


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.logging.Logger JavaDoc;
37
38 class EqExpr extends Expr {
39   private static final Logger JavaDoc log = Log.open(EqExpr.class);
40
41   private Expr _left;
42   private Expr _right;
43
44   EqExpr(Expr left, Expr right)
45   {
46     _left = left;
47     _right = right;
48
49     if (left == null || right == null)
50       throw new NullPointerException JavaDoc();
51
52     if (right instanceof UnboundIdentifierExpr &&
53     ! (left instanceof UnboundIdentifierExpr)) {
54       Expr temp = _right;
55       _right = _left;
56       _left = temp;
57     }
58   }
59
60   protected Expr bind(Query query)
61     throws SQLException JavaDoc
62   {
63     Expr newLeft = _left.bind(query);
64     Expr newRight = _right.bind(query);
65
66     if (newLeft instanceof ColumnExpr &&
67     newLeft.getType().equals(String JavaDoc.class)) {
68       return new StringEqExpr((ColumnExpr) newLeft, newRight);
69     }
70     else if (newRight instanceof ColumnExpr &&
71          newRight.getType().equals(String JavaDoc.class)) {
72       return new StringEqExpr((ColumnExpr) newRight, newLeft);
73     }
74     
75     if (_left == newLeft && _right == newRight)
76       return this;
77     else
78       return new EqExpr(newLeft, newRight);
79   }
80
81   /**
82    * Returns an index expression if available.
83    */

84   public IndexExpr getIndexExpr(FromItem item)
85   {
86     if (_left instanceof IdExpr) {
87       IdExpr expr = (IdExpr) _left;
88
89       if (expr.getColumn().getIndex() != null &&
90       item == expr.getFromItem()) {
91     return new IndexExpr(expr, _right);
92       }
93     }
94     
95     if (_right instanceof IdExpr) {
96       IdExpr expr = (IdExpr) _right;
97
98       if (expr.getColumn().getIndex() != null &&
99       item == expr.getFromItem()) {
100     return new IndexExpr(expr, _left);
101       }
102     }
103     
104     return null;
105   }
106
107   /**
108    * Returns the type of the expression.
109    */

110   public Class JavaDoc getType()
111   {
112     return boolean.class;
113   }
114
115   /**
116    * Returns the cost based on the given FromList.
117    */

118   public long cost(ArrayList JavaDoc<FromItem> fromList)
119   {
120     if (_left instanceof UnboundIdentifierExpr &&
121     _right.cost(fromList) == 0) {
122       UnboundIdentifierExpr id = (UnboundIdentifierExpr) _left;
123
124       return id.lookupCost(fromList);
125     }
126     else if (_right instanceof UnboundIdentifierExpr &&
127          _left.cost(fromList) == 0) {
128       UnboundIdentifierExpr id = (UnboundIdentifierExpr) _right;
129
130       return id.lookupCost(fromList);
131     }
132     else {
133       return subCost(fromList);
134     }
135   }
136
137   /**
138    * Returns the cost based on a subitem.
139    */

140   public long subCost(ArrayList JavaDoc<FromItem> fromList)
141   {
142     return _left.subCost(fromList) + _right.subCost(fromList);
143   }
144
145   /**
146    * Evaluates the expression for nulls
147    */

148   public boolean isNull(QueryContext context)
149     throws SQLException JavaDoc
150   {
151     return (_left.isNull(context) || _right.isNull(context));
152   }
153
154   /**
155    * Evaluates the expression as a boolean.
156    */

157   public int evalBoolean(QueryContext context)
158     throws SQLException JavaDoc
159   {
160     if (_left.isNull(context) || _right.isNull(context))
161       return UNKNOWN;
162     
163     if (_left.isLong() && _right.isLong()) {
164       if (_left.evalLong(context) == _right.evalLong(context))
165     return TRUE;
166       else
167     return FALSE;
168     }
169     else if (_left.isDouble() && _right.isDouble()) {
170       if (_left.evalDouble(context) == _right.evalDouble(context))
171     return TRUE;
172       else
173     return FALSE;
174     }
175     else {
176       String JavaDoc leftValue = _left.evalString(context);
177       String JavaDoc rightValue = _right.evalString(context);
178
179       if (leftValue == rightValue || leftValue.equals(rightValue))
180     return TRUE;
181       else
182     return FALSE;
183     }
184   }
185
186   public String JavaDoc evalString(QueryContext context)
187     throws SQLException JavaDoc
188   {
189     throw new SQLException JavaDoc("can't convert string to boolean");
190   }
191
192   /**
193    * Evaluates aggregate functions during the group phase.
194    *
195    * @param state the current database tuple
196    */

197   public void evalGroup(QueryContext context)
198     throws SQLException JavaDoc
199   {
200     _left.evalGroup(context);
201     _right.evalGroup(context);
202   }
203
204   public String JavaDoc toString()
205   {
206     return "(" + _left + " = " + _right + ")";
207   }
208 }
209
Popular Tags