KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > InExpr


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 import java.util.ArrayList JavaDoc;
38
39 /**
40  * "in" expression
41  */

42 public class InExpr extends AbstractAmberExpr {
43   private AmberExpr _expr;
44
45   private ArrayList JavaDoc<AmberExpr> _values;
46   private boolean _isNot;
47
48   /**
49    * Creates a new cmp expression
50    */

51   public InExpr(AmberExpr expr,
52                 ArrayList JavaDoc<AmberExpr> values,
53                 boolean isNot)
54   {
55     _expr = expr;
56     _values = values;
57     _isNot = isNot;
58   }
59
60   /**
61    * Returns true for a boolean expression.
62    */

63   public boolean isBoolean()
64   {
65     return true;
66   }
67
68   /**
69    * Returns the expr type.
70    */

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

79   public AmberExpr bindSelect(QueryParser parser)
80   {
81     _expr = _expr.bindSelect(parser);
82
83     for (int i = 0; i < _values.size(); i++) {
84       _values.set(i, _values.get(i).bindSelect(parser));
85     }
86
87     return this;
88   }
89
90   /**
91    * Returns true if the expression uses the from item.
92    */

93   public boolean usesFrom(FromItem from, int type, boolean isNot)
94   {
95     if (_expr.usesFrom(from, type))
96       return true;
97
98     for (int i = 0; i < _values.size(); i++) {
99       if (_values.get(i).usesFrom(from, type))
100         return true;
101     }
102
103     return false;
104   }
105
106   /**
107    * Returns true if the expression uses the from item.
108    */

109   public AmberExpr replaceJoin(JoinExpr join)
110   {
111     _expr = _expr.replaceJoin(join);
112
113     for (int i = 0; i < _values.size(); i++) {
114       _values.set(i, _values.get(i).replaceJoin(join));
115     }
116
117     return this;
118   }
119
120   /**
121    * Generates the where expression.
122    */

123   public void generateWhere(CharBuffer cb)
124   {
125     generateInternalWhere(cb, true);
126   }
127
128   /**
129    * Generates the (update) where expression.
130    */

131   public void generateUpdateWhere(CharBuffer cb)
132   {
133     generateInternalWhere(cb, false);
134   }
135
136   /**
137    * Generates the having expression.
138    */

139   public void generateHaving(CharBuffer cb)
140   {
141     generateWhere(cb);
142   }
143
144   public String JavaDoc toString()
145   {
146     CharBuffer cb = new CharBuffer();
147
148     cb.append('(');
149     cb.append(_expr);
150
151     if (_isNot)
152       cb.append(" NOT");
153
154     cb.append(" IN (");
155
156     for (int i = 0; i < _values.size(); i++) {
157       if (i != 0)
158         cb.append(',');
159
160       cb.append(_values.get(i));
161     }
162
163     cb.append("))");
164
165     return cb.toString();
166   }
167
168   //
169
// private
170

171   private void generateInternalWhere(CharBuffer cb,
172                                      boolean select)
173   {
174     cb.append('(');
175
176     if (select)
177       _expr.generateWhere(cb);
178     else
179       _expr.generateUpdateWhere(cb);
180
181     if (_isNot)
182       cb.append(" NOT");
183
184     cb.append(" IN (");
185
186     for (int i = 0; i < _values.size(); i++) {
187       if (i != 0)
188         cb.append(',');
189
190       if (select)
191         _values.get(i).generateWhere(cb);
192       else
193         _values.get(i).generateUpdateWhere(cb);
194     }
195
196     cb.append("))");
197   }
198 }
199
Popular Tags