KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCDynamicQLQuery


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Collections JavaDoc;
28 import javax.ejb.FinderException JavaDoc;
29
30 import org.jboss.deployment.DeploymentException;
31 import org.jboss.ejb.EntityEnterpriseContext;
32 import org.jboss.ejb.GenericEntityObjectFactory;
33 import org.jboss.ejb.EntityContainer;
34 import org.jboss.ejb.plugins.cmp.ejbql.Catalog;
35 import org.jboss.ejb.plugins.cmp.ejbql.SelectFunction;
36 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
37 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
38 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
39 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
40 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCDynamicQLQueryMetaData;
41 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData;
42
43 /**
44  * This class generates a query from JBoss-QL.
45  *
46  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
47  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
48  * @version $Revision: 58402 $
49  */

50 public final class JDBCDynamicQLQuery extends JDBCAbstractQueryCommand
51 {
52    private final Catalog catalog;
53    private final JDBCDynamicQLQueryMetaData metadata;
54
55    public JDBCDynamicQLQuery(JDBCStoreManager manager, JDBCQueryMetaData q)
56       throws DeploymentException
57    {
58       super(manager, q);
59       catalog = manager.getCatalog();
60       metadata = (JDBCDynamicQLQueryMetaData)q;
61    }
62
63    public Collection JavaDoc execute(Method JavaDoc finderMethod, Object JavaDoc[] args, EntityEnterpriseContext ctx, GenericEntityObjectFactory factory)
64       throws FinderException JavaDoc
65    {
66       String JavaDoc dynamicQL = (String JavaDoc)args[0];
67       if(getLog().isDebugEnabled())
68       {
69          getLog().debug("DYNAMIC-QL: " + dynamicQL);
70       }
71
72       QLCompiler compiler = null;
73       try
74       {
75          compiler = JDBCQueryManager.getInstance(metadata.getQLCompilerClass(), catalog);
76       }
77       catch(DeploymentException e)
78       {
79          throw new FinderException JavaDoc(e.getMessage());
80       }
81
82       // get the parameters
83
Object JavaDoc[] parameters = (Object JavaDoc[])args[1];
84       // parameter types
85
Class JavaDoc[] parameterTypes;
86       if(parameters == null)
87       {
88          parameterTypes = new Class JavaDoc[0];
89       }
90       else
91       {
92          // get the parameter types
93
parameterTypes = new Class JavaDoc[parameters.length];
94          for(int i = 0; i < parameters.length; i++)
95          {
96             if(parameters[i] == null)
97             {
98                throw new FinderException JavaDoc("Parameter[" + i + "] is null");
99             }
100             parameterTypes[i] = parameters[i].getClass();
101          }
102       }
103
104       // compile the dynamic-ql
105
try
106       {
107          compiler.compileJBossQL(
108             dynamicQL,
109             finderMethod.getReturnType(),
110             parameterTypes,
111             metadata);
112       }
113       catch(Throwable JavaDoc t)
114       {
115          t.printStackTrace();
116          throw new FinderException JavaDoc("Error compiling ejbql: " + t);
117       }
118
119       int offset = toInt(parameters, compiler.getOffsetParam(), compiler.getOffsetValue());
120       int limit = toInt(parameters, compiler.getLimitParam(), compiler.getLimitValue());
121
122       JDBCEntityBridge selectEntity = null;
123       JDBCCMPFieldBridge selectField = null;
124       SelectFunction selectFunction = null;
125       if(compiler.isSelectEntity())
126       {
127          selectEntity = (JDBCEntityBridge) compiler.getSelectEntity();
128       }
129       else if(compiler.isSelectField())
130       {
131          selectField = (JDBCCMPFieldBridge) compiler.getSelectField();
132       }
133       else
134       {
135          selectFunction = compiler.getSelectFunction();
136       }
137
138       boolean[] mask;
139       List JavaDoc leftJoinCMRList;
140       JDBCReadAheadMetaData readahead = metadata.getReadAhead();
141       if(selectEntity != null && readahead.isOnFind())
142       {
143          mask = selectEntity.getLoadGroupMask(readahead.getEagerLoadGroup());
144          leftJoinCMRList = compiler.getLeftJoinCMRList();
145
146          // exclude non-searchable columns if distinct is used
147
if(compiler.isSelectDistinct())
148          {
149             JDBCFieldBridge[] tableFields = selectEntity.getTableFields();
150             for(int i = 0; i < tableFields.length; ++i)
151             {
152                if(mask[i] && !tableFields[i].getJDBCType().isSearchable())
153                {
154                   mask[i] = false;
155                }
156             }
157          }
158       }
159       else
160       {
161          mask = null;
162          leftJoinCMRList = Collections.EMPTY_LIST;
163       }
164
165       // get the parameter order
166
setParameterList(compiler.getInputParameters());
167
168       EntityContainer con = ((JDBCStoreManager)compiler.getStoreManager()).getContainer();
169       factory = metadata.isResultTypeMappingLocal() ? (GenericEntityObjectFactory)con.getLocalProxyFactory() : con.getProxyFactory();
170
171       return execute(
172          compiler.getSQL(),
173          parameters,
174          offset,
175          limit,
176          selectEntity,
177          selectField,
178          selectFunction,
179          (JDBCStoreManager) compiler.getStoreManager(),
180          mask,
181          compiler.getInputParameters(),
182          leftJoinCMRList,
183          metadata,
184          factory,
185          log
186       );
187    }
188 }
189
Popular Tags