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.table.ForeignColumn;
034: import com.caucho.util.CharBuffer;
035:
036: import java.util.ArrayList;
037:
038: /**
039: * Unary expression.
040: */
041: public class UnaryExpr extends AbstractAmberExpr {
042: private AmberExpr _expr;
043: private int _token;
044:
045: /**
046: * Creates a new unary expression
047: */
048: public UnaryExpr(int token, AmberExpr expr) {
049: _token = token;
050: _expr = expr;
051: }
052:
053: /**
054: * Returns true for a boolean expr.
055: */
056: public boolean isBoolean() {
057: return (_token == QueryParser.NOT || _token == QueryParser.NULL || _token == QueryParser.NOT_NULL);
058: }
059:
060: /**
061: * Returns the java type.
062: */
063: public Class getJavaType() {
064: switch (_token) {
065: case QueryParser.NOT:
066: case QueryParser.NULL:
067: return boolean.class;
068: default:
069: return double.class;
070: }
071: }
072:
073: /**
074: * Binds the expression as a select item.
075: */
076: public AmberExpr bindSelect(QueryParser parser) {
077: _expr = _expr.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: switch (_token) {
087: case '-':
088: case '+':
089: case QueryParser.NULL:
090: case QueryParser.NOT_NULL:
091: return _expr.usesFrom(from, type);
092:
093: case QueryParser.NOT:
094: return _expr.usesFrom(from, type, !isNot);
095:
096: default:
097: return false;
098: }
099: }
100:
101: /**
102: * Generates the where expression.
103: */
104: public void generateWhere(CharBuffer cb) {
105: generateInternalWhere(cb, true);
106: }
107:
108: /**
109: * Generates the (update) where expression.
110: */
111: public void generateUpdateWhere(CharBuffer cb) {
112: generateInternalWhere(cb, false);
113: }
114:
115: /**
116: * Generates the having expression.
117: */
118: public void generateHaving(CharBuffer cb) {
119: generateWhere(cb);
120: }
121:
122: public String toString() {
123: String str = null;
124:
125: switch (_token) {
126: case '-':
127: str = "-";
128: break;
129: case '+':
130: str = "+";
131: break;
132: case QueryParser.NOT:
133: str = "not";
134: break;
135: case QueryParser.NULL:
136: return _expr + " is null";
137: case QueryParser.NOT_NULL:
138: return _expr + " is not null";
139: }
140:
141: return str + " " + _expr;
142: }
143:
144: //
145: // private
146:
147: private void generateInternalWhere(CharBuffer cb, boolean select) {
148: switch (_token) {
149: case '-':
150: cb.append(" -");
151: break;
152: case '+':
153: cb.append(" +");
154: break;
155: case QueryParser.NOT:
156: cb.append("NOT ");
157: break;
158: case QueryParser.NULL:
159: case QueryParser.NOT_NULL:
160: if (_expr instanceof ManyToOneExpr) {
161: ManyToOneExpr path = (ManyToOneExpr) _expr;
162: cb.append('(');
163:
164: ArrayList<ForeignColumn> keys = path.getLinkColumns()
165: .getColumns();
166: for (int i = 0; i < keys.size(); i++) {
167: if (i != 0)
168: cb.append(" and ");
169:
170: cb.append(path.getFromItem().getName());
171: cb.append(".");
172: cb.append(keys.get(i).getName());
173:
174: if (_token == QueryParser.NULL)
175: cb.append(" is null");
176: else
177: cb.append(" is not null");
178: }
179: cb.append(')');
180: } else {
181:
182: if (select)
183: _expr.generateWhere(cb);
184: else
185: _expr.generateUpdateWhere(cb);
186:
187: if (_token == QueryParser.NULL)
188: cb.append(" is null");
189: else
190: cb.append(" is not null");
191: }
192: return;
193: }
194:
195: if (select)
196: _expr.generateWhere(cb);
197: else
198: _expr.generateUpdateWhere(cb);
199: }
200: }
|