KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.helper.*;
27 import oracle.toplink.essentials.queryframework.*;
28 import oracle.toplink.essentials.exceptions.*;
29 import oracle.toplink.essentials.expressions.*;
30 import oracle.toplink.essentials.internal.databaseaccess.*;
31 import oracle.toplink.essentials.internal.sessions.AbstractRecord;
32 import oracle.toplink.essentials.internal.sessions.AbstractSession;
33
34 /**
35  * <p><b>Purpose</b>: Expression SQL printer.
36  * <p><b>Responsibilities</b>:<ul>
37  * <li> Print an expression in SQL format.
38  * <li> Replaces FIELD types with field names from the descriptor.
39  * <li> Replaces PARAMETER types with row or object values.
40  * <li> Calls accessor to print primitive types.
41  * </ul>
42  * @author Dorin Sandu
43  * @since TOPLink/Java 1.0
44  */

45 public class ExpressionSQLPrinter {
46
47     /**
48      * Stores the current session. The session accessor
49      * is used to print all the primitive types.
50      */

51     protected AbstractSession session;
52
53     /**
54      * Stores the call being created.
55      */

56     protected SQLCall call;
57
58     /**
59      * Stores the row. Used to print PARAMETER nodes.
60      */

61     protected AbstractRecord translationRow;
62
63     /**
64      * Indicates whether fully qualified field names
65      * (owner + table) should be used or not.
66      */

67     protected boolean shouldPrintQualifiedNames;
68
69     // What we write on
70
protected Writer writer;
71
72     /** Used for distincts in functions. */
73     protected boolean requiresDistinct;
74
75     // Used in figuring out when to print a comma in the select line
76
protected boolean isFirstElementPrinted;
77
78     public ExpressionSQLPrinter(AbstractSession session, AbstractRecord translationRow, SQLCall call, boolean printQualifiedNames) {
79         this.session = session;
80         this.translationRow = translationRow;
81         this.call = call;
82         this.shouldPrintQualifiedNames = printQualifiedNames;
83         this.requiresDistinct = false;
84         isFirstElementPrinted = false;
85     }
86
87     /**
88      * Return the call.
89      */

90     protected SQLCall getCall() {
91         return call;
92     }
93
94     /**
95      * INTERNAL:
96      * Return the database platform specific information.
97      */

98     public DatabasePlatform getPlatform() {
99         return session.getPlatform();
100     }
101
102     protected AbstractSession getSession() {
103         return session;
104     }
105
106     /**
107      * INTERNAL:
108      * Return the row for translation
109      */

110     protected AbstractRecord getTranslationRow() {
111         return translationRow;
112     }
113
114     public Writer getWriter() {
115         return writer;
116     }
117
118     /**
119      * INTERNAL:
120      * Used in figuring out when to print a comma in the select clause
121      */

122     public boolean isFirstElementPrinted() {
123         return isFirstElementPrinted;
124     }
125
126     public void printExpression(Expression expression) {
127         translateExpression(expression);
128     }
129
130     public void printField(DatabaseField field) {
131         if (field == null) {
132             return;
133         }
134
135         try {
136             // Print the field using either short or long notation i.e. owner + table name.
137
if (shouldPrintQualifiedNames()) {
138                 getWriter().write(field.getQualifiedName());
139             } else {
140                 getWriter().write(field.getName());
141             }
142         } catch (IOException exception) {
143             throw ValidationException.fileError(exception);
144         }
145     }
146
147     public void printParameter(ParameterExpression expression) {
148         try {
149             getCall().appendTranslationParameter(getWriter(), expression, getPlatform());
150
151         } catch (IOException exception) {
152             throw ValidationException.fileError(exception);
153         }
154     }
155
156     public void printParameter(DatabaseField field) {
157         getCall().appendTranslation(getWriter(), field);
158     }
159
160     public void printPrimitive(Object JavaDoc value) {
161         if (value instanceof Vector) {
162             printValuelist((Vector)value);
163             return;
164         }
165
166         session.getPlatform().appendLiteralToCall(getCall(), getWriter(), value);
167     }
168
169     public void printString(String JavaDoc value) {
170         try {
171             getWriter().write(value);
172
173         } catch (IOException exception) {
174             throw ValidationException.fileError(exception);
175         }
176     }
177
178     public void printValuelist(Vector values) {
179         try {
180             Enumeration valuesEnum = values.elements();
181             while (valuesEnum.hasMoreElements()) {
182                 Object JavaDoc value = valuesEnum.nextElement();
183                 session.getPlatform().appendLiteralToCall(getCall(), getWriter(), value);
184                 if (valuesEnum.hasMoreElements()) {
185                     getWriter().write(", ");
186                 }
187             }
188         } catch (IOException exception) {
189             throw ValidationException.fileError(exception);
190         }
191     }
192
193     /**
194      * If a distinct has been set the DISTINCT clause will be printed.
195      * This is required for batch reading.
196      */

197     public boolean requiresDistinct() {
198         return requiresDistinct;
199     }
200
201     protected void setCall(SQLCall call) {
202         this.call = call;
203     }
204
205     /**
206      * INTERNAL:
207      * Used in figuring out when to print a comma in the select clause
208      */

209     public void setIsFirstElementPrinted(boolean isFirstElementPrinted) {
210         this.isFirstElementPrinted = isFirstElementPrinted;
211     }
212
213     /**
214      * If a distinct has been set the DISTINCT clause will be printed.
215      * This is required for batch reading.
216      */

217     public void setRequiresDistinct(boolean requiresDistinct) {
218         this.requiresDistinct = requiresDistinct;
219     }
220
221     protected void setSession(AbstractSession theSession) {
222         session = theSession;
223     }
224
225     protected void setShouldPrintQualifiedNames(boolean shouldPrintQualifiedNames) {
226         this.shouldPrintQualifiedNames = shouldPrintQualifiedNames;
227     }
228
229     /**
230      * INTERNAL:
231      * Set the row for translation
232      */

233     protected void setTranslationRow(AbstractRecord theRow) {
234         translationRow = theRow;
235     }
236
237     public void setWriter(Writer writer) {
238         this.writer = writer;
239     }
240
241     public boolean shouldPrintParameterValues() {
242         return getTranslationRow() != null;
243     }
244
245     protected boolean shouldPrintQualifiedNames() {
246         return shouldPrintQualifiedNames;
247     }
248
249     /**
250      * Translate an expression i.e. call the appropriate
251      * translation method for the expression based on its
252      * type. The translation method is then responsible
253      * for translating the subexpressions.
254      */

255     protected void translateExpression(Expression theExpression) {
256         theExpression.printSQL(this);
257     }
258 }
259
Popular Tags