KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > accesslayer > sql > SqlMNStatement


1 package org.apache.ojb.broker.accesslayer.sql;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.ojb.broker.util.logging.Logger;
22
23 /**
24  * Model a MN-Statement based on Table, Columns and Values
25  *
26  * @author <a HREF="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
27  * @version $Id: SqlMNStatement.java,v 1.8.2.1 2005/12/21 22:23:44 tomdz Exp $
28  */

29 public abstract class SqlMNStatement implements SqlStatement
30 {
31     private String JavaDoc m_table;
32     private String JavaDoc[] m_columns;
33     private Logger m_logger;
34
35
36     /**
37      * Constructor for SqlMNStatement.
38      */

39     public SqlMNStatement(String JavaDoc table, String JavaDoc[] columns, Logger logger)
40     {
41         super();
42         this.m_table = table;
43         this.m_columns = columns;
44         this.m_logger = logger;
45     }
46
47     /**
48      * append table name
49      */

50     protected void appendTable(String JavaDoc table, StringBuffer JavaDoc stmt)
51     {
52         stmt.append(table);
53     }
54
55     /**
56      * Returns the columns.
57      * @return String[]
58      */

59     protected String JavaDoc[] getColumns()
60     {
61         return m_columns;
62     }
63
64     /**
65      * Returns the table.
66      * @return String
67      */

68     protected String JavaDoc getTable()
69     {
70         return m_table;
71     }
72
73     /**
74      * Returns the logger.
75      * @return Logger
76      */

77     protected Logger getLogger()
78     {
79         return m_logger;
80     }
81
82     /**
83      * Generate a sql where-clause matching the contraints defined by the array of fields
84      *
85      * @param columns array containing all columns used in WHERE clause
86      */

87     protected void appendWhereClause(StringBuffer JavaDoc stmt, Object JavaDoc[] columns)
88     {
89         stmt.append(" WHERE ");
90
91         for (int i = 0; i < columns.length; i++)
92         {
93             if (i > 0)
94             {
95                 stmt.append(" AND ");
96             }
97             stmt.append(columns[i]);
98             stmt.append("=?");
99         }
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 JavaDoc appendListOfColumns(String JavaDoc[] columns, StringBuffer JavaDoc stmt)
109     {
110         ArrayList JavaDoc columnList = new ArrayList JavaDoc();
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 }
127
Popular Tags