001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.internal.expressions;
038:
039: import java.io.*;
040: import java.util.*;
041: import oracle.toplink.essentials.exceptions.*;
042: import oracle.toplink.essentials.internal.helper.*;
043: import oracle.toplink.essentials.queryframework.*;
044: import oracle.toplink.essentials.internal.sessions.AbstractSession;
045:
046: /**
047: * <p><b>Purpose</b>: Print INSERT statement.
048: * <p><b>Responsibilities</b>:<ul>
049: * <li> Print INSERT statement.
050: * </ul>
051: * @author Dorin Sandu
052: * @since TOPLink/Java 1.0
053: */
054: public class SQLInsertStatement extends SQLModifyStatement {
055:
056: /**
057: * Append the string containing the SQL insert string for the given table.
058: */
059: protected SQLCall buildCallWithoutReturning(AbstractSession session) {
060: SQLCall call = new SQLCall();
061: call.returnNothing();
062:
063: Writer writer = new CharArrayWriter(200);
064: try {
065: writer.write("INSERT ");
066: writer.write("INTO ");
067: writer.write(getTable().getQualifiedName());
068: writer.write(" (");
069:
070: Vector fieldsForTable = new Vector();
071: for (Enumeration fieldsEnum = getModifyRow().keys(); fieldsEnum
072: .hasMoreElements();) {
073: DatabaseField field = (DatabaseField) fieldsEnum
074: .nextElement();
075: if (field.getTable().equals(getTable())
076: || (!field.hasTableName())) {
077: fieldsForTable.addElement(field);
078: }
079: }
080:
081: if (fieldsForTable.isEmpty()) {
082: throw QueryException.objectToInsertIsEmpty(getTable());
083: }
084:
085: for (int i = 0; i < fieldsForTable.size(); i++) {
086: writer.write(((DatabaseField) fieldsForTable
087: .elementAt(i)).getName());
088: if ((i + 1) < fieldsForTable.size()) {
089: writer.write(", ");
090: }
091: }
092: writer.write(") VALUES (");
093:
094: for (int i = 0; i < fieldsForTable.size(); i++) {
095: DatabaseField field = (DatabaseField) fieldsForTable
096: .elementAt(i);
097: call.appendModify(writer, field);
098: if ((i + 1) < fieldsForTable.size()) {
099: writer.write(", ");
100: }
101: }
102: writer.write(")");
103:
104: call.setSQLString(writer.toString());
105: } catch (IOException exception) {
106: throw ValidationException.fileError(exception);
107: }
108: return call;
109: }
110: }
|