001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.expr;
030:
031: import com.caucho.amber.query.FromItem;
032: import com.caucho.amber.query.QueryParser;
033: import com.caucho.amber.type.BooleanType;
034: import com.caucho.amber.type.Type;
035: import com.caucho.util.CharBuffer;
036:
037: /**
038: * Bound identifier expression.
039: */
040: public class LikeExpr extends AbstractAmberExpr {
041: private AmberExpr _expr;
042:
043: private AmberExpr _value;
044: private String _escape;
045: private boolean _isNot;
046:
047: /**
048: * Creates a new cmp expression
049: */
050: public LikeExpr(AmberExpr expr, AmberExpr value, String escape,
051: boolean isNot) {
052: _expr = expr;
053: _value = value;
054: _isNot = isNot;
055: _escape = escape;
056: }
057:
058: /**
059: * Returns true for a boolean expression.
060: */
061: public boolean isBoolean() {
062: return true;
063: }
064:
065: /**
066: * Returns the expr type.
067: */
068: public Type getType() {
069: return BooleanType.create();
070: }
071:
072: /**
073: * Binds the expression as a select item.
074: */
075: public AmberExpr bindSelect(QueryParser parser) {
076: _expr = _expr.bindSelect(parser);
077: _value = _value.bindSelect(parser);
078:
079: return this ;
080: }
081:
082: /**
083: * Returns true if the expression uses the from item.
084: */
085: public boolean usesFrom(FromItem from, int type, boolean isNot) {
086: return (_expr.usesFrom(from, type));
087: }
088:
089: /**
090: * Returns true if the expression uses the from item.
091: */
092: public AmberExpr replaceJoin(JoinExpr join) {
093: _expr = _expr.replaceJoin(join);
094: _value = _value.replaceJoin(join);
095:
096: return this ;
097: }
098:
099: /**
100: * Generates the where expression.
101: */
102: public void generateWhere(CharBuffer cb) {
103: generateInternalWhere(cb, true);
104: }
105:
106: /**
107: * Generates the (update) where expression.
108: */
109: public void generateUpdateWhere(CharBuffer cb) {
110: generateInternalWhere(cb, false);
111: }
112:
113: /**
114: * Generates the having expression.
115: */
116: public void generateHaving(CharBuffer cb) {
117: generateWhere(cb);
118: }
119:
120: public String toString() {
121: CharBuffer cb = CharBuffer.allocate();
122:
123: cb.append('(');
124: cb.append(_expr);
125:
126: if (_isNot)
127: cb.append(" NOT");
128:
129: cb.append(" LIKE ");
130:
131: cb.append(_value);
132:
133: cb.append(')');
134:
135: return cb.close();
136: }
137:
138: //
139: // private
140:
141: private void generateInternalWhere(CharBuffer cb, boolean select) {
142: cb.append('(');
143:
144: if (select)
145: _expr.generateWhere(cb);
146: else
147: _expr.generateUpdateWhere(cb);
148:
149: if (_isNot)
150: cb.append(" NOT");
151:
152: cb.append(" LIKE ");
153:
154: if (select)
155: _value.generateWhere(cb);
156: else
157: _value.generateUpdateWhere(cb);
158:
159: if (_escape != null) {
160: cb.append(" ESCAPE ");
161: cb.append(_escape);
162: }
163:
164: cb.append(')');
165: }
166: }
|