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 SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.ql;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.util.CharBuffer;
033:
034: /**
035: * An 'is' expression
036: */
037: class IsExpr extends Expr {
038: // value expression
039: private Expr _value;
040: // operation
041: private int _op;
042: // true if this is a negative between
043: private boolean _isNot;
044:
045: /**
046: * Creates a like expression.
047: *
048: * @param value the value expression
049: * @param op the operation
050: * @param isNot if true, this the like is negated
051: */
052: IsExpr(Query query, Expr value, int op, boolean isNot)
053: throws ConfigException {
054: _query = query;
055:
056: _value = value;
057: _op = op;
058: _isNot = isNot;
059:
060: evalTypes();
061: }
062:
063: /**
064: * Evaluates the types for the expression
065: */
066: void evalTypes() throws ConfigException {
067: if (getJavaType() != null)
068: return;
069:
070: if (_op == Query.EMPTY && !_value.isCollection())
071: throw error(L.l("IS EMPTY requires a collection at `{0}'",
072: _value));
073: else if (_op == Query.NULL && _value.isCollection())
074: throw error(L.l("IS NULL requires a single value at `{0}'",
075: _value));
076:
077: setJavaType(boolean.class);
078: }
079:
080: /**
081: * Prints the where SQL for this expression
082: *
083: * @param gen the java code generator
084: */
085: void generateWhere(CharBuffer cb) {
086: _value.generateWhereSubExpr(cb);
087:
088: Class valueType = _value.getJavaType();
089: /*
090: if (javax.ejb.EJBLocalObject.class.isAssignableFrom(valueType)) {
091: EjbEntityBean bean = _query.getBeanByType(valueType);
092:
093: valueType = bean.getPrimaryKey().getJavaType();
094:
095: if (byte.class.equals(valueType) ||
096: short.class.equals(valueType) ||
097: int.class.equals(valueType) ||
098: long.class.equals(valueType)) {
099: if (_op == Query.NULL) {
100: if (_isNot)
101: cb.append(" <> 0");
102: else
103: cb.append(" = 0");
104: return;
105: }
106: }
107: throw new UnsupportedOperationException();
108: }
109: */
110:
111: if (_isNot)
112: cb.append(" IS NOT ");
113: else
114: cb.append(" IS ");
115:
116: switch (_op) {
117: case Query.NULL:
118: cb.append("NULL");
119: break;
120: case Query.EMPTY:
121: cb.append("EMPTY");
122: break;
123: }
124: }
125:
126: public String toString() {
127: String str = _value.toString();
128:
129: if (_isNot)
130: str += " IS NOT ";
131: else
132: str += " IS ";
133:
134: switch (_op) {
135: case Query.NULL:
136: return str + "NULL";
137: case Query.EMPTY:
138: return str + "EMPTY";
139: default:
140: return super.toString();
141: }
142: }
143: }
|