KickJava   Java API By Example, From Geeks To Geeks.

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


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.queryframework.*;
27 import oracle.toplink.essentials.expressions.*;
28 import oracle.toplink.essentials.internal.helper.*;
29 import oracle.toplink.essentials.internal.sessions.AbstractRecord;
30 import oracle.toplink.essentials.internal.sessions.AbstractSession;
31
32 /**
33  * Used for wrapping constant values.
34  */

35 public class ConstantExpression extends Expression {
36     protected Object JavaDoc value;
37     protected Expression localBase;
38
39     public ConstantExpression() {
40         super();
41     }
42
43     public ConstantExpression(Object JavaDoc newValue, Expression baseExpression) {
44         super();
45         value = newValue;
46         localBase = baseExpression;
47     }
48
49     /**
50      * INTERNAL:
51      * Used for debug printing.
52      */

53     public String JavaDoc descriptionOfNodeType() {
54         return "Constant";
55     }
56
57     /**
58      * Return the expression builder which is the ultimate base of this expression, or
59      * null if there isn't one (shouldn't happen if we start from a root)
60      */

61     public ExpressionBuilder getBuilder() {
62         return getLocalBase().getBuilder();
63     }
64
65     protected Expression getLocalBase() {
66         return localBase;
67     }
68
69     public Object JavaDoc getValue() {
70         return value;
71     }
72
73     public boolean isConstantExpression() {
74         return true;
75     }
76
77     /**
78      * INTERNAL:
79      */

80     public boolean isValueExpression() {
81         return true;
82     }
83
84     /**
85      * INTERNAL:
86      * Used for cloning.
87      */

88     protected void postCopyIn(Dictionary alreadyDone) {
89         super.postCopyIn(alreadyDone);
90         localBase = localBase.copiedVersionFrom(alreadyDone);
91     }
92
93     /**
94      * INTERNAL:
95      * Print SQL onto the stream, using the ExpressionPrinter for context
96      */

97     public void printSQL(ExpressionSQLPrinter printer) {
98         Object JavaDoc value = getLocalBase().getFieldValue(getValue());
99         printer.printPrimitive(value);
100     }
101
102     /**
103      * INTERNAL:
104      * Print SQL, this is called from functions, so must not be converted through the mapping.
105      */

106     public void printSQLWithoutConversion(ExpressionSQLPrinter printer) {
107         printer.printPrimitive(getValue());
108     }
109
110     /**
111      * INTERNAL:
112      * Print java for project class generation
113      */

114     public void printJava(ExpressionJavaPrinter printer) {
115         printer.printJava(getValue());
116     }
117
118     /**
119      * INTERNAL:
120      * This expression is built on a different base than the one we want. Rebuild it and
121      * return the root of the new tree
122      */

123     public Expression rebuildOn(Expression newBase) {
124         Expression result = (ConstantExpression)clone();
125         result.setLocalBase(getLocalBase().rebuildOn(newBase));
126         return result;
127     }
128
129     public void setLocalBase(Expression e) {
130         localBase = e;
131     }
132
133     /**
134      * INTERNAL:
135      * Rebuild myself against the base, with the values of parameters supplied by the context
136      * expression. This is used for transforming a standalone expression (e.g. the join criteria of a mapping)
137      * into part of some larger expression. You normally would not call this directly, instead calling twist
138      * See the comment there for more details"
139      */

140     public Expression twistedForBaseAndContext(Expression newBase, Expression context) {
141         return (Expression)this.clone();
142     }
143
144     /**
145      * INTERNAL:
146      * Return the value for in memory comparison.
147      * This is only valid for valueable expressions.
148      */

149     public Object JavaDoc valueFromObject(Object JavaDoc object, AbstractSession session, AbstractRecord translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean isObjectUnregistered) {
150         return getLocalBase().getFieldValue(getValue());
151     }
152
153     /**
154      * INTERNAL:
155      * Used to print a debug form of the expression tree.
156      */

157     public void writeDescriptionOn(BufferedWriter writer) throws IOException {
158         writer.write(String.valueOf(getValue()));
159     }
160
161     /**
162      * INTERNAL:
163      * Append the constant value into the printer
164      */

165     public void writeFields(ExpressionSQLPrinter printer, Vector newFields, SQLSelectStatement statement) {
166         //print ", " before each selected field except the first one
167
if (printer.isFirstElementPrinted()) {
168             printer.printString(", ");
169         } else {
170             printer.setIsFirstElementPrinted(true);
171         }
172
173         // This field is a constant value, so any name can be used.
174
newFields.addElement(new DatabaseField("*"));
175         printSQL(printer);
176     }
177 }
178
Popular Tags