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.metadata.JDBCJBossQLQueryMetaData;
028: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
029: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData;
030:
031: /**
032: * This class generates a query from JBoss-QL.
033: *
034: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
035: * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
036: * @version $Revision: 61754 $
037: */
038: public final class JDBCJBossQLQuery extends JDBCAbstractQueryCommand {
039:
040: public JDBCJBossQLQuery(JDBCStoreManager manager,
041: JDBCQueryMetaData q) throws DeploymentException {
042: super (manager, q);
043:
044: JDBCJBossQLQueryMetaData metadata = (JDBCJBossQLQueryMetaData) q;
045: if (getLog().isDebugEnabled()) {
046: getLog().debug("JBossQL: " + metadata.getJBossQL());
047: }
048:
049: QLCompiler compiler = JDBCQueryManager.getInstance(metadata
050: .getQLCompilerClass(), manager.getCatalog());
051:
052: try {
053: compiler.compileJBossQL(metadata.getJBossQL(), metadata
054: .getMethod().getReturnType(), metadata.getMethod()
055: .getParameterTypes(), metadata);
056: } catch (Throwable t) {
057: t.printStackTrace();
058: throw new DeploymentException("Error compiling JBossQL "
059: + "statement '" + metadata.getJBossQL() + "'", t);
060: }
061:
062: setSQL(compiler.getSQL());
063: setOffsetParam(compiler.getOffsetParam());
064: setOffsetValue(compiler.getOffsetValue());
065: setLimitParam(compiler.getLimitParam());
066: setLimitValue(compiler.getLimitValue());
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: JDBCCMPFieldBridge[] tableFields = (JDBCCMPFieldBridge[]) 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(),
101: (JDBCStoreManager) compiler.getStoreManager());
102: }
103:
104: // get the parameter order
105: setParameterList(compiler.getInputParameters());
106: }
107: }
|