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 BetweenExpr extends AbstractAmberExpr {
041: private AmberExpr _expr;
042:
043: private AmberExpr _min;
044: private AmberExpr _max;
045:
046: private boolean _isNot;
047:
048: /**
049: * Creates a new cmp expression
050: */
051: public BetweenExpr(AmberExpr expr, AmberExpr min, AmberExpr max,
052: boolean isNot) {
053: _expr = expr;
054: _min = min;
055: _max = max;
056:
057: _isNot = isNot;
058: }
059:
060: /**
061: * Returns true for a boolean expression.
062: */
063: public boolean isBoolean() {
064: return true;
065: }
066:
067: /**
068: * Returns the expr type.
069: */
070: public Type getType() {
071: return BooleanType.create();
072: }
073:
074: /**
075: * Binds the expression as a select item.
076: */
077: public AmberExpr bindSelect(QueryParser parser) {
078: _expr = _expr.bindSelect(parser);
079: _min = _min.bindSelect(parser);
080: _max = _max.bindSelect(parser);
081:
082: return this ;
083: }
084:
085: /**
086: * Returns true if the expression uses the from item.
087: */
088: public boolean usesFrom(FromItem from, int type, boolean isNot) {
089: return (_expr.usesFrom(from, type) || _min.usesFrom(from, type) || _max
090: .usesFrom(from, type));
091: }
092:
093: /**
094: * Generates the where expression.
095: */
096: public void generateWhere(CharBuffer cb) {
097: generateInternalWhere(cb, true);
098: }
099:
100: /**
101: * Generates the (update) where expression.
102: */
103: public void generateUpdateWhere(CharBuffer cb) {
104: generateInternalWhere(cb, false);
105: }
106:
107: //
108: // private
109:
110: private void generateInternalWhere(CharBuffer cb, boolean select) {
111: cb.append('(');
112:
113: if (select)
114: _expr.generateWhere(cb);
115: else
116: _expr.generateUpdateWhere(cb);
117:
118: if (_isNot)
119: cb.append(" NOT");
120:
121: cb.append(" BETWEEN ");
122:
123: if (select)
124: _min.generateWhere(cb);
125: else
126: _min.generateUpdateWhere(cb);
127:
128: cb.append(" AND ");
129:
130: if (select)
131: _max.generateWhere(cb);
132: else
133: _max.generateUpdateWhere(cb);
134:
135: cb.append(')');
136: }
137:
138: /**
139: * Generates the having expression.
140: */
141: public void generateHaving(CharBuffer cb) {
142: generateWhere(cb);
143: }
144:
145: public String toString() {
146: CharBuffer cb = CharBuffer.allocate();
147:
148: cb.append('(');
149: cb.append(_expr);
150:
151: if (_isNot)
152: cb.append(" NOT");
153:
154: cb.append(" BETWEEN ");
155:
156: cb.append(_min);
157:
158: cb.append(" AND ");
159:
160: cb.append(_max);
161:
162: cb.append(')');
163:
164: return cb.close();
165: }
166: }
|