KickJava   Java API By Example, From Geeks To Geeks.

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


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.expressions.Expression;
28 import oracle.toplink.essentials.queryframework.*;
29 import oracle.toplink.essentials.internal.databaseaccess.DatabaseCall;
30 import oracle.toplink.essentials.internal.helper.*;
31 import oracle.toplink.essentials.internal.sessions.AbstractSession;
32
33 /**
34  * <p><b>Purpose</b>: Print DELETE statement with non trivial WHERE clause
35  * <p><b>Responsibilities</b>:<ul>
36  * <li> Print DELETE statement.
37  * </ul>
38  * @author Andrei Ilitchev
39  * @since TOPLink 10.1.3
40  */

41 public class SQLDeleteAllStatement extends SQLDeleteStatement {
42
43     protected Expression inheritanceExpression;
44
45     protected SQLCall selectCallForExist;
46     protected String JavaDoc tableAliasInSelectCallForExist;
47
48     protected SQLCall selectCallForNotExist;
49     protected String JavaDoc tableAliasInSelectCallForNotExist;
50     
51     // A pair of Vectors for join expression
52
protected Vector aliasedFields;
53     protected Vector originalFields;
54     
55     public void setSelectCallForExist(SQLCall selectCallForExist) {
56         this.selectCallForExist = selectCallForExist;
57     }
58     public SQLCall getSelectCallForExist() {
59         return selectCallForExist;
60     }
61     public void setSelectCallForNotExist(SQLCall selectCallForNotExist) {
62         this.selectCallForNotExist = selectCallForNotExist;
63     }
64     public SQLCall getSelectCallForNotExist() {
65         return selectCallForNotExist;
66     }
67     public void setTableAliasInSelectCallForExist(String JavaDoc tableAliasInSelectCallForExist) {
68         this.tableAliasInSelectCallForExist = tableAliasInSelectCallForExist;
69     }
70     public String JavaDoc getTableAliasInSelectCallForExist() {
71         return tableAliasInSelectCallForExist;
72     }
73     public void setTableAliasInSelectCallForNotExist(String JavaDoc tableAliasInSelectCallForNotExist) {
74         this.tableAliasInSelectCallForNotExist = tableAliasInSelectCallForNotExist;
75     }
76     public String JavaDoc getTableAliasInSelectCallForNotExist() {
77         return tableAliasInSelectCallForNotExist;
78     }
79     public void setPrimaryKeyFieldsForAutoJoin(Collection primaryKeyFields) {
80         if(primaryKeyFields instanceof Vector) {
81             setOriginalFieldsForJoin((Vector)primaryKeyFields);
82         } else {
83             setOriginalFieldsForJoin(new Vector(primaryKeyFields));
84         }
85         setAliasedFieldsForJoin((Vector)getOriginalFieldsForJoin().clone());
86     }
87     public void setOriginalFieldsForJoin(Vector originalFields) {
88         this.originalFields = originalFields;
89     }
90     public Vector getOriginalFieldsForJoin() {
91         return originalFields;
92     }
93     public void setAliasedFieldsForJoin(Vector aliasedFields) {
94         this.aliasedFields = aliasedFields;
95     }
96     public Vector getAliasedFieldsForExpression() {
97         return aliasedFields;
98     }
99     public void setInheritanceExpression(Expression inheritanceExpression) {
100         this.inheritanceExpression = inheritanceExpression;
101     }
102     public Expression getInheritanceExpression() {
103         return inheritanceExpression;
104     }
105
106     /**
107      * Append the string containing the SQL insert string for the given table.
108      */

109     public DatabaseCall buildCall(AbstractSession session) {
110         SQLCall call = (SQLCall)super.buildCall(session);
111
112         Writer writer = new CharArrayWriter(100);
113         try {
114             // because where clause is null,
115
// call.sqlString == "DELETE FROM getTable().getQualifiedName()"
116
writer.write(call.getSQLString());
117
118             boolean whereWasPrinted = true;
119             if(selectCallForExist != null) {
120                 writer.write(" WHERE EXISTS(");
121                 // EXIST Example: selectCall.sqlString:
122
// "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))"
123
writeSelect(writer, selectCallForExist, tableAliasInSelectCallForExist, call);
124                 // closing bracket for EXISTS
125
writer.write(")");
126             } else if (inheritanceExpression != null) {
127                 writer.write(" WHERE ");
128                 // Example: (PROJ_TYPE = 'L')
129
ExpressionSQLPrinter printer = new ExpressionSQLPrinter(session, getTranslationRow(), call, false);
130                 printer.setWriter(writer);
131                 printer.printExpression(inheritanceExpression);
132             } else {
133                 whereWasPrinted = false;
134             }
135
136             if(selectCallForNotExist != null) {
137                 if(whereWasPrinted) {
138                     writer.write(" AND");
139                 } else {
140                     writer.write(" WHERE");
141                 }
142                 writer.write(" NOT EXISTS(");
143                 // NOT EXIST Example: selectCall.sqlString:
144
// "SELECT t0.EMP_ID FROM EMPLOYEE t0, SALARY t1 WHERE (t1.EMP_ID = t0.EMP_ID)"
145
writeSelect(writer, selectCallForNotExist, tableAliasInSelectCallForNotExist, call);
146                 // closing bracket for EXISTS
147
writer.write(")");
148             }
149
150             call.setSQLString(writer.toString());
151             
152         } catch (IOException exception) {
153             throw ValidationException.fileError(exception);
154         }
155                 
156         return call;
157     }
158     
159     protected void writeSelect(Writer writer, SQLCall selectCall, String JavaDoc tableAliasInSelectCall, SQLCall call) throws IOException {
160         writer.write(selectCall.getSQLString());
161
162         // join
163
// Example: AND t0.EMP_ID = EMP_ID
164
for(int i=0; i < originalFields.size(); i++) {
165             writer.write(" AND ");
166             if(tableAliasInSelectCall != null) {
167                 writer.write(tableAliasInSelectCall);
168                 writer.write('.');
169             }
170             writer.write(((DatabaseField)aliasedFields.elementAt(i)).getName());
171             writer.write(" = ");
172             writer.write(table.getQualifiedName());
173             writer.write('.');
174             writer.write(((DatabaseField)originalFields.elementAt(i)).getName());
175         }
176         
177         call.getParameters().addAll(selectCall.getParameters());
178         call.getParameterTypes().addAll(selectCall.getParameterTypes());
179     }
180 }
181
Popular Tags