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 org.jboss.deployment.DeploymentException;
025: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
026: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
027: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
028: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQlQueryMetaData;
029: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
030: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData;
031:
032: /**
033: * This class generates a query from EJB-QL.
034: *
035: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
036: * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
037: * @version $Revision: 61754 $
038: */
039: public final class JDBCEJBQLQuery extends JDBCAbstractQueryCommand {
040:
041: public JDBCEJBQLQuery(JDBCStoreManager manager, JDBCQueryMetaData q)
042: throws DeploymentException {
043: super (manager, q);
044:
045: JDBCQlQueryMetaData metadata = (JDBCQlQueryMetaData) q;
046: if (getLog().isDebugEnabled()) {
047: getLog().debug("EJB-QL: " + metadata.getEjbQl());
048: }
049:
050: QLCompiler compiler = JDBCQueryManager.getInstance(metadata
051: .getQLCompilerClass(), manager.getCatalog());
052:
053: try {
054: compiler.compileEJBQL(metadata.getEjbQl(), metadata
055: .getMethod().getReturnType(), metadata.getMethod()
056: .getParameterTypes(), metadata);
057: } catch (Throwable t) {
058: log.error(t.getMessage(), t);
059: throw new DeploymentException(
060: "Error compiling EJB-QL statement for EJB '"
061: + manager.getContainer().getBeanMetaData()
062: .getEjbName() + "': "
063: + metadata.getEjbQl(), t);
064: }
065:
066: setSQL(compiler.getSQL());
067:
068: // set select object
069: if (compiler.isSelectEntity()) {
070: JDBCEntityBridge selectEntity = (JDBCEntityBridge) compiler
071: .getSelectEntity();
072:
073: // set the select entity
074: setSelectEntity(selectEntity);
075:
076: // set the preload fields
077: JDBCReadAheadMetaData readahead = metadata.getReadAhead();
078: if (readahead.isOnFind()) {
079: setEagerLoadGroup(readahead.getEagerLoadGroup());
080: setOnFindCMRList(compiler.getLeftJoinCMRList());
081:
082: // exclude non-searchable columns if distinct is used
083: if (compiler.isSelectDistinct()) {
084: boolean[] mask = getEagerLoadMask();
085: JDBCFieldBridge[] tableFields = selectEntity
086: .getTableFields();
087: for (int i = 0; i < tableFields.length; ++i) {
088: if (mask[i]
089: && !tableFields[i].getJDBCType()
090: .isSearchable()) {
091: mask[i] = false;
092: }
093: }
094: }
095: }
096: } else if (compiler.isSelectField()) {
097: setSelectField((JDBCCMPFieldBridge) compiler
098: .getSelectField());
099: } else {
100: setSelectFunction(compiler.getSelectFunction(), manager);
101: }
102:
103: // get the parameter order
104: setParameterList(compiler.getInputParameters());
105: }
106: }
|