KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > ql > 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.ejb.ql;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.util.CharBuffer;
33
34 /**
35  * A between expression
36  */

37 class BetweenExpr extends Expr {
38   // value expression
39
private Expr _value;
40   // min expression
41
private Expr _min;
42   // max expression
43
private Expr _max;
44   // true if this is a negative between
45
private boolean _isNot;
46
47   /**
48    * Creates a binary expression.
49    *
50    * @param value the value expression
51    * @param min the minimum expression
52    * @param max the maximum expression
53    * @param isNot if true, this the between is negated
54    */

55   BetweenExpr(Expr value, Expr min, Expr max, boolean isNot)
56     throws ConfigException
57   {
58     _value = value;
59     _min = min;
60     _max = max;
61     _isNot = isNot;
62
63     evalTypes();
64   }
65
66   /**
67    * Evaluates the types for the expression
68    */

69   void evalTypes()
70     throws ConfigException
71   {
72     if (getJavaType() != null)
73       return;
74
75     setJavaType(boolean.class);
76     
77     if (_value.isDate() && _min.isDate() && _max.isDate())
78       return;
79     
80     else if (_value.isDate())
81       throw error(L.l("BETWEEN expects date expression at `{0}'. All values must either be date values or all must be numeric values.", _value));
82
83     else if (_min.isDate())
84       throw error(L.l("BETWEEN expects date expression at `{0}'. All values must either be date values or all must be numeric values.", _min));
85     
86     else if (_max.isDate())
87       throw error(L.l("BETWEEN expects date expression at `{0}'. All values must either be date values or all must be numeric values.", _max));
88
89     if (! _value.isNumeric())
90       throw error(L.l("BETWEEN expects numeric expression at `{0}'", _value));
91     
92     if (! _min.isNumeric())
93       throw error(L.l("BETWEEN expects numeric expression at `{0}'", _min));
94     
95     if (! _max.isNumeric())
96       throw error(L.l("BETWEEN expects numeric expression at `{0}'", _max));
97   }
98   
99
100   /**
101    * Prints the where SQL for this expression
102    *
103    * @param cb the java code generator
104    */

105   void generateWhere(CharBuffer cb)
106   {
107     _value.generateWhereSubExpr(cb);
108
109     if (_isNot)
110       cb.append(" NOT BETWEEN ");
111     else
112       cb.append(" BETWEEN ");
113
114     _min.generateWhereSubExpr(cb);
115               
116     cb.append(" AND ");
117
118     _max.generateWhereSubExpr(cb);
119   }
120
121   /**
122    * Prints the where SQL for this expression
123    */

124   void generateWhereSubExpr(CharBuffer cb)
125   {
126     cb.append("(");
127     generateWhere(cb);
128     cb.append(")");
129   }
130
131   public String JavaDoc toString()
132   {
133     if (_isNot)
134       return "NOT BETWEEN " + _min + " AND " + _max;
135     else
136       return "BETWEEN " + _min + " AND " + _max;
137   }
138 }
139
Popular Tags