KickJava   Java API By Example, From Geeks To Geeks.

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


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 LikeExpr extends AbstractAmberExpr {
41   private AmberExpr _expr;
42
43   private AmberExpr _value;
44   private String JavaDoc _escape;
45   private boolean _isNot;
46
47   /**
48    * Creates a new cmp expression
49    */

50   public LikeExpr(AmberExpr expr,
51                   AmberExpr value,
52                   String JavaDoc escape,
53                   boolean isNot)
54   {
55     _expr = expr;
56     _value = value;
57     _isNot = isNot;
58     _escape = escape;
59   }
60
61   /**
62    * Returns true for a boolean expression.
63    */

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

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

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

91   public boolean usesFrom(FromItem from, int type, boolean isNot)
92   {
93     return (_expr.usesFrom(from, type));
94   }
95
96   /**
97    * Returns true if the expression uses the from item.
98    */

99   public AmberExpr replaceJoin(JoinExpr join)
100   {
101     _expr = _expr.replaceJoin(join);
102     _value = _value.replaceJoin(join);
103
104     return this;
105   }
106
107   /**
108    * Generates the where expression.
109    */

110   public void generateWhere(CharBuffer cb)
111   {
112     generateInternalWhere(cb, true);
113   }
114
115   /**
116    * Generates the (update) where expression.
117    */

118   public void generateUpdateWhere(CharBuffer cb)
119   {
120     generateInternalWhere(cb, false);
121   }
122
123   /**
124    * Generates the having expression.
125    */

126   public void generateHaving(CharBuffer cb)
127   {
128     generateWhere(cb);
129   }
130
131   public String JavaDoc toString()
132   {
133     CharBuffer cb = CharBuffer.allocate();
134
135     cb.append('(');
136     cb.append(_expr);
137
138     if (_isNot)
139       cb.append(" NOT");
140
141     cb.append(" LIKE ");
142
143     cb.append(_value);
144
145     cb.append(')');
146
147     return cb.close();
148   }
149
150   //
151
// private
152

153   private void generateInternalWhere(CharBuffer cb,
154                                      boolean select)
155   {
156     cb.append('(');
157
158     if (select)
159       _expr.generateWhere(cb);
160     else
161       _expr.generateUpdateWhere(cb);
162
163     if (_isNot)
164       cb.append(" NOT");
165
166     cb.append(" LIKE ");
167
168     if (select)
169       _value.generateWhere(cb);
170     else
171       _value.generateUpdateWhere(cb);
172
173     if (_escape != null) {
174       cb.append(" ESCAPE ");
175       cb.append(_escape);
176     }
177
178     cb.append(')');
179   }
180 }
181
Popular Tags