KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
26 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
27 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
28 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData;
29 import org.jboss.deployment.DeploymentException;
30
31 /**
32  * JDBCFindByQuery automatic finder used in CMP 1.x.
33  *
34  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
35  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
36  * @author <a HREF="mailto:marc.fleury@telkel.com">Marc Fleury</a>
37  * @author <a HREF="mailto:shevlandj@kpi.com.au">Joe Shevland</a>
38  * @author <a HREF="mailto:justin@j-m-f.demon.co.uk">Justin Forder</a>
39  * @author <a HREF="mailto:danch@nvisia.com">danch (Dan Christopherson)</a>
40  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
41  * @version $Revision: 43506 $
42  */

43 public final class JDBCFindByQuery extends JDBCAbstractQueryCommand
44 {
45    // The meta-info for the field we are finding by
46
private final JDBCCMPFieldBridge cmpField;
47
48    public JDBCFindByQuery(JDBCStoreManager manager, JDBCQueryMetaData q)
49       throws DeploymentException
50    {
51
52       super(manager, q);
53
54       JDBCEntityBridge entity = (JDBCEntityBridge) manager.getEntityBridge();
55
56       String JavaDoc finderName = q.getMethod().getName();
57
58       // finder name will be like findByFieldName
59
// we need to convert it to fieldName.
60
String JavaDoc cmpFieldName = Character.toLowerCase(finderName.charAt(6)) + finderName.substring(7);
61
62       // get the field
63
cmpField = entity.getCMPFieldByName(cmpFieldName);
64       if(cmpField == null)
65       {
66          throw new IllegalArgumentException JavaDoc(
67             "No finder for this method: " + finderName);
68       }
69
70       // set the preload fields
71
JDBCReadAheadMetaData readAhead = q.getReadAhead();
72       if(readAhead.isOnFind())
73       {
74          setEagerLoadGroup(readAhead.getEagerLoadGroup());
75       }
76
77       // generate the sql
78
StringBuffer JavaDoc sql = new StringBuffer JavaDoc(300);
79       sql.append(SQLUtil.SELECT);
80
81       SQLUtil.getColumnNamesClause(entity.getPrimaryKeyFields(), sql);
82       if(getEagerLoadGroup() != null)
83       {
84          SQLUtil.appendColumnNamesClause(entity, getEagerLoadGroup(), sql);
85       }
86       sql.append(SQLUtil.FROM)
87          .append(entity.getQualifiedTableName())
88          .append(SQLUtil.WHERE);
89       SQLUtil.getWhereClause(cmpField, sql);
90
91       setSQL(sql.toString());
92       setParameterList(QueryParameter.createParameters(0, cmpField));
93    }
94 }
95
Popular Tags