KickJava   Java API By Example, From Geeks To Geeks.

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


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

42 public class SQLUpdateAllStatementForOracleAnonymousBlock extends SQLModifyStatement {
43     protected HashMap JavaDoc tables_databaseFieldsToValues;
44     protected HashMap JavaDoc tablesToPrimaryKeyFields;
45     protected SQLCall selectCall;
46     
47     protected static final String JavaDoc varSuffix = "_VAR";
48     protected static final String JavaDoc typeSuffix = "_TYPE";
49     protected static final String JavaDoc tab = " ";
50     protected static final String JavaDoc dbltab = tab + tab;
51     protected static final String JavaDoc trpltab = dbltab + tab;
52     
53     public void setSelectCall(SQLCall selectCall) {
54         this.selectCall = selectCall;
55     }
56     public SQLCall getSelectCall() {
57         return selectCall;
58     }
59     public void setTablesToPrimaryKeyFields(HashMap JavaDoc tablesToPrimaryKeyFields) {
60         this.tablesToPrimaryKeyFields = tablesToPrimaryKeyFields;
61     }
62     public HashMap JavaDoc getTablesToPrimaryKeyFields() {
63         return tablesToPrimaryKeyFields;
64     }
65     public void setTables_databaseFieldsToValues(HashMap JavaDoc tables_databaseFieldsToValues) {
66         this.tables_databaseFieldsToValues = tables_databaseFieldsToValues;
67     }
68     public HashMap JavaDoc getTables_databaseFieldsToValues() {
69         return tables_databaseFieldsToValues;
70     }
71
72     /**
73      * Append the string containing the SQL insert string for the given table.
74      */

75     public DatabaseCall buildCall(AbstractSession session) {
76         SQLCall call = new SQLCall();
77         call.returnNothing();
78         
79         Writer writer = new CharArrayWriter(100);
80         
81         Vector JavaDoc mainPrimaryKeys = new Vector JavaDoc();
82         mainPrimaryKeys.addAll((Collection JavaDoc)tablesToPrimaryKeyFields.get(table));
83         
84         Vector JavaDoc allFields = (Vector JavaDoc)mainPrimaryKeys.clone();
85         Iterator JavaDoc itDatabaseFieldsToValues = tables_databaseFieldsToValues.values().iterator();
86         while(itDatabaseFieldsToValues.hasNext()) {
87             Iterator JavaDoc itDatabaseFields = ((HashMap JavaDoc)itDatabaseFieldsToValues.next()).keySet().iterator();
88             while(itDatabaseFields.hasNext()) {
89                 allFields.addElement(itDatabaseFields.next());
90             }
91         }
92         
93         try {
94             //DECLARE
95
writer.write("DECLARE\n");
96             
97             for(int i=0; i < allFields.size(); i++) {
98                 writeDeclareTypeAndVar(writer, (DatabaseField)allFields.elementAt(i));
99             }
100
101             //BEGIN
102
writer.write("BEGIN\n");
103
104             // select t0.emp_id, concat('Even', t0.f_name), t1.salary + 1000 BULK COLLECT into EMPLOYEEE_EMP_ID_VAR, EMPLOYEEE_F_NAME_VAR, SALARY_SALARY_VAR from employee t0, salary t1 where t0.l_name like 'updateEmployeeTestUsingTempTable' and t0.f_name in ('0', '2') and t1.salary = 0 and t0.emp_id = t1.emp_id;
105
String JavaDoc selectStr = selectCall.getSQLString();
106             int index = selectStr.toUpperCase().indexOf(" FROM ");
107             String JavaDoc firstPart = selectStr.substring(0, index);
108             String JavaDoc secondPart = selectStr.substring(index, selectStr.length());
109             
110             writer.write(tab);
111             writer.write(firstPart);
112             writer.write(" BULK COLLECT INTO ");
113
114             for(int i=0; i < allFields.size(); i++) {
115                 writeVar(writer, (DatabaseField)allFields.elementAt(i));
116                 if(i < allFields.size() - 1) {
117                     writer.write(", ");
118                 }
119             }
120             writer.write(secondPart);
121             writer.write(";\n");
122
123             call.getParameters().addAll(selectCall.getParameters());
124             call.getParameterTypes().addAll(selectCall.getParameterTypes());
125
126             DatabaseField firstMainPrimaryKey = (DatabaseField)mainPrimaryKeys.firstElement();
127             writer.write(tab);
128             writer.write("IF ");
129             writeVar(writer, firstMainPrimaryKey);
130             writer.write(".COUNT > 0 THEN\n");
131             
132             Iterator JavaDoc itEntries = tables_databaseFieldsToValues.entrySet().iterator();
133             while(itEntries.hasNext()) {
134                 writeForAll(writer, firstMainPrimaryKey);
135                 writer.write(trpltab);
136                 writer.write("UPDATE ");
137                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)itEntries.next();
138                 DatabaseTable t = (DatabaseTable)entry.getKey();
139                 writer.write(t.getQualifiedName());
140                 writer.write(" SET ");
141                 HashMap JavaDoc databaseFieldsToValues = (HashMap JavaDoc)entry.getValue();
142                 int counter = 0;
143                 Iterator JavaDoc itDatabaseFields = databaseFieldsToValues.keySet().iterator();
144                 while(itDatabaseFields.hasNext()) {
145                     counter++;
146                     DatabaseField field = (DatabaseField)itDatabaseFields.next();
147                     writer.write(field.getName());
148                     writer.write(" = ");
149                     writeVar(writer, field);
150                     writer.write("(i)");
151                     if(counter < databaseFieldsToValues.size()) {
152                         writer.write(", ");
153                     }
154                 }
155                 
156                 writer.write(" WHERE ");
157                 
158                 Vector JavaDoc tablePrimaryKeys = new Vector JavaDoc();
159                 tablePrimaryKeys.addAll((Collection JavaDoc)tablesToPrimaryKeyFields.get(t));
160                 for(int i=0; i < mainPrimaryKeys.size(); i++) {
161                     DatabaseField tableField = (DatabaseField)tablePrimaryKeys.elementAt(i);
162                     writer.write(tableField.getName());
163                     writer.write(" = ");
164                     DatabaseField mainField = (DatabaseField )mainPrimaryKeys.elementAt(i);
165                     writeVar(writer, mainField);
166                     writer.write("(i)");
167                     if(i < mainPrimaryKeys.size()-1) {
168                         writer.write(" AND ");
169                     } else {
170                         writer.write(";\n");
171                     }
172                 }
173             }
174
175             writer.write(tab);
176             writer.write("END IF;\n");
177             
178             writer.write(tab);
179             DatabaseField outField = new DatabaseField("ROW_COUNT");
180             outField.setType(Integer JavaDoc.class);
181             call.appendOut(writer, outField);
182             writer.write(" := ");
183             writeVar(writer, firstMainPrimaryKey);
184             writer.write(".COUNT;\n");
185             
186             writer.write("END;");
187
188             call.setSQLString(writer.toString());
189             
190         } catch (IOException exception) {
191             throw ValidationException.fileError(exception);
192         }
193                 
194         return call;
195     }
196     
197     protected static void writeUniqueFieldName(Writer writer, DatabaseField field) throws IOException {
198         // EMPLOYEE_EMP_ID
199
writer.write(field.getTableName());
200         writer.write("_");
201         writer.write(field.getName());
202     }
203     
204     protected static void writeType(Writer writer, DatabaseField field) throws IOException {
205         // EMPLOYEE_EMP_ID_TYPE
206
writeUniqueFieldName(writer, field);
207         writer.write(typeSuffix);
208     }
209     
210     protected static void writeVar(Writer writer, DatabaseField field) throws IOException {
211         // EMPLOYEE_EMP_ID_VAR
212
writeUniqueFieldName(writer, field);
213         writer.write(varSuffix);
214     }
215     
216     protected static void writeDeclareTypeAndVar(Writer writer, DatabaseField field) throws IOException {
217         // TYPE EMPLOYEE_EMP_ID_TYPE IS TABLE OF EMPLOYEE.EMP_ID%TYPE;
218
writer.write(tab);
219         writer.write("TYPE ");
220         writeType(writer, field);
221         writer.write(" IS TABLE OF ");
222         writer.write(field.getQualifiedName());
223         writer.write("%TYPE;\n");
224         
225         // EMPLOYEE_EMP_ID_VAR EMP_ID_TYPE;
226
writer.write(tab);
227         writeVar(writer, field);
228         writer.write(" ");
229         writeType(writer, field);
230         writer.write(";\n");
231     }
232
233     protected static void writeForAll(Writer writer, DatabaseField field) throws IOException {
234         //FORALL i IN EMPLOYEE_EMP_ID_VAR.FIRST..EMPLOYEE_EMP_ID_VAR.LAST
235
writer.write(dbltab);
236         writer.write("FORALL i IN ");
237         writeVar(writer, field);
238         writer.write(".FIRST..");
239         writeVar(writer, field);
240         writer.write(".LAST\n");
241     }
242 }
243
Popular Tags