KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > ConstantNode


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.ConstantNode
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.types.DataValueDescriptor;
25 import org.apache.derby.iapi.types.TypeId;
26 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
27
28 import org.apache.derby.iapi.error.StandardException;
29
30 import org.apache.derby.iapi.services.compiler.MethodBuilder;
31 import org.apache.derby.iapi.services.compiler.LocalField;
32
33 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
34
35 import java.lang.reflect.Modifier JavaDoc;
36
37 import org.apache.derby.iapi.services.sanity.SanityManager;
38
39 import org.apache.derby.iapi.store.access.Qualifier;
40
41 import org.apache.derby.iapi.util.ReuseFactory;
42
43 import java.sql.Date JavaDoc;
44 import java.sql.Time JavaDoc;
45 import java.sql.Timestamp JavaDoc;
46
47 import java.util.Vector JavaDoc;
48
49 /**
50  * ConstantNode holds literal constants as well as nulls.
51  * <p>
52  * A NULL from the parser may not yet know its type; that
53  * must be set during binding, as it is for parameters.
54  * <p>
55  * the DataValueDescriptor methods want to throw exceptions
56  * when they are of the wrong type, but to do that they
57  * must check typeId when the value is null, rather than
58  * the instanceof check they do for returning a valid value.
59  * <p>
60  * For code generation, we generate a static field. Then we set the
61  * field be the proper constant expression (something like <code>
62  * getDatavalueFactory().getCharDataValue("hello", ...)) </code>)
63  * in the constructor of the generated method. Ideally
64  * we would have just
65  */

66 abstract class ConstantNode extends ValueNode
67 {
68     protected DataValueDescriptor value;
69
70     /*
71     ** In case generateExpression() is called twice (something
72     ** that probably wont happen but might), we will cache
73     ** our generated expression and just return a reference
74     ** to the field that holds our value (instead of having
75     ** two fields holding the same constant).
76     */

77
78     /**
79      * Initializer for non-numeric types
80      *
81      * @param typeId The Type ID of the datatype
82      * @param nullable True means the constant is nullable
83      * @param maximumWidth The maximum number of bytes in the data value
84      *
85      * @exception StandardException
86      */

87     public void init(
88             Object JavaDoc typeId,
89             Object JavaDoc nullable,
90             Object JavaDoc maximumWidth)
91         throws StandardException
92     {
93         /* Fill in the type information in the parent ValueNode */
94         init(
95                             typeId,
96                             ReuseFactory.getInteger(0),
97                             ReuseFactory.getInteger(0),
98                             nullable,
99                             maximumWidth);
100     }
101
102     /**
103      * Constructor for untyped nodes, which contain little information
104      *
105      */

106     ConstantNode()
107     {
108         super();
109     }
110
111     /**
112      * Set the value in this ConstantNode.
113      */

114     void setValue(DataValueDescriptor value)
115     {
116         this.value = value;
117     }
118
119     /**
120       * Get the value in this ConstantNode
121       */

122     public DataValueDescriptor getValue()
123     {
124         return value;
125     }
126
127     /**
128      * Convert this object to a String. See comments in QueryTreeNode.java
129      * for how this should be done for tree printing.
130      *
131      * @return This object as a String
132      */

133
134     public String JavaDoc toString()
135     {
136         if (SanityManager.DEBUG)
137         {
138             return "value: " + value + "\n" +
139                 super.toString();
140         }
141         else
142         {
143             return "";
144         }
145     }
146
147     /**
148      * Return whether or not this expression tree is cloneable.
149      *
150      * @return boolean Whether or not this expression tree is cloneable.
151      */

152     public boolean isCloneable()
153     {
154         return true;
155     }
156
157     /**
158      * Return a clone of this node.
159      *
160      * @return ValueNode A clone of this node.
161      *
162      */

163     public ValueNode getClone()
164     {
165         /* All constants can simply be reused */
166         return this;
167     }
168
169     /**
170      * Bind this expression. This means binding the sub-expressions,
171      * as well as figuring out what the return type is for this expression.
172      * In this case, there are no sub-expressions, and the return type
173      * is already known, so this is just a stub.
174      *
175      * @param fromList The FROM list for the query this
176      * expression is in, for binding columns.
177      * @param subqueryList The subquery list being built as we find SubqueryNodes
178      * @param aggregateVector The aggregate vector being built as we find AggregateNodes
179      *
180      * @return The new top of the expression tree.
181      */

182     public ValueNode bindExpression(
183             FromList fromList, SubqueryList subqueryList,
184             Vector JavaDoc aggregateVector)
185     {
186         /*
187         ** This has to be here for binding to work, but it doesn't
188         ** have to do anything, because the datatypes of constant nodes
189         ** are pre-generated by the parser.
190         */

191         return this;
192     }
193
194     /**
195      * Return whether or not this expression tree represents a constant expression.
196      *
197      * @return Whether or not this expression tree represents a constant expression.
198      */

199     public boolean isConstantExpression()
200     {
201         return true;
202     }
203
204     /** @see ValueNode#constantExpression */
205     public boolean constantExpression(PredicateList whereClause)
206     {
207         return true;
208     }
209
210     /**
211      * For a ConstantNode, we generate the equivalent literal value.
212      * A null is generated as a Null value cast to the type of
213      * the constant node.
214      * The subtypes of ConstantNode generate literal expressions
215      * for non-null values.
216      *
217      * @param acb The ExpressionClassBuilder for the class being built
218      * @param mb The method the code to place the code
219      *
220      * @exception StandardException Thrown on error
221      */

222     public void generateExpression
223     (
224         ExpressionClassBuilder acb,
225         MethodBuilder mb
226     ) throws StandardException
227     {
228         /* Are we generating a SQL null value? */
229         if (isNull())
230         {
231             acb.generateNull(mb, getTypeCompiler());
232         }
233         else
234         {
235             generateConstant(acb, mb); // ask sub type to give a constant,
236
// usually a literal like 'hello'
237

238             acb.generateDataValue(mb, getTypeCompiler(), (LocalField) null);
239         }
240     }
241
242     /**
243      * This generates the proper constant. It is implemented
244      * by every specific constant node (e.g. IntConstantNode).
245      *
246      * @param acb The ExpressionClassBuilder for the class being built
247      * @param mb The method the code to place the code
248      *
249      * @exception StandardException Thrown on error
250      */

251     abstract void generateConstant(ExpressionClassBuilder acb, MethodBuilder mb)
252         throws StandardException;
253
254     /**
255      * Return whether or not this node represents a typed null constant.
256      *
257      */

258     public boolean isNull()
259     {
260         return (value == null || value.isNull());
261     }
262
263     /**
264      * Return the variant type for the underlying expression.
265      * The variant type can be:
266      * VARIANT - variant within a scan
267      * (method calls and non-static field access)
268      * SCAN_INVARIANT - invariant within a scan
269      * (column references from outer tables)
270      * QUERY_INVARIANT - invariant within the life of a query
271      * VARIANT - immutable
272      *
273      * @return The variant type for the underlying expression.
274      */

275     protected int getOrderableVariantType()
276     {
277         // Constants are constant for the life of the query
278
return Qualifier.CONSTANT;
279     }
280         
281     protected boolean isEquivalent(ValueNode o) throws StandardException
282     {
283         if (isSameNodeType(o)) {
284             ConstantNode other = (ConstantNode)o;
285             
286             // value can be null which represents a SQL NULL value.
287
return ( (other.getValue() == null && getValue() == null) ||
288                      (other.getValue() != null &&
289                              other.getValue().compare(getValue()) == 0) );
290         }
291         return false;
292     }
293 }
294
Popular Tags