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.util.logging.Logger;
021    
022    /**
023     * Model an UPDATE Statement
024     *
025     * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
026     * @version $Id: SqlUpdateStatement.java,v 1.1 2007-08-24 22:17:39 ewestfal Exp $
027     */
028    public class SqlUpdateStatement extends SqlPkStatement
029    {
030        protected String sql;
031    
032        /**
033         * Constructor for SqlUpdateStatement.
034         *
035         * @param cld
036         * @param logger
037         */
038        public SqlUpdateStatement(ClassDescriptor cld, Logger logger)
039        {
040            super(cld, logger);
041        }
042    
043        /**
044         * generates a SET-phrase for a prepared update statement.
045         *
046         * @param stmt the StringBuffer
047         */
048        private void appendSetClause(ClassDescriptor cld, StringBuffer stmt)
049        {
050            FieldDescriptor[] fields = cld.getNonPkRwFields();
051    
052            if(fields.length == 0)
053            {
054                return;
055            }
056    
057            stmt.append(" SET ");
058            for(int i = 0; i < fields.length; i++)
059            {
060                stmt.append(fields[i].getColumnName());
061                stmt.append("=?");
062                if(i < fields.length - 1)
063                {
064                    stmt.append(",");
065                }
066            }
067        }
068    
069        /**
070         * @see SqlStatement#getStatement()
071         */
072        public String getStatement()
073        {
074            if(sql == null)
075            {
076                StringBuffer stmt = new StringBuffer(1024);
077                ClassDescriptor cld = getClassDescriptor();
078    
079                stmt.append("UPDATE ");
080                appendTable(cld, stmt);
081                appendSetClause(cld, stmt);
082                appendWhereClause(cld, true, stmt); //use Locking
083    
084                sql = stmt.toString();
085            }
086            return sql;
087        }
088    
089    }