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.util.ArrayList;
019    import java.util.List;
020    
021    import org.apache.ojb.broker.util.logging.Logger;
022    
023    /**
024     * Model a MN-Statement based on Table, Columns and Values
025     *
026     * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
027     * @version $Id: SqlMNStatement.java,v 1.1 2007-08-24 22:17:39 ewestfal Exp $
028     */
029    public abstract class SqlMNStatement implements SqlStatement
030    {
031        private String m_table;
032        private String[] m_columns;
033        private Logger m_logger;
034    
035    
036        /**
037         * Constructor for SqlMNStatement.
038         */
039        public SqlMNStatement(String table, String[] columns, Logger logger)
040        {
041            super();
042            this.m_table = table;
043            this.m_columns = columns;
044            this.m_logger = logger;
045        }
046    
047        /**
048         * append table name
049         */
050        protected void appendTable(String table, StringBuffer stmt)
051        {
052            stmt.append(table);
053        }
054    
055        /**
056         * Returns the columns.
057         * @return String[]
058         */
059        protected String[] getColumns()
060        {
061            return m_columns;
062        }
063    
064        /**
065         * Returns the table.
066         * @return String
067         */
068        protected String getTable()
069        {
070            return m_table;
071        }
072    
073        /**
074         * Returns the logger.
075         * @return Logger
076         */
077        protected Logger getLogger()
078        {
079            return m_logger;
080        }
081    
082        /**
083         * Generate a sql where-clause matching the contraints defined by the array of fields
084         *
085         * @param columns array containing all columns used in WHERE clause
086         */
087        protected void appendWhereClause(StringBuffer stmt, Object[] columns)
088        {
089            stmt.append(" WHERE ");
090    
091            for (int i = 0; i < columns.length; i++)
092            {
093                if (i > 0)
094                {
095                    stmt.append(" AND ");
096                }
097                stmt.append(columns[i]);
098                stmt.append("=?");
099            }
100        }
101    
102        /**
103        * Appends to the statement a comma separated list of column names.
104        *
105        * @param columns defines the columns to be selected (for reports)
106        * @return list of column names
107        */
108        protected List appendListOfColumns(String[] columns, StringBuffer stmt)
109        {
110            ArrayList columnList = new ArrayList();
111    
112            for (int i = 0; i < columns.length; i++)
113            {
114                if (i > 0)
115                {
116                    stmt.append(",");
117                }
118                stmt.append(columns[i]);
119                columnList.add(columns[i]);
120            }
121            return columnList;
122    
123        }
124    
125    
126    }