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 java.lang.ref.WeakReference;
019    
020    import org.apache.ojb.broker.OJBRuntimeException;
021    import org.apache.ojb.broker.PersistenceBrokerException;
022    import org.apache.ojb.broker.metadata.ClassDescriptor;
023    import org.apache.ojb.broker.metadata.FieldDescriptor;
024    import org.apache.ojb.broker.util.logging.Logger;
025    
026    /**
027     * Model simple Statements based on ClassDescriptor and/or PrimaryKey
028     *
029     * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
030     * @version $Id: SqlPkStatement.java,v 1.1 2007-08-24 22:17:39 ewestfal Exp $
031     */
032    public abstract class SqlPkStatement implements SqlStatement
033    {
034        // arminw: Use weak reference to allow GC of removed metadata instances
035        private WeakReference m_classDescriptor;
036        private Logger m_logger;
037    
038        /** Constructor for SqlPkStatement. */
039        public SqlPkStatement(ClassDescriptor aCld, Logger aLogger)
040        {
041            super();
042            m_classDescriptor = new WeakReference(aCld);
043            m_logger = aLogger;
044        }
045    
046        /** append table name */
047        protected void appendTable(ClassDescriptor cld, StringBuffer stmt)
048        {
049            stmt.append(cld.getFullTableName());
050        }
051    
052        /**
053         * Returns the logger.
054         *
055         * @return Logger
056         */
057        protected Logger getLogger()
058        {
059            return m_logger;
060        }
061    
062        /**
063         * Returns the classDescriptor.
064         *
065         * @return ClassDescriptor
066         */
067        protected ClassDescriptor getClassDescriptor()
068        {
069            ClassDescriptor cld = (ClassDescriptor) m_classDescriptor.get();
070            if(cld == null)
071            {
072                throw new OJBRuntimeException("Requested ClassDescriptor instance was already GC by JVM");
073            }
074            return cld;
075        }
076    
077        /**
078         * Generate a sql where-clause for the array of fields
079         *
080         * @param fields array containing all columns used in WHERE clause
081         */
082        protected void appendWhereClause(FieldDescriptor[] fields, StringBuffer stmt) throws PersistenceBrokerException
083        {
084            stmt.append(" WHERE ");
085    
086            for(int i = 0; i < fields.length; i++)
087            {
088                FieldDescriptor fmd = fields[i];
089    
090                stmt.append(fmd.getColumnName());
091                stmt.append(" = ? ");
092                if(i < fields.length - 1)
093                {
094                    stmt.append(" AND ");
095                }
096            }
097        }
098    
099        /**
100         * Generate a where clause for a prepared Statement.
101         * Only primary key and locking fields are used in this where clause
102         *
103         * @param cld the ClassDescriptor
104         * @param useLocking true if locking fields should be included
105         * @param stmt the StatementBuffer
106         */
107        protected void appendWhereClause(ClassDescriptor cld, boolean useLocking, StringBuffer stmt)
108        {
109            FieldDescriptor[] pkFields = cld.getPkFields();
110            FieldDescriptor[] fields;
111    
112            fields = pkFields;
113            if(useLocking)
114            {
115                FieldDescriptor[] lockingFields = cld.getLockingFields();
116                if(lockingFields.length > 0)
117                {
118                    fields = new FieldDescriptor[pkFields.length + lockingFields.length];
119                    System.arraycopy(pkFields, 0, fields, 0, pkFields.length);
120                    System.arraycopy(lockingFields, 0, fields, pkFields.length, lockingFields.length);
121                }
122            }
123    
124            appendWhereClause(fields, stmt);
125        }
126    
127    }