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.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
025: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
026: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCAbstractCMPFieldBridge;
027: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
028: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData;
029: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCTypeMappingMetaData;
030: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCFunctionMappingMetaData;
031: import org.jboss.ejb.EntityEnterpriseContext;
032: import org.jboss.ejb.GenericEntityObjectFactory;
033: import org.jboss.deployment.DeploymentException;
034:
035: import javax.ejb.FinderException;
036: import java.util.Collection;
037: import java.util.Collections;
038: import java.util.List;
039: import java.lang.reflect.Method;
040:
041: /**
042: * JDBCBeanExistsCommand is a JDBC query that checks if an id exists
043: * in the database. This is used by the create and findByPrimaryKey
044: * code.
045: *
046: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
047: * @author <a href="mailto:marc.fleury@telkel.com">Marc Fleury</a>
048: * @author <a href="mailto:justin@j-m-f.demon.co.uk">Justin Forder</a>
049: * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
050: * @version $Revision: 57209 $
051: */
052: public final class JDBCFindByPrimaryKeyQuery extends
053: JDBCAbstractQueryCommand {
054: private JDBCStoreManager manager;
055: private boolean rowLocking;
056:
057: public JDBCFindByPrimaryKeyQuery(JDBCStoreManager manager,
058: JDBCQueryMetaData q) throws DeploymentException {
059: super (manager, q);
060: this .manager = manager;
061: rowLocking = manager.getMetaData().hasRowLocking();
062:
063: JDBCEntityBridge entity = (JDBCEntityBridge) manager
064: .getEntityBridge();
065:
066: JDBCTypeMappingMetaData typeMapping = this .manager
067: .getJDBCTypeFactory().getTypeMapping();
068: AliasManager aliasManager = new AliasManager(typeMapping
069: .getAliasHeaderPrefix(), typeMapping
070: .getAliasHeaderSuffix(), typeMapping
071: .getAliasMaxLength());
072:
073: String alias = aliasManager.getAlias(entity.getEntityName());
074:
075: StringBuffer select = new StringBuffer(200);
076: SQLUtil.getColumnNamesClause(entity.getPrimaryKeyFields(),
077: alias, select);
078:
079: StringBuffer from = new StringBuffer();
080: from.append(entity.getQualifiedTableName()).append(' ').append(
081: alias);
082:
083: // set the preload fields
084: JDBCReadAheadMetaData readAhead = q.getReadAhead();
085: if (readAhead.isOnFind()) {
086: setEagerLoadGroup(readAhead.getEagerLoadGroup());
087: if (getEagerLoadMask() != null) {
088: SQLUtil.appendColumnNamesClause(
089: entity.getTableFields(), getEagerLoadMask(),
090: alias, select);
091:
092: List onFindCMRList = JDBCAbstractQueryCommand
093: .getLeftJoinCMRNodes(entity, entity
094: .getQualifiedTableName(), readAhead
095: .getLeftJoins(), null);
096:
097: if (!onFindCMRList.isEmpty()) {
098: setOnFindCMRList(onFindCMRList);
099: JDBCAbstractQueryCommand.leftJoinCMRNodes(alias,
100: onFindCMRList, aliasManager, from);
101: JDBCAbstractQueryCommand
102: .appendLeftJoinCMRColumnNames(
103: onFindCMRList, aliasManager, select);
104: }
105: }
106: }
107:
108: StringBuffer where = new StringBuffer();
109: SQLUtil.getWhereClause(entity.getPrimaryKeyFields(), alias,
110: where);
111:
112: // generate the sql
113: StringBuffer sql = new StringBuffer(300);
114: if (rowLocking && readAhead.isOnFind()
115: && getEagerLoadMask() != null) {
116: JDBCFunctionMappingMetaData rowLockingTemplate = typeMapping
117: .getRowLockingTemplate();
118: rowLockingTemplate.getFunctionSql(new Object[] { select,
119: from, where.length() == 0 ? null : where, null // order by
120: }, sql);
121: } else {
122: sql.append(SQLUtil.SELECT).append(select).append(
123: SQLUtil.FROM).append(from).append(SQLUtil.WHERE)
124: .append(where);
125: }
126:
127: setSQL(sql.toString());
128: setParameterList(QueryParameter.createPrimaryKeyParameters(0,
129: entity));
130: }
131:
132: public Collection execute(Method finderMethod, Object[] args,
133: EntityEnterpriseContext ctx,
134: GenericEntityObjectFactory factory) throws FinderException {
135: // Check in readahead cache.
136: if (manager.getReadAheadCache().getPreloadDataMap(args[0],
137: false) != null) {
138: // copy pk [JBAS-1361]
139: Object pk = null;
140: JDBCFieldBridge[] pkFields = manager.getEntityBridge()
141: .getPrimaryKeyFields();
142: for (int i = 0; i < pkFields.length; ++i) {
143: JDBCAbstractCMPFieldBridge pkField = ((JDBCAbstractCMPFieldBridge) pkFields[i]);
144: Object fieldValue = pkField.getPrimaryKeyValue(args[0]);
145: pk = pkField.setPrimaryKeyValue(pk, fieldValue);
146: }
147:
148: final Object ejbObject = factory.getEntityEJBObject(pk);
149: return Collections.singletonList(ejbObject);
150: }
151: return super.execute(finderMethod, args, ctx, factory);
152: }
153: }
|