KickJava   Java API By Example, From Geeks To Geeks.

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


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 Software Foundation, 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 CmpExpr extends Expr {
39   private static final Logger JavaDoc log = Log.open(CmpExpr.class);
40
41   private Expr _left;
42   private Expr _right;
43   private int _op;
44
45   CmpExpr(Expr left, Expr right, int op)
46   {
47     _left = left;
48     _right = right;
49     _op = op;
50   }
51
52   protected Expr bind(Query query)
53     throws SQLException JavaDoc
54   {
55     _left = _left.bind(query);
56     _right = _right.bind(query);
57
58     switch (_op) {
59     case Parser.LT:
60     case Parser.LE:
61     case Parser.GT:
62     case Parser.GE:
63       return new DoubleCmpExpr(_op, _left, _right);
64     }
65
66     if (_left.isDouble() || _right.isDouble())
67       return new DoubleCmpExpr(_op, _left, _right);
68
69     return this;
70   }
71
72   /**
73    * Returns the type of the expression.
74    */

75   public Class JavaDoc getType()
76   {
77     return boolean.class;
78   }
79
80   /**
81    * Returns the cost based on the given FromList.
82    */

83   public long subCost(ArrayList JavaDoc<FromItem> fromList)
84   {
85     return _left.subCost(fromList) + _right.subCost(fromList);
86   }
87
88   /**
89    * Evaluates the expression as a boolean.
90    */

91   public int evalBoolean(QueryContext context)
92     throws SQLException JavaDoc
93   {
94     if (_left.isNull(context) || _right.isNull(context))
95       return UNKNOWN;
96     
97     switch (_op) {
98     case Parser.NE:
99       {
100     String JavaDoc leftValue = _left.evalString(context);
101     String JavaDoc rightValue = _right.evalString(context);
102
103     if (! (leftValue == rightValue ||
104            leftValue != null && leftValue.equals(rightValue)))
105       return TRUE;
106     else
107       return FALSE;
108       }
109     
110     case Parser.EQ:
111       {
112     String JavaDoc leftValue = _left.evalString(context);
113     String JavaDoc rightValue = _right.evalString(context);
114
115     if (leftValue == rightValue ||
116         leftValue != null && leftValue.equals(rightValue))
117       return TRUE;
118     else
119       return FALSE;
120       }
121
122     default:
123       throw new SQLException JavaDoc("can't compare");
124     }
125   }
126
127   public String JavaDoc evalString(QueryContext context)
128     throws SQLException JavaDoc
129   {
130     throw new SQLException JavaDoc("can't convert string to boolean");
131   }
132
133   /**
134    * Evaluates aggregate functions during the group phase.
135    *
136    * @param state the current database tuple
137    */

138   public void evalGroup(QueryContext context)
139     throws SQLException JavaDoc
140   {
141     _left.evalGroup(context);
142     _right.evalGroup(context);
143   }
144
145   public String JavaDoc toString()
146   {
147     switch (_op) {
148     case Parser.EQ:
149       return "(" + _left + " = " + _right + ")";
150     case Parser.NE:
151       return "(" + _left + " <> " + _right + ")";
152     case Parser.LT:
153       return "(" + _left + " < " + _right + ")";
154     case Parser.LE:
155       return "(" + _left + " <= " + _right + ")";
156     case Parser.GT:
157       return "(" + _left + " > " + _right + ")";
158     case Parser.GE:
159       return "(" + _left + " >= " + _right + ")";
160     default:
161       return "(" + _left + " =? " + _right + ")";
162     }
163   }
164 }
165
Popular Tags