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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.ql;
031:
032: import com.caucho.ejb.cfg21.EjbEntityBean;
033: import java.lang.reflect.*;
034: import javax.ejb.*;
035:
036: import com.caucho.amber.type.EntityType;
037: import com.caucho.config.ConfigException;
038: import com.caucho.ejb.cfg.*;
039: import com.caucho.util.CharBuffer;
040:
041: /**
042: * Expression representing a select method argument.
043: */
044: class ArgExpr extends Expr {
045: // method index
046: private int _index;
047:
048: private Class _coerceType;
049:
050: /**
051: * Creates a new argument expression.
052: *
053: * @param index the method index
054: */
055: ArgExpr(Query query, int index) throws ConfigException {
056: _query = query;
057: _index = index;
058:
059: evalTypes();
060: }
061:
062: /**
063: * Returns the method index.
064: */
065: int getIndex() {
066: return _query.getArgIndex(_index);
067: }
068:
069: /**
070: * Evaluates the types for the expression
071: */
072: void evalTypes() throws ConfigException {
073: if (getJavaType() != null)
074: return;
075:
076: ApiMethod method = _query.getMethod();
077:
078: Class[] args = method.getParameterTypes();
079:
080: if (args.length <= _index - 1)
081: throw error(L.l("`{0}' exceeds number of arguments", "?"
082: + _index));
083:
084: setJavaType(args[_index - 1]);
085:
086: _query.setArgSize(_index, getComponentCount());
087: }
088:
089: void setCoerceType(Class javaType) {
090: _coerceType = javaType;
091: }
092:
093: /**
094: * Only args can be coerced.
095: */
096: boolean canCoerce() {
097: return true;
098: }
099:
100: int getComponentCount() {
101: Class javaType = getJavaType();
102:
103: if (javaType.isPrimitive() || javaType.isArray()
104: || javaType.getName().startsWith("java."))
105: return 1;
106:
107: if (EJBLocalObject.class.isAssignableFrom(javaType)) {
108: EjbEntityBean bean = _query.getConfig().findEntityByLocal(
109: javaType);
110:
111: EntityType type = bean.getEntityType();
112:
113: return type.getId().getKeys().size();
114: }
115:
116: Field[] fields = javaType.getFields();
117:
118: return fields.length;
119: }
120:
121: /**
122: * Prints the where SQL for this expression
123: *
124: * @param gen the java code generator
125: */
126: void generateWhere(CharBuffer cb) {
127: cb.append("?" + getIndex());
128: }
129:
130: void generateComponent(CharBuffer cb, int index) {
131: cb.append("?" + (getIndex() + index));
132: }
133:
134: /**
135: * Printable version of the object.
136: */
137: public String toString() {
138: return "?" + _index;
139: }
140: }
|