KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.BooleanConstantNode
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.services.compiler.MethodBuilder;
25
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.sql.compile.Optimizable;
29
30 import org.apache.derby.iapi.types.BooleanDataValue;
31 import org.apache.derby.iapi.types.DataValueDescriptor;
32 import org.apache.derby.iapi.types.TypeId;
33
34 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
35
36 import org.apache.derby.iapi.util.ReuseFactory;
37 import java.sql.Types JavaDoc;
38
39 public final class BooleanConstantNode extends ConstantNode
40 {
41     /* Cache actual value to save overhead and
42      * throws clauses.
43      */

44     boolean booleanValue;
45     boolean unknownValue;
46
47     /**
48      * Initializer for a BooleanConstantNode.
49      *
50      * @param arg1 A boolean containing the value of the constant OR The TypeId for the type of the node
51      *
52      * @exception StandardException
53      */

54     public void init(
55                     Object JavaDoc arg1)
56         throws StandardException
57     {
58         /*
59         ** RESOLVE: The length is fixed at 1, even for nulls.
60         ** Is that OK?
61         */

62
63         if (arg1 instanceof Boolean JavaDoc)
64         {
65             /* Fill in the type information in the parent ValueNode */
66             super.init(TypeId.BOOLEAN_ID,
67              Boolean.FALSE,
68              ReuseFactory.getInteger(1));
69
70             booleanValue = ((Boolean JavaDoc) arg1).booleanValue();
71             super.setValue(getDataValueFactory().getDataValue(booleanValue));
72         }
73         else
74         {
75             super.init(
76                 arg1,
77                 Boolean.TRUE,
78                 ReuseFactory.getInteger(0));
79             unknownValue = true;
80         }
81     }
82
83     /**
84      * Return the value from this BooleanConstantNode
85      *
86      * @return The value of this BooleanConstantNode.
87      *
88      */

89
90     //public boolean getBoolean()
91
//{
92
// return booleanValue;
93
//}
94

95     /**
96      * Return the length
97      *
98      * @return The length of the value this node represents
99      *
100      * @exception StandardException Thrown on error
101      */

102
103     //public int getLength() throws StandardException
104
//{
105
// return value.getLength();
106
//}
107

108     /**
109      * Return an Object representing the bind time value of this
110      * expression tree. If the expression tree does not evaluate to
111      * a constant at bind time then we return null.
112      * This is useful for bind time resolution of VTIs.
113      * RESOLVE: What do we do for primitives?
114      *
115      * @return An Object representing the bind time value of this expression tree.
116      * (null if not a bind time constant.)
117      *
118      */

119     Object JavaDoc getConstantValueAsObject()
120     {
121         return booleanValue ? Boolean.TRUE : Boolean.FALSE;
122     }
123
124     /**
125      * Return the value as a string.
126      *
127      * @return The value as a string.
128      *
129      */

130     String JavaDoc getValueAsString()
131     {
132         if (booleanValue)
133         {
134             return "true";
135         }
136         else
137         {
138             return "false";
139         }
140     }
141
142     /**
143      * Does this represent a true constant.
144      *
145      * @return Whether or not this node represents a true constant.
146      */

147     boolean isBooleanTrue()
148     {
149         return (booleanValue && !unknownValue);
150     }
151
152     /**
153      * Does this represent a false constant.
154      *
155      * @return Whether or not this node represents a false constant.
156      */

157     boolean isBooleanFalse()
158     {
159         return (!booleanValue && !unknownValue);
160     }
161
162     /**
163      * The default selectivity for value nodes is 50%. This is overridden
164      * in specific cases, such as the RelationalOperators.
165      */

166     public double selectivity(Optimizable optTable)
167     {
168         if (isBooleanTrue())
169         {
170             return 1.0;
171         }
172         else
173         {
174             return 0.0;
175         }
176     }
177
178     /**
179      * Eliminate NotNodes in the current query block. We traverse the tree,
180      * inverting ANDs and ORs and eliminating NOTs as we go. We stop at
181      * ComparisonOperators and boolean expressions. We invert
182      * ComparisonOperators and replace boolean expressions with
183      * boolean expression = false.
184      * NOTE: Since we do not recurse under ComparisonOperators, there
185      * still could be NotNodes left in the tree.
186      *
187      * @param underNotNode Whether or not we are under a NotNode.
188      *
189      *
190      * @return The modified expression
191      *
192      */

193     ValueNode eliminateNots(boolean underNotNode)
194     {
195         if (! underNotNode)
196         {
197             return this;
198         }
199
200         booleanValue = !booleanValue;
201         super.setValue(getDataValueFactory().getDataValue(booleanValue));
202
203         return this;
204     }
205
206     /**
207      * This generates the proper constant. It is implemented
208      * by every specific constant node (e.g. IntConstantNode).
209      *
210      * @param acb The ExpressionClassBuilder for the class being built
211      * @param mb The method the code to place the code
212      *
213      */

214     void generateConstant(ExpressionClassBuilder acb, MethodBuilder mb)
215     {
216         mb.push(booleanValue);
217     }
218
219     /**
220      * Set the value in this ConstantNode.
221      */

222     public void setValue(DataValueDescriptor value)
223     {
224         super.setValue( value);
225         unknownValue = true;
226         try
227         {
228             if( value != null && value.isNotNull().getBoolean())
229             {
230                 booleanValue = value.getBoolean();
231                 unknownValue = false;
232             }
233         }
234         catch( StandardException se){}
235     } // end of setValue
236
}
237
Popular Tags