KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.TestConstraintNode
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.compile.C_NodeTypes;
25
26 import org.apache.derby.iapi.services.compiler.MethodBuilder;
27 import org.apache.derby.iapi.reference.ClassName;
28
29 import org.apache.derby.iapi.error.StandardException;
30
31 import org.apache.derby.iapi.types.TypeId;
32 import org.apache.derby.iapi.types.BooleanDataValue;
33 import org.apache.derby.iapi.types.DataTypeDescriptor;
34
35 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
36 import org.apache.derby.iapi.services.classfile.VMOpcode;
37
38 import java.util.Vector JavaDoc;
39
40 /**
41  * A TestConstraintNode is used to determine when a constraint
42  * has been violated.
43  *
44  * @author jeff
45  */

46
47 public class TestConstraintNode extends UnaryLogicalOperatorNode
48 {
49     private String JavaDoc sqlState;
50     private String JavaDoc tableName;
51     private String JavaDoc constraintName;
52
53     /**
54      * Initializer for a TestConstraintNode
55      *
56      * @param booleanValue The operand of the constraint test
57      * @param sqlState The SQLState of the exception to throw if the
58      * constraint has failed
59      * @param tableName The name of the table that the constraint is on
60      * @param constraintName The name of the constraint being checked
61      */

62
63     public void init(Object JavaDoc booleanValue,
64                      Object JavaDoc sqlState,
65                      Object JavaDoc tableName,
66                      Object JavaDoc constraintName)
67     {
68         super.init(booleanValue, "throwExceptionIfFalse");
69         this.sqlState = (String JavaDoc) sqlState;
70         this.tableName = (String JavaDoc) tableName;
71         this.constraintName = (String JavaDoc) constraintName;
72     }
73
74     /**
75      * Bind this logical operator. All that has to be done for binding
76      * a logical operator is to bind the operand, check that the operand
77      * is SQLBoolean, and set the result type to SQLBoolean.
78      *
79      * @param fromList The query's FROM list
80      * @param subqueryList The subquery list being built as we find SubqueryNodes
81      * @param aggregateVector The aggregate vector being built as we find AggregateNodes
82      *
83      * @return The new top of the expression tree.
84      *
85      * @exception StandardException Thrown on error
86      */

87
88     public ValueNode bindExpression(
89         FromList fromList, SubqueryList subqueryList,
90         Vector JavaDoc aggregateVector)
91             throws StandardException
92     {
93         bindUnaryOperator(fromList, subqueryList, aggregateVector);
94
95         /*
96         ** If the operand is not boolean, cast it.
97         */

98
99         if ( ! operand.getTypeServices().getTypeId().getSQLTypeName().equals(
100                                                         TypeId.BOOLEAN_NAME))
101         {
102             operand = (ValueNode)
103                 getNodeFactory().getNode(
104                     C_NodeTypes.CAST_NODE,
105                     operand,
106                     new DataTypeDescriptor(TypeId.BOOLEAN_ID, true),
107                     getContextManager());
108             ((CastNode) operand).bindCastNodeOnly();
109         }
110
111         /* Set the type info */
112         setFullTypeInfo();
113
114         return this;
115     }
116
117     /**
118      * Do code generation for the TestConstraint operator.
119      *
120      * @param acb The ExpressionClassBuilder for the class we're generating
121      * @param mb The method the expression will go into
122      *
123      *
124      * @exception StandardException Thrown on error
125      */

126
127     public void generateExpression(ExpressionClassBuilder acb,
128                                             MethodBuilder mb)
129                                     throws StandardException
130     {
131
132         /*
133         ** This generates the following code:
134         **
135         ** operand.testConstraint(sqlState, tableName, constraintName)
136         */

137
138         operand.generateExpression(acb, mb);
139
140         mb.push(sqlState);
141         mb.push(tableName);
142         mb.push(constraintName);
143
144         mb.callMethod(VMOpcode.INVOKEINTERFACE, ClassName.BooleanDataValue,
145                 "throwExceptionIfFalse", ClassName.BooleanDataValue, 3);
146
147     }
148 }
149
Popular Tags