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.8.2.1 2005/12/21 22:23:44 tomdz Exp $
028: */
029: public abstract class SqlMNStatement implements SqlStatement {
030: private String m_table;
031: private String[] m_columns;
032: private Logger m_logger;
033:
034: /**
035: * Constructor for SqlMNStatement.
036: */
037: public SqlMNStatement(String table, String[] columns, Logger logger) {
038: super ();
039: this .m_table = table;
040: this .m_columns = columns;
041: this .m_logger = logger;
042: }
043:
044: /**
045: * append table name
046: */
047: protected void appendTable(String table, StringBuffer stmt) {
048: stmt.append(table);
049: }
050:
051: /**
052: * Returns the columns.
053: * @return String[]
054: */
055: protected String[] getColumns() {
056: return m_columns;
057: }
058:
059: /**
060: * Returns the table.
061: * @return String
062: */
063: protected String getTable() {
064: return m_table;
065: }
066:
067: /**
068: * Returns the logger.
069: * @return Logger
070: */
071: protected Logger getLogger() {
072: return m_logger;
073: }
074:
075: /**
076: * Generate a sql where-clause matching the contraints defined by the array of fields
077: *
078: * @param columns array containing all columns used in WHERE clause
079: */
080: protected void appendWhereClause(StringBuffer stmt, Object[] columns) {
081: stmt.append(" WHERE ");
082:
083: for (int i = 0; i < columns.length; i++) {
084: if (i > 0) {
085: stmt.append(" AND ");
086: }
087: stmt.append(columns[i]);
088: stmt.append("=?");
089: }
090: }
091:
092: /**
093: * Appends to the statement a comma separated list of column names.
094: *
095: * @param columns defines the columns to be selected (for reports)
096: * @return list of column names
097: */
098: protected List appendListOfColumns(String[] columns,
099: StringBuffer stmt) {
100: ArrayList columnList = new ArrayList();
101:
102: for (int i = 0; i < columns.length; i++) {
103: if (i > 0) {
104: stmt.append(",");
105: }
106: stmt.append(columns[i]);
107: columnList.add(columns[i]);
108: }
109: return columnList;
110:
111: }
112:
113: }
|