KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > 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 Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.expr;
30
31 import com.caucho.amber.query.FromItem;
32 import com.caucho.amber.query.QueryParser;
33 import com.caucho.amber.type.BooleanType;
34 import com.caucho.amber.type.Type;
35 import com.caucho.util.CharBuffer;
36
37 /**
38  * Bound identifier expression.
39  */

40 public class BetweenExpr extends AbstractAmberExpr {
41   private AmberExpr _expr;
42
43   private AmberExpr _min;
44   private AmberExpr _max;
45
46   private boolean _isNot;
47
48   /**
49    * Creates a new cmp expression
50    */

51   public BetweenExpr(AmberExpr expr,
52                      AmberExpr min,
53                      AmberExpr max,
54                      boolean isNot)
55   {
56     _expr = expr;
57     _min = min;
58     _max = max;
59
60     _isNot = isNot;
61   }
62
63   /**
64    * Returns true for a boolean expression.
65    */

66   public boolean isBoolean()
67   {
68     return true;
69   }
70
71   /**
72    * Returns the expr type.
73    */

74   public Type getType()
75   {
76     return BooleanType.create();
77   }
78
79   /**
80    * Binds the expression as a select item.
81    */

82   public AmberExpr bindSelect(QueryParser parser)
83   {
84     _expr = _expr.bindSelect(parser);
85     _min = _min.bindSelect(parser);
86     _max = _max.bindSelect(parser);
87
88     return this;
89   }
90
91   /**
92    * Returns true if the expression uses the from item.
93    */

94   public boolean usesFrom(FromItem from, int type, boolean isNot)
95   {
96     return (_expr.usesFrom(from, type) ||
97             _min.usesFrom(from, type) ||
98             _max.usesFrom(from, type));
99   }
100
101   /**
102    * Generates the where expression.
103    */

104   public void generateWhere(CharBuffer cb)
105   {
106     generateInternalWhere(cb, true);
107   }
108
109   /**
110    * Generates the (update) where expression.
111    */

112   public void generateUpdateWhere(CharBuffer cb)
113   {
114     generateInternalWhere(cb, false);
115   }
116
117   //
118
// private
119

120   private void generateInternalWhere(CharBuffer cb,
121                                      boolean select)
122   {
123     cb.append('(');
124
125     if (select)
126       _expr.generateWhere(cb);
127     else
128       _expr.generateUpdateWhere(cb);
129
130     if (_isNot)
131       cb.append(" NOT");
132
133     cb.append(" BETWEEN ");
134
135     if (select)
136       _min.generateWhere(cb);
137     else
138       _min.generateUpdateWhere(cb);
139
140     cb.append(" AND ");
141
142     if (select)
143       _max.generateWhere(cb);
144     else
145       _max.generateUpdateWhere(cb);
146
147     cb.append(')');
148   }
149
150   /**
151    * Generates the having expression.
152    */

153   public void generateHaving(CharBuffer cb)
154   {
155     generateWhere(cb);
156   }
157
158   public String JavaDoc toString()
159   {
160     CharBuffer cb = CharBuffer.allocate();
161
162     cb.append('(');
163     cb.append(_expr);
164
165     if (_isNot)
166       cb.append(" NOT");
167
168     cb.append(" BETWEEN ");
169
170     cb.append(_min);
171
172     cb.append(" AND ");
173
174     cb.append(_max);
175
176     cb.append(')');
177
178     return cb.close();
179   }
180 }
181
Popular Tags