KickJava   Java API By Example, From Geeks To Geeks.

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


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.metadata.ClassDescriptor;
22 import org.apache.ojb.broker.metadata.FieldDescriptor;
23 import org.apache.ojb.broker.util.logging.Logger;
24
25 /**
26  * Model an INSERT Statement
27  *
28  * @author <a HREF="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
29  * @version $Id: SqlInsertStatement.java,v 1.7.2.2 2005/12/21 22:23:43 tomdz Exp $
30  */

31 public class SqlInsertStatement extends SqlPkStatement
32 {
33     private String JavaDoc sql;
34
35     /**
36      * Constructor for SqlInsertStatement.
37      *
38      * @param cld
39      * @param logger
40      */

41     public SqlInsertStatement(ClassDescriptor cld, Logger logger)
42     {
43         super(cld, logger);
44     }
45
46     /** @see SqlStatement#getStatement() */
47     public String JavaDoc getStatement()
48     {
49         if(sql == null)
50         {
51             StringBuffer JavaDoc stmt = new StringBuffer JavaDoc(1024);
52             ClassDescriptor cld = getClassDescriptor();
53
54             stmt.append("INSERT INTO ");
55             appendTable(cld, stmt);
56             stmt.append(" (");
57             appendListOfColumns(cld, stmt);
58             stmt.append(")");
59             appendListOfValues(cld, stmt);
60
61             sql = stmt.toString();
62         }
63         return sql;
64     }
65
66     private List JavaDoc appendListOfColumns(ClassDescriptor cld, StringBuffer JavaDoc buf)
67     {
68         FieldDescriptor[] fields = cld.getAllRwFields();
69
70         ArrayList JavaDoc columnList = new ArrayList JavaDoc();
71
72         for(int i = 0; i < fields.length; i++)
73         {
74             if(i > 0)
75             {
76                 buf.append(",");
77             }
78             buf.append(fields[i].getColumnName());
79             columnList.add(fields[i].getAttributeName());
80         }
81         return columnList;
82     }
83
84     /**
85      * generates a values(?,) for a prepared insert statement.
86      * returns null if there are no fields
87      * @param stmt the StringBuffer
88      */

89     private void appendListOfValues(ClassDescriptor cld, StringBuffer JavaDoc stmt)
90     {
91         FieldDescriptor[] fields = cld.getAllRwFields();
92
93         if(fields.length == 0)
94         {
95             return;
96         }
97
98         stmt.append(" VALUES (");
99         for(int i = 0; i < fields.length; i++)
100         {
101             stmt.append("?");
102             if(i < fields.length - 1)
103             {
104                 stmt.append(",");
105             }
106         }
107         stmt.append(") ");
108     }
109
110 }
111
112
Popular Tags