01: package org.apache.ojb.broker.accesslayer.sql;
02:
03: /* Copyright 2002-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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.6.2.2 2005/12/21 22:23:44 tomdz Exp $
27: */
28: public class SqlUpdateStatement extends SqlPkStatement {
29: protected String sql;
30:
31: /**
32: * Constructor for SqlUpdateStatement.
33: *
34: * @param cld
35: * @param logger
36: */
37: public SqlUpdateStatement(ClassDescriptor cld, Logger logger) {
38: super (cld, logger);
39: }
40:
41: /**
42: * generates a SET-phrase for a prepared update statement.
43: *
44: * @param stmt the StringBuffer
45: */
46: private void appendSetClause(ClassDescriptor cld, StringBuffer stmt) {
47: FieldDescriptor[] fields = cld.getNonPkRwFields();
48:
49: if (fields.length == 0) {
50: return;
51: }
52:
53: stmt.append(" SET ");
54: for (int i = 0; i < fields.length; i++) {
55: stmt.append(fields[i].getColumnName());
56: stmt.append("=?");
57: if (i < fields.length - 1) {
58: stmt.append(",");
59: }
60: }
61: }
62:
63: /**
64: * @see SqlStatement#getStatement()
65: */
66: public String getStatement() {
67: if (sql == null) {
68: StringBuffer stmt = new StringBuffer(1024);
69: ClassDescriptor cld = getClassDescriptor();
70:
71: stmt.append("UPDATE ");
72: appendTable(cld, stmt);
73: appendSetClause(cld, stmt);
74: appendWhereClause(cld, true, stmt); //use Locking
75:
76: sql = stmt.toString();
77: }
78: return sql;
79: }
80:
81: }
|