KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > expressions > SQLUpdateStatement


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.expressions;
23
24 import java.io.*;
25 import java.util.*;
26 import oracle.toplink.essentials.exceptions.*;
27 import oracle.toplink.essentials.internal.helper.*;
28 import oracle.toplink.essentials.queryframework.*;
29 import oracle.toplink.essentials.internal.sessions.AbstractSession;
30
31 /**
32  * <p><b>Purpose</b>: Print UPDATE statement.
33  * <p><b>Responsibilities</b>:<ul>
34  * <li> Print UPDATE statement.
35  * </ul>
36  * @author Dorin Sandu
37  * @since TOPLink/Java 1.0
38  */

39 public class SQLUpdateStatement extends SQLModifyStatement {
40
41     /**
42      * Append the string containing the SQL insert string for the given table.
43      */

44     protected SQLCall buildCallWithoutReturning(AbstractSession session) {
45         SQLCall call = new SQLCall();
46         call.returnNothing();
47
48         Writer writer = new CharArrayWriter(100);
49         try {
50             writer.write("UPDATE ");
51             writer.write(getTable().getQualifiedName());
52             writer.write(" SET ");
53
54             Vector fieldsForTable = new Vector();
55             for (Enumeration fieldsEnum = getModifyRow().keys(); fieldsEnum.hasMoreElements();) {
56                 DatabaseField field = (DatabaseField)fieldsEnum.nextElement();
57                 if (field.getTable().equals(getTable()) || (!field.hasTableName())) {
58                     fieldsForTable.addElement(field);
59                 }
60             }
61
62             if (fieldsForTable.isEmpty()) {
63                 return null;
64             }
65
66             for (int i = 0; i < fieldsForTable.size(); i++) {
67                 DatabaseField field = (DatabaseField)fieldsForTable.elementAt(i);
68                 writer.write(field.getName());
69                 writer.write(" = ");
70                 call.appendModify(writer, field);
71
72                 if ((i + 1) < fieldsForTable.size()) {
73                     writer.write(", ");
74                 }
75             }
76
77             if (!(getWhereClause() == null)) {
78                 writer.write(" WHERE ");
79                 ExpressionSQLPrinter printer = new ExpressionSQLPrinter(session, getTranslationRow(), call, false);
80                 printer.setWriter(writer);
81                 printer.printExpression(getWhereClause());
82             }
83
84             call.setSQLString(writer.toString());
85             return call;
86         } catch (IOException exception) {
87             throw ValidationException.fileError(exception);
88         }
89     }
90 }
91
Popular Tags