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 org.apache.ojb.broker.metadata.ClassDescriptor;
19 import org.apache.ojb.broker.metadata.FieldDescriptor;
20 import org.apache.ojb.broker.util.logging.Logger;
21
22 /**
23 * Model an UPDATE Statement
24 *
25 * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
26 * @version $Id: SqlUpdateStatement.java,v 1.1 2007-08-24 22:17:39 ewestfal Exp $
27 */
28 public class SqlUpdateStatement extends SqlPkStatement
29 {
30 protected String sql;
31
32 /**
33 * Constructor for SqlUpdateStatement.
34 *
35 * @param cld
36 * @param logger
37 */
38 public SqlUpdateStatement(ClassDescriptor cld, Logger logger)
39 {
40 super(cld, logger);
41 }
42
43 /**
44 * generates a SET-phrase for a prepared update statement.
45 *
46 * @param stmt the StringBuffer
47 */
48 private void appendSetClause(ClassDescriptor cld, StringBuffer stmt)
49 {
50 FieldDescriptor[] fields = cld.getNonPkRwFields();
51
52 if(fields.length == 0)
53 {
54 return;
55 }
56
57 stmt.append(" SET ");
58 for(int i = 0; i < fields.length; i++)
59 {
60 stmt.append(fields[i].getColumnName());
61 stmt.append("=?");
62 if(i < fields.length - 1)
63 {
64 stmt.append(",");
65 }
66 }
67 }
68
69 /**
70 * @see SqlStatement#getStatement()
71 */
72 public String getStatement()
73 {
74 if(sql == null)
75 {
76 StringBuffer stmt = new StringBuffer(1024);
77 ClassDescriptor cld = getClassDescriptor();
78
79 stmt.append("UPDATE ");
80 appendTable(cld, stmt);
81 appendSetClause(cld, stmt);
82 appendWhereClause(cld, true, stmt); //use Locking
83
84 sql = stmt.toString();
85 }
86 return sql;
87 }
88
89 }