📄 unaryoperatornode.java
字号:
/* Derby - Class org.apache.derby.impl.sql.compile.UnaryOperatorNode Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.impl.sql.compile;import org.apache.derby.iapi.store.access.Qualifier;import org.apache.derby.iapi.sql.compile.Visitable;import org.apache.derby.iapi.sql.compile.Visitor;import org.apache.derby.iapi.sql.dictionary.DataDictionary;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.reference.ClassName;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.compiler.MethodBuilder;import org.apache.derby.iapi.services.compiler.LocalField;import org.apache.derby.iapi.services.io.StoredFormatIds;import org.apache.derby.iapi.types.TypeId;import org.apache.derby.iapi.types.DataTypeDescriptor;import java.lang.reflect.Modifier;import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;import org.apache.derby.iapi.util.JBitSet;import org.apache.derby.iapi.services.classfile.VMOpcode;import java.sql.Types;import java.util.Vector;/** * A UnaryOperatorNode represents a built-in unary operator as defined by * the ANSI/ISO SQL standard. This covers operators like +, -, NOT, and IS NULL. * Java operators are not represented here: the JSQL language allows Java * methods to be called from expressions, but not Java operators. * * @author Jeff Lichtman */public class UnaryOperatorNode extends ValueNode{ String operator; String methodName; int operatorType; String resultInterfaceType; String receiverInterfaceType; /** * WARNING: operand may be NULL for COUNT(*). */ ValueNode operand; public final static int UNARY_PLUS = 1; public final static int UNARY_MINUS = 2; public final static int NOT = 3; public final static int IS_NULL = 4; // At the time of adding XML support, it was decided that // we should avoid creating new OperatorNodes where possible. // So for the XML-related unary operators we just add the // necessary code to _this_ class, similar to what is done in // TernarnyOperatorNode. Subsequent unary operators (whether // XML-related or not) should follow this example when // possible. public final static int XMLPARSE_OP = 0; public final static int XMLSERIALIZE_OP = 1; // NOTE: in the following 4 arrays, order // IS important. static final String[] UnaryOperators = { "xmlparse", "xmlserialize" }; static final String[] UnaryMethodNames = { "XMLParse", "XMLSerialize" }; static final String[] UnaryResultTypes = { ClassName.XMLDataValue, // XMLParse ClassName.StringDataValue // XMLSerialize }; static final String[] UnaryArgTypes = { ClassName.StringDataValue, // XMLParse ClassName.XMLDataValue // XMLSerialize }; // Array to hold Objects that contain primitive // args required by the operator method call. private Object [] additionalArgs; /** * Initializer for a UnaryOperatorNode * * @param operand The operand of the node * @param operatorOrOpType Either 1) the name of the operator, * OR 2) an Integer holding the operatorType for this operator. * @param methodNameOrParams Either 1) name of the method * to call for this operator, or 2) an array of Objects * from which primitive method parameters can be * retrieved. */ public void init( Object operand, Object operatorOrOpType, Object methodNameOrAddedArgs) { this.operand = (ValueNode) operand; if (operatorOrOpType instanceof String) { // then 2nd and 3rd params are operator and methodName, // respectively. this.operator = (String) operatorOrOpType; this.methodName = (String) methodNameOrAddedArgs; this.operatorType = -1; } else { // 2nd and 3rd params are operatorType and additional args, // respectively. if (SanityManager.DEBUG) { SanityManager.ASSERT( ((operatorOrOpType instanceof Integer) && ((methodNameOrAddedArgs == null) || (methodNameOrAddedArgs instanceof Object[]))), "Init params in UnaryOperator node have the " + "wrong type."); } this.operatorType = ((Integer) operatorOrOpType).intValue(); this.operator = UnaryOperators[this.operatorType]; this.methodName = UnaryMethodNames[this.operatorType]; this.resultInterfaceType = UnaryResultTypes[this.operatorType]; this.receiverInterfaceType = UnaryArgTypes[this.operatorType]; this.additionalArgs = (Object[])methodNameOrAddedArgs; } } /** * Initializer for a UnaryOperatorNode * * @param operand The operand of the node */ public void init(Object operand) { this.operand = (ValueNode) operand; this.operatorType = -1; } /** * Set the operator. * * @param operator The operator. * * @return Nothing. */ void setOperator(String operator) { this.operator = operator; this.operatorType = -1; } /** * Get the operator of this unary operator. * * @return The operator of this unary operator. */ String getOperatorString() { return operator; } /** * Set the methodName. * * @param methodName The methodName. * * @return Nothing. */ void setMethodName(String methodName) { this.methodName = methodName; this.operatorType = -1; } /** * Convert this object to a String. See comments in QueryTreeNode.java * for how this should be done for tree printing. * * @return This object as a String */ public String toString() { if (SanityManager.DEBUG) { return "operator: " + operator + "\n" + "methodName: " + methodName + "\n" + super.toString(); } else { return ""; } } /** * Prints the sub-nodes of this object. See QueryTreeNode.java for * how tree printing is supposed to work. * * @param depth The depth of this node in the tree * * @return Nothing */ public void printSubNodes(int depth) { if (SanityManager.DEBUG) { super.printSubNodes(depth); if (operand != null) { printLabel(depth, "operand: "); operand.treePrint(depth + 1); } } } /** * Get the operand of this unary operator. * * @return The operand of this unary operator. */ public ValueNode getOperand() { return operand; } /** * Set the clause that this node appears in. * * @param clause The clause that this node appears in. * * @return Nothing. */ public void setClause(int clause) { super.setClause(clause); /* ** Operator may be null for COUNT(*) */ if (operand != null) { operand.setClause(clause); } } /** * Bind this expression. This means binding the sub-expressions, * as well as figuring out what the return type is for this expression. * * @param fromList The FROM list for the query this * expression is in, for binding columns. * @param subqueryList The subquery list being built as we find SubqueryNodes * @param aggregateVector The aggregate vector being built as we find AggregateNodes * * @return The new top of the expression tree. * * @exception StandardException Thrown on error */ public ValueNode bindExpression( FromList fromList, SubqueryList subqueryList, Vector aggregateVector) throws StandardException { return bindUnaryOperator(fromList, subqueryList, aggregateVector); } /** * Workhorse for bindExpression. This exists so it can be called * by child classes. */ protected ValueNode bindUnaryOperator( FromList fromList, SubqueryList subqueryList, Vector aggregateVector) throws StandardException { /* ** Operand can be null for COUNT(*) which ** is treated like a normal aggregate. */ if (operand == null) { return this; } operand = operand.bindExpression(fromList, subqueryList, aggregateVector); if (operand.isParameterNode()) bindParameter(); /* If the operand is not a built-in type, then generate a bound conversion * tree to a built-in type. */ if (! (operand instanceof UntypedNullConstantNode) && ! operand.getTypeId().systemBuiltIn() && ! (this instanceof IsNullNode)) { operand = operand.genSQLJavaSQLTree(); } if (operatorType == XMLPARSE_OP) bindXMLParse(); else if (operatorType == XMLSERIALIZE_OP) bindXMLSerialize(); return this; } /** * Bind an XMLPARSE operator. Makes sure the operand type * is correct, and sets the result type. * * @exception StandardException Thrown on error */ public void bindXMLParse() throws StandardException { // Check the type of the operand - this function is allowed only on // string value (char) types. TypeId operandType = operand.getTypeId(); if (operandType != null) { switch (operandType.getJDBCTypeId()) { case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.CLOB:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -