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: * A unary expression
036: */
037: class UnaryExpr extends Expr {
038: // unary operation
039: private int _op;
040: // main expression
041: private Expr _expr;
042:
043: /**
044: * Creates a unary expression.
045: *
046: * @param op the operation
047: * @param expr the expression
048: */
049: UnaryExpr(int op, Expr expr) throws ConfigException {
050: _op = op;
051: _expr = expr;
052:
053: evalTypes();
054: }
055:
056: /**
057: * Evaluates the types for the expression
058: */
059: void evalTypes() throws ConfigException {
060: if (getJavaType() != null)
061: return;
062:
063: switch (_op) {
064: case '+':
065: if (!_expr.isNumeric())
066: throw error(L.l(
067: "'+' expects numeric expression at '{0}'",
068: _expr));
069: setJavaType(_expr.getJavaType());
070: break;
071: case '-':
072: if (!_expr.isNumeric())
073: throw error(L.l(
074: "'-' expects numeric expression at '{0}'",
075: _expr));
076: setJavaType(_expr.getJavaType());
077: break;
078: case Query.NOT:
079: if (!_expr.isBoolean())
080: throw error(L.l(
081: "NOT expects boolean expression at '{0}'",
082: _expr));
083: setJavaType(boolean.class);
084: break;
085:
086: default:
087: throw new RuntimeException();
088: }
089: }
090:
091: /**
092: * Prints the where SQL for this expression
093: *
094: * @param gen the java code generator
095: */
096: void generateWhere(CharBuffer cb) {
097: switch (_op) {
098: case '+':
099: cb.append("+");
100: break;
101: case '-':
102: cb.append("-");
103: break;
104: case Query.NOT:
105: cb.append("NOT ");
106: break;
107: }
108:
109: _expr.generateWhereSubExpr(cb);
110: }
111: }
|