KickJava   Java API By Example, From Geeks To Geeks.

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


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
33 import java.sql.SQLException JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 class BetweenExpr extends Expr {
38   private static final Logger JavaDoc log = Log.open(BetweenExpr.class);
39
40   private Expr _expr;
41   private Expr _min;
42   private Expr _max;
43   private boolean _isNot;
44
45   private boolean _isLong;
46
47   BetweenExpr(Expr expr, Expr min, Expr max, boolean isNot)
48   {
49     _expr = expr;
50     _min = min;
51     _max = max;
52     _isNot = isNot;
53   }
54
55   protected Expr bind(Query query)
56     throws SQLException JavaDoc
57   {
58     _expr = _expr.bind(query);
59     _min = _min.bind(query);
60     _max = _max.bind(query);
61
62     _isLong = _expr.isLong();
63
64     return this;
65   }
66
67   /**
68    * Returns the type of the expression.
69    */

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

78   public long subCost(ArrayList JavaDoc<FromItem> fromList)
79   {
80     return (_expr.subCost(fromList) +
81         _min.subCost(fromList) +
82         _max.subCost(fromList));
83   }
84
85   /**
86    * Evaluates the expression as a boolean.
87    */

88   public int evalBoolean(QueryContext context)
89     throws SQLException JavaDoc
90   {
91     if (_expr.isNull(context))
92       return UNKNOWN;
93     
94     if (_isLong) {
95       long min = _min.evalLong(context);
96       long max = _max.evalLong(context);
97
98       long value = _expr.evalLong(context);
99
100       if (_isNot)
101     return ! (min <= value && value <= max) ? TRUE : FALSE;
102       else
103     return min <= value && value <= max ? TRUE : FALSE;
104     }
105     else {
106       double min = _min.evalDouble(context);
107       double max = _max.evalDouble(context);
108
109       double value = _expr.evalDouble(context);
110
111       if (_isNot)
112     return ! (min <= value && value <= max) ? TRUE : FALSE;
113       else
114     return min <= value && value <= max ? TRUE : FALSE;
115     }
116   }
117
118   public String JavaDoc evalString(QueryContext context)
119     throws SQLException JavaDoc
120   {
121     throw new SQLException JavaDoc("can't convert string to boolean");
122   }
123
124   /**
125    * Evaluates aggregate functions during the group phase.
126    *
127    * @param state the current database tuple
128    */

129   public void evalGroup(QueryContext context)
130     throws SQLException JavaDoc
131   {
132     _expr.evalGroup(context);
133     _min.evalGroup(context);
134     _max.evalGroup(context);
135   }
136
137   public String JavaDoc toString()
138   {
139     return "(" + _expr + " BETWEEN " + _min + " AND " + _max + ")";
140   }
141 }
142
Popular Tags