01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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 org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
25: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
26: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
27: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData;
28: import org.jboss.deployment.DeploymentException;
29:
30: /**
31: * JDBCFindByQuery automatic finder used in CMP 1.x.
32: *
33: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
34: * @author <a href="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
35: * @author <a href="mailto:marc.fleury@telkel.com">Marc Fleury</a>
36: * @author <a href="mailto:shevlandj@kpi.com.au">Joe Shevland</a>
37: * @author <a href="mailto:justin@j-m-f.demon.co.uk">Justin Forder</a>
38: * @author <a href="mailto:danch@nvisia.com">danch (Dan Christopherson)</a>
39: * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
40: * @version $Revision: 57209 $
41: */
42: public final class JDBCFindByQuery extends JDBCAbstractQueryCommand {
43: // The meta-info for the field we are finding by
44: private final JDBCCMPFieldBridge cmpField;
45:
46: public JDBCFindByQuery(JDBCStoreManager manager, JDBCQueryMetaData q)
47: throws DeploymentException {
48:
49: super (manager, q);
50:
51: JDBCEntityBridge entity = (JDBCEntityBridge) manager
52: .getEntityBridge();
53:
54: String finderName = q.getMethod().getName();
55:
56: // finder name will be like findByFieldName
57: // we need to convert it to fieldName.
58: String cmpFieldName = Character.toLowerCase(finderName
59: .charAt(6))
60: + finderName.substring(7);
61:
62: // get the field
63: cmpField = entity.getCMPFieldByName(cmpFieldName);
64: if (cmpField == null) {
65: throw new IllegalArgumentException(
66: "No finder for this method: " + finderName);
67: }
68:
69: // set the preload fields
70: JDBCReadAheadMetaData readAhead = q.getReadAhead();
71: if (readAhead.isOnFind()) {
72: setEagerLoadGroup(readAhead.getEagerLoadGroup());
73: }
74:
75: // generate the sql
76: StringBuffer sql = new StringBuffer(300);
77: sql.append(SQLUtil.SELECT);
78:
79: SQLUtil.getColumnNamesClause(entity.getPrimaryKeyFields(), sql);
80: if (getEagerLoadGroup() != null) {
81: SQLUtil.appendColumnNamesClause(entity,
82: getEagerLoadGroup(), sql);
83: }
84: sql.append(SQLUtil.FROM).append(entity.getQualifiedTableName())
85: .append(SQLUtil.WHERE);
86: SQLUtil.getWhereClause(cmpField, sql);
87:
88: setSQL(sql.toString());
89: setParameterList(QueryParameter.createParameters(0, cmpField));
90: }
91: }
|