001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.BooleanConstantNode
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.compile;
023:
024: import org.apache.derby.iapi.services.compiler.MethodBuilder;
025:
026: import org.apache.derby.iapi.error.StandardException;
027:
028: import org.apache.derby.iapi.sql.compile.Optimizable;
029:
030: import org.apache.derby.iapi.types.BooleanDataValue;
031: import org.apache.derby.iapi.types.DataValueDescriptor;
032: import org.apache.derby.iapi.types.TypeId;
033:
034: import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
035:
036: import org.apache.derby.iapi.util.ReuseFactory;
037: import java.sql.Types;
038:
039: public final class BooleanConstantNode extends ConstantNode {
040: /* Cache actual value to save overhead and
041: * throws clauses.
042: */
043: boolean booleanValue;
044: boolean unknownValue;
045:
046: /**
047: * Initializer for a BooleanConstantNode.
048: *
049: * @param arg1 A boolean containing the value of the constant OR The TypeId for the type of the node
050: *
051: * @exception StandardException
052: */
053: public void init(Object arg1) throws StandardException {
054: /*
055: ** RESOLVE: The length is fixed at 1, even for nulls.
056: ** Is that OK?
057: */
058:
059: if (arg1 instanceof Boolean) {
060: /* Fill in the type information in the parent ValueNode */
061: super .init(TypeId.BOOLEAN_ID, Boolean.FALSE, ReuseFactory
062: .getInteger(1));
063:
064: booleanValue = ((Boolean) arg1).booleanValue();
065: super .setValue(getDataValueFactory().getDataValue(
066: booleanValue));
067: } else {
068: super .init(arg1, Boolean.TRUE, ReuseFactory.getInteger(0));
069: unknownValue = true;
070: }
071: }
072:
073: /**
074: * Return the value from this BooleanConstantNode
075: *
076: * @return The value of this BooleanConstantNode.
077: *
078: */
079:
080: //public boolean getBoolean()
081: //{
082: // return booleanValue;
083: //}
084: /**
085: * Return the length
086: *
087: * @return The length of the value this node represents
088: *
089: * @exception StandardException Thrown on error
090: */
091:
092: //public int getLength() throws StandardException
093: //{
094: // return value.getLength();
095: //}
096: /**
097: * Return an Object representing the bind time value of this
098: * expression tree. If the expression tree does not evaluate to
099: * a constant at bind time then we return null.
100: * This is useful for bind time resolution of VTIs.
101: * RESOLVE: What do we do for primitives?
102: *
103: * @return An Object representing the bind time value of this expression tree.
104: * (null if not a bind time constant.)
105: *
106: */
107: Object getConstantValueAsObject() {
108: return booleanValue ? Boolean.TRUE : Boolean.FALSE;
109: }
110:
111: /**
112: * Return the value as a string.
113: *
114: * @return The value as a string.
115: *
116: */
117: String getValueAsString() {
118: if (booleanValue) {
119: return "true";
120: } else {
121: return "false";
122: }
123: }
124:
125: /**
126: * Does this represent a true constant.
127: *
128: * @return Whether or not this node represents a true constant.
129: */
130: boolean isBooleanTrue() {
131: return (booleanValue && !unknownValue);
132: }
133:
134: /**
135: * Does this represent a false constant.
136: *
137: * @return Whether or not this node represents a false constant.
138: */
139: boolean isBooleanFalse() {
140: return (!booleanValue && !unknownValue);
141: }
142:
143: /**
144: * The default selectivity for value nodes is 50%. This is overridden
145: * in specific cases, such as the RelationalOperators.
146: */
147: public double selectivity(Optimizable optTable) {
148: if (isBooleanTrue()) {
149: return 1.0;
150: } else {
151: return 0.0;
152: }
153: }
154:
155: /**
156: * Eliminate NotNodes in the current query block. We traverse the tree,
157: * inverting ANDs and ORs and eliminating NOTs as we go. We stop at
158: * ComparisonOperators and boolean expressions. We invert
159: * ComparisonOperators and replace boolean expressions with
160: * boolean expression = false.
161: * NOTE: Since we do not recurse under ComparisonOperators, there
162: * still could be NotNodes left in the tree.
163: *
164: * @param underNotNode Whether or not we are under a NotNode.
165: *
166: *
167: * @return The modified expression
168: *
169: */
170: ValueNode eliminateNots(boolean underNotNode) {
171: if (!underNotNode) {
172: return this ;
173: }
174:
175: booleanValue = !booleanValue;
176: super
177: .setValue(getDataValueFactory().getDataValue(
178: booleanValue));
179:
180: return this ;
181: }
182:
183: /**
184: * This generates the proper constant. It is implemented
185: * by every specific constant node (e.g. IntConstantNode).
186: *
187: * @param acb The ExpressionClassBuilder for the class being built
188: * @param mb The method the code to place the code
189: *
190: */
191: void generateConstant(ExpressionClassBuilder acb, MethodBuilder mb) {
192: mb.push(booleanValue);
193: }
194:
195: /**
196: * Set the value in this ConstantNode.
197: */
198: public void setValue(DataValueDescriptor value) {
199: super .setValue(value);
200: unknownValue = true;
201: try {
202: if (value != null && value.isNotNull().getBoolean()) {
203: booleanValue = value.getBoolean();
204: unknownValue = false;
205: }
206: } catch (StandardException se) {
207: }
208: } // end of setValue
209: }
|