001    package org.apache.ojb.broker.accesslayer.sql;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.metadata.ClassDescriptor;
019    import org.apache.ojb.broker.metadata.FieldDescriptor;
020    import org.apache.ojb.broker.platforms.Platform;
021    import org.apache.ojb.broker.query.Criteria;
022    import org.apache.ojb.broker.query.Query;
023    import org.apache.ojb.broker.query.QueryByCriteria;
024    import org.apache.ojb.broker.util.logging.Logger;
025    
026    /**
027     * Model a SELECT Statement by Primary Key
028     *
029     * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
030     * @version $Id: SqlSelectByPkStatement.java,v 1.1 2007-08-24 22:17:39 ewestfal Exp $
031     */
032    
033    public class SqlSelectByPkStatement extends SqlSelectStatement
034    {
035        /**
036         * Constructor for SqlSelectByPkStatement.
037         *
038         * @param cld
039         * @param logger
040         */
041        public SqlSelectByPkStatement(Platform pf, ClassDescriptor cld, Logger logger)
042        {
043            super(pf, cld, buildQuery(cld), logger);
044        }
045    
046        /**
047         * Build a Pk-Query base on the ClassDescriptor.
048         *
049         * @param cld
050         * @return a select by PK query
051         */
052        private static Query buildQuery(ClassDescriptor cld)
053        {
054            FieldDescriptor[] pkFields = cld.getPkFields();
055            Criteria crit = new Criteria();
056    
057            for(int i = 0; i < pkFields.length; i++)
058            {
059                crit.addEqualTo(pkFields[i].getAttributeName(), null);
060            }
061            return new QueryByCriteria(cld.getClassOfObject(), crit);
062        }
063    }