KickJava   Java API By Example, From Geeks To Geeks.

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


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.Vector JavaDoc;
26 import java.util.Collection JavaDoc;
27
28 import oracle.toplink.essentials.exceptions.*;
29 import oracle.toplink.essentials.queryframework.*;
30 import oracle.toplink.essentials.internal.sessions.AbstractSession;
31 import oracle.toplink.essentials.internal.databaseaccess.DatabaseCall;
32
33 /**
34  * @author Andrei Ilitchev
35  * @since TOPLink/Java 1.0
36  */

37 public abstract class SQLModifyAllStatementForTempTable extends SQLModifyStatement {
38     public static final int CREATE_TEMP_TABLE = 0;
39     public static final int INSERT_INTO_TEMP_TABLE = 1;
40     public static final int UPDATE_ORIGINAL_TABLE = 2;
41     public static final int CLEANUP_TEMP_TABLE = 3;
42     
43     protected Collection JavaDoc allFields;
44     protected Collection JavaDoc primaryKeyFields;
45     protected SQLCall selectCall;
46     protected int mode;
47     
48     abstract protected Collection JavaDoc getUsedFields();
49     abstract protected void writeUpdateOriginalTable(AbstractSession session, Writer writer) throws IOException;
50
51     public void setAllFields(Collection JavaDoc allFields) {
52         this.allFields = allFields;
53     }
54     public Collection JavaDoc getAllFields() {
55         return allFields;
56     }
57     public void setSelectCall(SQLCall selectCall) {
58         this.selectCall = selectCall;
59     }
60     public SQLCall getSelectCall() {
61         return selectCall;
62     }
63     public void setPrimaryKeyFields(Collection JavaDoc primaryKeyFields) {
64         this.primaryKeyFields = primaryKeyFields;
65     }
66     public Collection JavaDoc getPrimaryKeyFields() {
67         return primaryKeyFields;
68     }
69     public void setMode(int mode) {
70         this.mode = mode;
71     }
72     public int getMode() {
73         return mode;
74     }
75
76     /**
77      * Append the string containing the SQL insert string for the given table.
78      */

79     public DatabaseCall buildCall(AbstractSession session) {
80         SQLCall call = new SQLCall();
81         call.returnNothing();
82         
83         Writer writer = new CharArrayWriter(100);
84         
85         try {
86             if(mode == CREATE_TEMP_TABLE) {
87                 session.getPlatform().writeCreateTempTableSql(writer, table, session,
88                                                 new Vector JavaDoc(getPrimaryKeyFields()),
89                                                 getUsedFields(),
90                                                 new Vector JavaDoc(getAllFields()));
91             } else if(mode == INSERT_INTO_TEMP_TABLE) {
92                 session.getPlatform().writeInsertIntoTableSql(writer, table, getUsedFields());
93
94                 call.getParameters().addAll(selectCall.getParameters());
95                 call.getParameterTypes().addAll(selectCall.getParameterTypes());
96                 
97                 String JavaDoc selectStr = selectCall.getSQLString();
98                 writer.write(selectStr);
99                 
100             } else if(mode == UPDATE_ORIGINAL_TABLE) {
101                 writeUpdateOriginalTable(session, writer);
102             } else if(mode == CLEANUP_TEMP_TABLE) {
103                 session.getPlatform().writeCleanUpTempTableSql(writer, table);
104             } else {
105                 // should never happen
106
}
107
108             call.setSQLString(writer.toString());
109             
110         } catch (IOException exception) {
111             throw ValidationException.fileError(exception);
112         }
113                 
114         return call;
115     }
116 }
117
Popular Tags