001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc;
023:
024: import java.lang.reflect.Method;
025: import java.util.Collection;
026: import java.util.List;
027: import java.util.Collections;
028: import javax.ejb.FinderException;
029:
030: import org.jboss.deployment.DeploymentException;
031: import org.jboss.ejb.EntityEnterpriseContext;
032: import org.jboss.ejb.GenericEntityObjectFactory;
033: import org.jboss.ejb.EntityContainer;
034: import org.jboss.ejb.plugins.cmp.ejbql.Catalog;
035: import org.jboss.ejb.plugins.cmp.ejbql.SelectFunction;
036: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
037: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
038: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
039: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
040: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCDynamicQLQueryMetaData;
041: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData;
042:
043: /**
044: * This class generates a query from JBoss-QL.
045: *
046: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
047: * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
048: * @version $Revision: 61754 $
049: */
050: public final class JDBCDynamicQLQuery extends JDBCAbstractQueryCommand {
051: private final Catalog catalog;
052: private final JDBCDynamicQLQueryMetaData metadata;
053:
054: public JDBCDynamicQLQuery(JDBCStoreManager manager,
055: JDBCQueryMetaData q) throws DeploymentException {
056: super (manager, q);
057: catalog = manager.getCatalog();
058: metadata = (JDBCDynamicQLQueryMetaData) q;
059: }
060:
061: public Collection execute(Method finderMethod, Object[] args,
062: EntityEnterpriseContext ctx,
063: GenericEntityObjectFactory factory) throws FinderException {
064: String dynamicQL = (String) args[0];
065: if (getLog().isDebugEnabled()) {
066: getLog().debug("DYNAMIC-QL: " + dynamicQL);
067: }
068:
069: QLCompiler compiler = null;
070: try {
071: compiler = JDBCQueryManager.getInstance(metadata
072: .getQLCompilerClass(), catalog);
073: } catch (DeploymentException e) {
074: throw new FinderException(e.getMessage());
075: }
076:
077: // get the parameters
078: Object[] parameters = (Object[]) args[1];
079: // parameter types
080: Class[] parameterTypes;
081: if (parameters == null) {
082: parameterTypes = new Class[0];
083: } else {
084: // get the parameter types
085: parameterTypes = new Class[parameters.length];
086: for (int i = 0; i < parameters.length; i++) {
087: if (parameters[i] == null) {
088: throw new FinderException("Parameter[" + i
089: + "] is null");
090: }
091: parameterTypes[i] = parameters[i].getClass();
092: }
093: }
094:
095: // compile the dynamic-ql
096: try {
097: compiler.compileJBossQL(dynamicQL, finderMethod
098: .getReturnType(), parameterTypes, metadata);
099: } catch (Throwable t) {
100: t.printStackTrace();
101: throw new FinderException("Error compiling ejbql: " + t);
102: }
103:
104: int offset = toInt(parameters, compiler.getOffsetParam(),
105: compiler.getOffsetValue());
106: int limit = toInt(parameters, compiler.getLimitParam(),
107: compiler.getLimitValue());
108:
109: JDBCEntityBridge selectEntity = null;
110: JDBCCMPFieldBridge selectField = null;
111: SelectFunction selectFunction = null;
112: if (compiler.isSelectEntity()) {
113: selectEntity = (JDBCEntityBridge) compiler
114: .getSelectEntity();
115: } else if (compiler.isSelectField()) {
116: selectField = (JDBCCMPFieldBridge) compiler
117: .getSelectField();
118: } else {
119: selectFunction = compiler.getSelectFunction();
120: }
121:
122: boolean[] mask;
123: List leftJoinCMRList;
124: JDBCReadAheadMetaData readahead = metadata.getReadAhead();
125: if (selectEntity != null && readahead.isOnFind()) {
126: mask = selectEntity.getLoadGroupMask(readahead
127: .getEagerLoadGroup());
128: leftJoinCMRList = compiler.getLeftJoinCMRList();
129:
130: // exclude non-searchable columns if distinct is used
131: if (compiler.isSelectDistinct()) {
132: JDBCFieldBridge[] tableFields = selectEntity
133: .getTableFields();
134: for (int i = 0; i < tableFields.length; ++i) {
135: if (mask[i]
136: && !tableFields[i].getJDBCType()
137: .isSearchable()) {
138: mask[i] = false;
139: }
140: }
141: }
142: } else {
143: mask = null;
144: leftJoinCMRList = Collections.EMPTY_LIST;
145: }
146:
147: // get the parameter order
148: setParameterList(compiler.getInputParameters());
149:
150: EntityContainer con = ((JDBCStoreManager) compiler
151: .getStoreManager()).getContainer();
152: factory = metadata.isResultTypeMappingLocal() ? (GenericEntityObjectFactory) con
153: .getLocalProxyFactory()
154: : con.getProxyFactory();
155:
156: return execute(compiler.getSQL(), parameters, offset, limit,
157: selectEntity, selectField, selectFunction,
158: (JDBCStoreManager) compiler.getStoreManager(), mask,
159: compiler.getInputParameters(), leftJoinCMRList,
160: metadata, factory, log);
161: }
162: }
|