KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.IsNode
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.sql.dictionary.DataDictionary;
25 import org.apache.derby.iapi.services.sanity.SanityManager;
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.sql.compile.NodeFactory;
29
30 import org.apache.derby.iapi.types.BooleanDataValue;
31
32 import org.apache.derby.iapi.services.compiler.MethodBuilder;
33
34 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
35 import org.apache.derby.impl.sql.compile.ActivationClassBuilder;
36 import org.apache.derby.iapi.reference.ClassName;
37 import org.apache.derby.iapi.services.classfile.VMOpcode;
38
39 import java.lang.reflect.Modifier JavaDoc;
40
41 import java.util.Vector JavaDoc;
42
43 public class IsNode extends BinaryLogicalOperatorNode
44 {
45     private boolean notMe; // set to true if we're to negate the sense of this node
46

47     /**
48      * Initializer for an IsNode
49      *
50      * @param leftOperand The left operand of the IS
51      * @param rightOperand The right operand of the IS
52      * @param notMe Whether to reverse the sense of this node.
53      */

54
55     public void init(
56                             Object JavaDoc leftOperand,
57                             Object JavaDoc rightOperand,
58                             Object JavaDoc notMe )
59     {
60         // the false for shortCir
61
super.init(leftOperand, rightOperand, "is" );
62         this.notMe = ((Boolean JavaDoc) notMe).booleanValue();
63     }
64
65     /**
66      * Bind this logical operator. All that has to be done for binding
67      * a logical operator is to bind the operands, check that both operands
68      * are BooleanDataValue, and set the result type to BooleanDataValue.
69      *
70      * @param fromList The query's FROM list
71      * @param subqueryList The subquery list being built as we find SubqueryNodes
72      * @param aggregateVector The aggregate vector being built as we find AggregateNodes
73      *
74      * @return The new top of the expression tree.
75      *
76      * @exception StandardException Thrown on error
77      */

78
79     public ValueNode bindExpression(
80         FromList fromList, SubqueryList subqueryList,
81         Vector JavaDoc aggregateVector)
82             throws StandardException
83     {
84         super.bindExpression(fromList, subqueryList, aggregateVector);
85
86         leftOperand.checkIsBoolean();
87         rightOperand.checkIsBoolean();
88
89         setType(leftOperand.getTypeServices());
90
91         return this;
92     }
93
94     
95     /**
96      * Eliminate NotNodes in the current query block. We just mark whether
97      * this IS node is under an eliminated NOT node.
98      *
99      * @param underNotNode Whether or not we are under a NotNode.
100      *
101      *
102      * @return The modified expression
103      *
104      * @exception StandardException Thrown on error
105      */

106     ValueNode eliminateNots(boolean underNotNode)
107                     throws StandardException
108     {
109         if ( underNotNode ) { notMe = !notMe; }
110
111         leftOperand = leftOperand.eliminateNots( false);
112         rightOperand = rightOperand.eliminateNots( false );
113
114         return this;
115     }
116
117     /**
118      * Do the 1st step in putting child expressions into conjunctive normal
119      * form. This step ensures that the top level of the child expression is
120      * a chain of AndNodes terminated by a true BooleanConstantNode.
121      *
122      * @return The modified expression
123      *
124      * @exception StandardException Thrown on error
125      */

126     public ValueNode putAndsOnTop()
127                     throws StandardException
128     {
129         leftOperand = leftOperand.putAndsOnTop();
130         rightOperand = rightOperand.putAndsOnTop();
131
132         return this;
133     }
134
135     /**
136      * Verify that putAndsOnTop() did its job correctly. Verify that the top level
137      * of the expression is a chain of AndNodes terminated by a true BooleanConstantNode.
138      *
139      * @return Boolean which reflects validity of the tree.
140      */

141     public boolean verifyPutAndsOnTop()
142     {
143         return ( leftOperand.verifyPutAndsOnTop() && rightOperand.verifyPutAndsOnTop() );
144     }
145
146     /**
147      * Finish putting an expression into conjunctive normal
148      * form. An expression tree in conjunctive normal form meets
149      * the following criteria:
150      * o If the expression tree is not null,
151      * the top level will be a chain of AndNodes terminating
152      * in a true BooleanConstantNode.
153      * o The left child of an AndNode will never be an AndNode.
154      * o Any right-linked chain that includes an AndNode will
155      * be entirely composed of AndNodes terminated by a true BooleanConstantNode.
156      * o The left child of an OrNode will never be an OrNode.
157      * o Any right-linked chain that includes an OrNode will
158      * be entirely composed of OrNodes terminated by a false BooleanConstantNode.
159      * o ValueNodes other than AndNodes and OrNodes are considered
160      * leaf nodes for purposes of expression normalization.
161      * In other words, we won't do any normalization under
162      * those nodes.
163      *
164      * In addition, we track whether or not we are under a top level AndNode.
165      * SubqueryNodes need to know this for subquery flattening.
166      *
167      * @param underTopAndNode Whether or not we are under a top level AndNode.
168      *
169      *
170      * @return The modified expression
171      *
172      * @exception StandardException Thrown on error
173      */

174     public ValueNode changeToCNF(boolean underTopAndNode)
175                     throws StandardException
176     {
177         leftOperand = leftOperand.changeToCNF(false );
178         rightOperand = rightOperand.changeToCNF(false );
179
180         return this;
181     }
182
183     /**
184      * Verify that changeToCNF() did its job correctly. Verify that:
185      * o AndNode - rightOperand is not instanceof OrNode
186      * leftOperand is not instanceof AndNode
187      * o OrNode - rightOperand is not instanceof AndNode
188      * leftOperand is not instanceof OrNode
189      *
190      * @return Boolean which reflects validity of the tree.
191      */

192     public boolean verifyChangeToCNF()
193     {
194         return ( leftOperand.verifyChangeToCNF() && rightOperand.verifyChangeToCNF() );
195     }
196
197
198     /**
199      * Do code generation for this logical binary operator.
200      *
201      * @param acb The ExpressionClassBuilder for the class we're generating
202      * @param mb the method the expression will go into
203      *
204      * @exception StandardException Thrown on error
205      */

206
207     public void generateExpression(ExpressionClassBuilder acb,
208                                             MethodBuilder mb)
209         throws StandardException
210     {
211         String JavaDoc evaluatorMethodName;
212
213         /*
214         ** Generate the return value. Generated code is:
215         **
216         ** <fieldLeft>.<evaluatorMethodName>(<fieldRight>)
217         */

218
219         if ( notMe ) { evaluatorMethodName = "isNot"; }
220         else { evaluatorMethodName = "is"; }
221
222         leftOperand.generateExpression(acb, mb);
223         rightOperand.generateExpression(acb, mb);
224         mb.callMethod(VMOpcode.INVOKEINTERFACE, ClassName.BooleanDataValue, evaluatorMethodName,
225                             ClassName.BooleanDataValue, 1);
226     }
227 }
228
Popular Tags