001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.TestConstraintNode
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.sql.compile.C_NodeTypes;
025:
026: import org.apache.derby.iapi.services.compiler.MethodBuilder;
027: import org.apache.derby.iapi.reference.ClassName;
028:
029: import org.apache.derby.iapi.error.StandardException;
030:
031: import org.apache.derby.iapi.types.TypeId;
032: import org.apache.derby.iapi.types.BooleanDataValue;
033: import org.apache.derby.iapi.types.DataTypeDescriptor;
034:
035: import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
036: import org.apache.derby.iapi.services.classfile.VMOpcode;
037:
038: import java.util.Vector;
039:
040: /**
041: * A TestConstraintNode is used to determine when a constraint
042: * has been violated.
043: *
044: * @author jeff
045: */
046:
047: public class TestConstraintNode extends UnaryLogicalOperatorNode {
048: private String sqlState;
049: private String tableName;
050: private String constraintName;
051:
052: /**
053: * Initializer for a TestConstraintNode
054: *
055: * @param booleanValue The operand of the constraint test
056: * @param sqlState The SQLState of the exception to throw if the
057: * constraint has failed
058: * @param tableName The name of the table that the constraint is on
059: * @param constraintName The name of the constraint being checked
060: */
061:
062: public void init(Object booleanValue, Object sqlState,
063: Object tableName, Object constraintName) {
064: super .init(booleanValue, "throwExceptionIfFalse");
065: this .sqlState = (String) sqlState;
066: this .tableName = (String) tableName;
067: this .constraintName = (String) constraintName;
068: }
069:
070: /**
071: * Bind this logical operator. All that has to be done for binding
072: * a logical operator is to bind the operand, check that the operand
073: * is SQLBoolean, and set the result type to SQLBoolean.
074: *
075: * @param fromList The query's FROM list
076: * @param subqueryList The subquery list being built as we find SubqueryNodes
077: * @param aggregateVector The aggregate vector being built as we find AggregateNodes
078: *
079: * @return The new top of the expression tree.
080: *
081: * @exception StandardException Thrown on error
082: */
083:
084: public ValueNode bindExpression(FromList fromList,
085: SubqueryList subqueryList, Vector aggregateVector)
086: throws StandardException {
087: bindUnaryOperator(fromList, subqueryList, aggregateVector);
088:
089: /*
090: ** If the operand is not boolean, cast it.
091: */
092:
093: if (!operand.getTypeServices().getTypeId().getSQLTypeName()
094: .equals(TypeId.BOOLEAN_NAME)) {
095: operand = (ValueNode) getNodeFactory().getNode(
096: C_NodeTypes.CAST_NODE, operand,
097: new DataTypeDescriptor(TypeId.BOOLEAN_ID, true),
098: getContextManager());
099: ((CastNode) operand).bindCastNodeOnly();
100: }
101:
102: /* Set the type info */
103: setFullTypeInfo();
104:
105: return this ;
106: }
107:
108: /**
109: * Do code generation for the TestConstraint operator.
110: *
111: * @param acb The ExpressionClassBuilder for the class we're generating
112: * @param mb The method the expression will go into
113: *
114: *
115: * @exception StandardException Thrown on error
116: */
117:
118: public void generateExpression(ExpressionClassBuilder acb,
119: MethodBuilder mb) throws StandardException {
120:
121: /*
122: ** This generates the following code:
123: **
124: ** operand.testConstraint(sqlState, tableName, constraintName)
125: */
126:
127: operand.generateExpression(acb, mb);
128:
129: mb.push(sqlState);
130: mb.push(tableName);
131: mb.push(constraintName);
132:
133: mb.callMethod(VMOpcode.INVOKEINTERFACE,
134: ClassName.BooleanDataValue, "throwExceptionIfFalse",
135: ClassName.BooleanDataValue, 3);
136:
137: }
138: }
|