KickJava   Java API By Example, From Geeks To Geeks.

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


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 INSERT statement.
33  * <p><b>Responsibilities</b>:<ul>
34  * <li> Print INSERT statement.
35  * </ul>
36  * @author Dorin Sandu
37  * @since TOPLink/Java 1.0
38  */

39 public class SQLInsertStatement 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(200);
49         try {
50             writer.write("INSERT ");
51             writer.write("INTO ");
52             writer.write(getTable().getQualifiedName());
53             writer.write(" (");
54
55             Vector fieldsForTable = new Vector();
56             for (Enumeration fieldsEnum = getModifyRow().keys(); fieldsEnum.hasMoreElements();) {
57                 DatabaseField field = (DatabaseField)fieldsEnum.nextElement();
58                 if (field.getTable().equals(getTable()) || (!field.hasTableName())) {
59                     fieldsForTable.addElement(field);
60                 }
61             }
62
63             if (fieldsForTable.isEmpty()) {
64                 throw QueryException.objectToInsertIsEmpty(getTable());
65             }
66
67             for (int i = 0; i < fieldsForTable.size(); i++) {
68                 writer.write(((DatabaseField)fieldsForTable.elementAt(i)).getName());
69                 if ((i + 1) < fieldsForTable.size()) {
70                     writer.write(", ");
71                 }
72             }
73             writer.write(") VALUES (");
74
75             for (int i = 0; i < fieldsForTable.size(); i++) {
76                 DatabaseField field = (DatabaseField)fieldsForTable.elementAt(i);
77                 call.appendModify(writer, field);
78                 if ((i + 1) < fieldsForTable.size()) {
79                     writer.write(", ");
80                 }
81             }
82             writer.write(")");
83
84             call.setSQLString(writer.toString());
85         } catch (IOException exception) {
86             throw ValidationException.fileError(exception);
87         }
88         return call;
89     }
90 }
91
Popular Tags