KickJava   Java API By Example, From Geeks To Geeks.

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


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 oracle.toplink.essentials.exceptions.*;
28 import oracle.toplink.essentials.expressions.*;
29 import oracle.toplink.essentials.queryframework.*;
30 import oracle.toplink.essentials.internal.sessions.AbstractSession;
31 import java.util.Collection JavaDoc;
32 import oracle.toplink.essentials.internal.databaseaccess.DatabaseCall;
33 import oracle.toplink.essentials.internal.helper.DatabaseField;
34
35 /**
36  * @author Guy Pelletier
37  * @since TOPLink/Java 1.0
38  */

39 public class SQLUpdateAllStatement extends SQLModifyStatement {
40     protected HashMap JavaDoc m_updateClauses;
41     protected HashMap JavaDoc databaseFieldsToTableAliases;
42
43     protected SQLCall selectCallForExist;
44     protected String JavaDoc tableAliasInSelectCallForExist;
45     protected Collection JavaDoc primaryKeyFields;
46     
47     public void setSelectCallForExist(SQLCall selectCallForExist) {
48         this.selectCallForExist = selectCallForExist;
49     }
50     public SQLCall getSelectCallForExist() {
51         return selectCallForExist;
52     }
53     public void setTableAliasInSelectCallForExist(String JavaDoc tableAliasInSelectCallForExist) {
54         this.tableAliasInSelectCallForExist = tableAliasInSelectCallForExist;
55     }
56     public String JavaDoc getTableAliasInSelectCallForExist() {
57         return tableAliasInSelectCallForExist;
58     }
59     public void setPrimaryKeyFieldsForAutoJoin(Collection JavaDoc primaryKeyFields) {
60         this.primaryKeyFields = primaryKeyFields;
61     }
62     public Collection JavaDoc getPrimaryKeyFieldsForAutoJoin() {
63         return primaryKeyFields;
64     }
65     public void setUpdateClauses(HashMap JavaDoc updateClauses) {
66         m_updateClauses = updateClauses;
67     }
68     public HashMap JavaDoc getUpdateClauses() {
69         return m_updateClauses;
70     }
71     public void setDatabaseFieldsToTableAliases(HashMap JavaDoc databaseFieldsToTableAliases) {
72         this.databaseFieldsToTableAliases = databaseFieldsToTableAliases;
73     }
74     public HashMap JavaDoc getDatabaseFieldsToTableAliases() {
75         return databaseFieldsToTableAliases;
76     }
77     
78     /**
79      * Append the string containing the SQL insert string for the given table.
80      */

81     public DatabaseCall buildCall(AbstractSession session) {
82         SQLCall call = buildSimple(session);
83         if(selectCallForExist == null) {
84             return call;
85         }
86         Writer writer = new CharArrayWriter(100);
87         try {
88             writer.write(call.getSQLString());
89
90             if(selectCallForExist != null) {
91                 writer.write(" WHERE EXISTS(");
92                 // EXIST Example: selectCall.sqlString:
93
// "SELECT t0.EMP_ID FROM EMPLOYEE t0, SALARY t1 WHERE (((t0.F_NAME LIKE 'a') AND (t1.SALARY = 0)) AND (t1.EMP_ID = t0.EMP_ID))"
94
writeSelect(writer, selectCallForExist, tableAliasInSelectCallForExist, call);
95                 // closing bracket for EXISTS
96
writer.write(")");
97             }
98
99             call.setSQLString(writer.toString());
100             
101         } catch (IOException exception) {
102             throw ValidationException.fileError(exception);
103         }
104                 
105         return call;
106     }
107     
108     protected SQLCall buildSimple(AbstractSession session) {
109         SQLCall call = new SQLCall();
110         call.returnNothing();
111         Writer writer = new CharArrayWriter(100);
112         ExpressionSQLPrinter printer = new ExpressionSQLPrinter(session, getTranslationRow(), call, false);
113         printer.setWriter(writer);
114
115         try {
116             // UPDATE //
117
writer.write("UPDATE ");
118             writer.write(getTable().getQualifiedName());
119             // SET CLAUSE //
120
writer.write(" SET ");
121
122             Iterator JavaDoc i = m_updateClauses.keySet().iterator();
123             boolean commaNeeded = false;
124
125             while (i.hasNext()) {
126                 if (commaNeeded) {
127                     writer.write(", ");
128                 }
129
130                 DatabaseField field = (DatabaseField)i.next();
131                 Object JavaDoc value = m_updateClauses.get(field);
132
133                 writer.write(field.getName());
134                 writer.write(" = ");
135                 if(value instanceof Expression) {
136                     printer.printExpression((Expression)value);
137                 } else {
138                     // must be SQLCall
139
SQLCall selCall = (SQLCall)value;
140                     String JavaDoc tableAlias = (String JavaDoc)getDatabaseFieldsToTableAliases().get(field);
141                     // should be SQLCall select
142
writer.write("(");
143                     writeSelect(writer, selCall, tableAlias, call);
144                     writer.write(")");
145                 }
146
147                 commaNeeded = true;
148             }
149
150             // WHERE CLAUSE //
151
if (getWhereClause() != null) {
152                 writer.write(" WHERE ");
153                 printer.printExpression(getWhereClause());
154             }
155
156             call.setSQLString(writer.toString());
157             return call;
158         } catch (IOException exception) {
159             throw ValidationException.fileError(exception);
160         }
161     }
162
163     protected void writeSelect(Writer writer, SQLCall selectCall, String JavaDoc tableAliasInSelectCall, SQLCall call) throws IOException {
164         writer.write(selectCall.getSQLString());
165
166         // Auto join
167
// Example: AND t0.EMP_ID = EMP_ID
168
Iterator JavaDoc it = getPrimaryKeyFieldsForAutoJoin().iterator();
169         while(it.hasNext()) {
170             writer.write(" AND ");
171             String JavaDoc fieldName = ((DatabaseField)it.next()).getName();
172             if(tableAliasInSelectCall != null) {
173                 writer.write(tableAliasInSelectCall);
174                 writer.write('.');
175             }
176             writer.write(fieldName);
177             writer.write(" = ");
178             writer.write(table.getQualifiedName());
179             writer.write('.');
180             writer.write(fieldName);
181         }
182         
183         call.getParameters().addAll(selectCall.getParameters());
184         call.getParameterTypes().addAll(selectCall.getParameterTypes());
185     }
186 }
187
Popular Tags