⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ternaryoperatornode.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*   Derby - Class org.apache.derby.impl.sql.compile.TernaryOperatorNode   Copyright 1998, 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.services.compiler.MethodBuilder;import org.apache.derby.iapi.services.compiler.LocalField;import org.apache.derby.iapi.services.io.StoredFormatIds;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.sql.compile.C_NodeTypes;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.store.access.Qualifier;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.sql.compile.TypeCompiler;import org.apache.derby.iapi.types.NumberDataValue;import org.apache.derby.iapi.types.StringDataValue;import org.apache.derby.iapi.types.TypeId;import org.apache.derby.iapi.types.DataTypeDescriptor;import org.apache.derby.iapi.store.access.Qualifier;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.reference.ClassName;import org.apache.derby.iapi.services.classfile.VMOpcode;import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;import org.apache.derby.iapi.util.JBitSet;import org.apache.derby.iapi.util.ReuseFactory;import java.lang.reflect.Modifier;import java.sql.Types;import java.util.Vector;/** * A TernaryOperatorNode represents a built-in ternary operators. * This covers  built-in functions like substr(). * Java operators are not represented here: the JSQL language allows Java * methods to be called from expressions, but not Java operators. * * @author Jerry Brenner */public class TernaryOperatorNode extends ValueNode{	String		operator;	String		methodName;	int			operatorType;	ValueNode	receiver; 	ValueNode	leftOperand;	ValueNode	rightOperand;	String		resultInterfaceType;	String		receiverInterfaceType;	String		leftInterfaceType;	String		rightInterfaceType;	int			trimType;	public static final int TRIM = 0;	public static final int LOCATE = 1;	public static final int SUBSTRING = 2;	public static final int LIKE = 3;	public static final int TIMESTAMPADD = 4;	public static final int TIMESTAMPDIFF = 5;	static final String[] TernaryOperators = {"trim", "LOCATE", "substring", "like", "TIMESTAMPADD", "TIMESTAMPDIFF"};	static final String[] TernaryMethodNames = {"trim", "locate", "substring", "like", "timestampAdd", "timestampDiff"};	static final String[] TernaryResultType = {ClassName.StringDataValue, 			ClassName.NumberDataValue,			ClassName.ConcatableDataValue,			ClassName.BooleanDataValue,            ClassName.DateTimeDataValue, 			ClassName.NumberDataValue};	static final String[][] TernaryArgType = {	{ClassName.StringDataValue, ClassName.StringDataValue, "java.lang.Integer"},	{ClassName.StringDataValue, ClassName.StringDataValue, ClassName.NumberDataValue},	{ClassName.ConcatableDataValue, ClassName.NumberDataValue, ClassName.NumberDataValue},	{ClassName.DataValueDescriptor, ClassName.DataValueDescriptor, ClassName.DataValueDescriptor},    {ClassName.DateTimeDataValue, "java.lang.Integer", ClassName.NumberDataValue}, // time.timestampadd( interval, count)    {ClassName.DateTimeDataValue, "java.lang.Integer", ClassName.DateTimeDataValue}// time2.timestampDiff( interval, time1)	};	/**	 * Initializer for a TernaryOperatorNode	 *	 * @param receiver		The receiver (eg, string being operated on in substr())	 * @param leftOperand	The left operand of the node	 * @param rightOperand	The right operand of the node	 * @param operatorType	The type of the operand	 */	public void init(					Object receiver,					Object leftOperand,					Object rightOperand,					Object operatorType,					Object trimType)	{		this.receiver = (ValueNode) receiver;		this.leftOperand = (ValueNode) leftOperand;		this.rightOperand = (ValueNode) rightOperand;		this.operatorType = ((Integer) operatorType).intValue();		this.operator = (String) TernaryOperators[this.operatorType];		this.methodName = (String) TernaryMethodNames[this.operatorType];		this.resultInterfaceType = (String) TernaryResultType[this.operatorType];		this.receiverInterfaceType = (String) TernaryArgType[this.operatorType][0];		this.leftInterfaceType = (String) TernaryArgType[this.operatorType][1];		this.rightInterfaceType = (String) TernaryArgType[this.operatorType][2];		if (trimType != null)				this.trimType = ((Integer) trimType).intValue();	}	/**	 * 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" + 				"resultInterfaceType: " + resultInterfaceType + "\n" + 				"receiverInterfaceType: " + receiverInterfaceType + "\n" + 				"leftInterfaceType: " + leftInterfaceType + "\n" + 				"rightInterfaceType: " + rightInterfaceType + "\n" + 				super.toString();		}		else		{			return "";		}	}	/**	 * 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);		receiver.setClause(clause);		leftOperand.setClause(clause);		if (rightOperand != null)		{			rightOperand.setClause(clause);		}	}	/**	 * 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 (receiver != null)			{				printLabel(depth, "receiver: ");				receiver.treePrint(depth + 1);			}			if (leftOperand != null)			{				printLabel(depth, "leftOperand: ");				leftOperand.treePrint(depth + 1);			}			if (rightOperand != null)			{				printLabel(depth, "rightOperand: ");				rightOperand.treePrint(depth + 1);			}		}	}	/**	 * 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	{		receiver = receiver.bindExpression(fromList, subqueryList, 			aggregateVector);		leftOperand = leftOperand.bindExpression(fromList, subqueryList, 			aggregateVector);		if (rightOperand != null)		{			rightOperand = rightOperand.bindExpression(fromList, subqueryList, 				aggregateVector);		}		if (operatorType == TRIM)			trimBind();		else if (operatorType == LOCATE)			locateBind();		else if (operatorType == SUBSTRING)			substrBind();		else if (operatorType == TIMESTAMPADD)            timestampAddBind();		else if (operatorType == TIMESTAMPDIFF)            timestampDiffBind();		return this;	}	/**	 * Preprocess an expression tree.  We do a number of transformations	 * here (including subqueries, IN lists, LIKE and BETWEEN) plus	 * subquery flattening.	 * NOTE: This is done before the outer ResultSetNode is preprocessed.	 *	 * @param	numTables			Number of tables in the DML Statement	 * @param	outerFromList		FromList from outer query block	 * @param	outerSubqueryList	SubqueryList from outer query block	 * @param	outerPredicateList	PredicateList from outer query block	 *	 * @return		The modified expression	 *	 * @exception StandardException		Thrown on error	 */	public ValueNode preprocess(int numTables,								FromList outerFromList,								SubqueryList outerSubqueryList,								PredicateList outerPredicateList) 					throws StandardException	{		receiver = receiver.preprocess(numTables,											 outerFromList, outerSubqueryList,											 outerPredicateList);		leftOperand = leftOperand.preprocess(numTables,											 outerFromList, outerSubqueryList,											 outerPredicateList);		if (rightOperand != null)		{			rightOperand = rightOperand.preprocess(numTables,												   outerFromList, outerSubqueryList,												   outerPredicateList);		}		return this;	}	/**	 * Do code generation for this ternary operator.	 *	 * @param acb	The ExpressionClassBuilder for the class we're generating	 * @param mb	The method the expression will go into	 *	 *	 * @exception StandardException		Thrown on error	 */	public void generateExpression(ExpressionClassBuilder acb,											MethodBuilder mb)		throws StandardException	{		int nargs = 0;		String receiverType = null;		/* Allocate an object for re-use to hold the result of the operator */		LocalField field = acb.newFieldDeclaration(Modifier.PRIVATE, resultInterfaceType);		receiver.generateExpression(acb, mb);		if (operatorType == TRIM)		{			mb.push(trimType);			mb.getField(field);			nargs = 2;			receiverType = receiverInterfaceType;		}		else if (operatorType == LOCATE)		{			leftOperand.generateExpression(acb, mb); 			mb.upCast(leftInterfaceType);			rightOperand.generateExpression(acb, mb);			mb.upCast(rightInterfaceType);			mb.getField(field);			nargs = 3;				}		else if (operatorType == SUBSTRING)		{			leftOperand.generateExpression(acb, mb); 			mb.upCast(leftInterfaceType);			if (rightOperand != null)			{				rightOperand.generateExpression(acb, mb);				mb.upCast(rightInterfaceType);			}			else			{				mb.pushNull(rightInterfaceType);			}			mb.getField(field); // third arg			mb.push(receiver.getTypeServices().getMaximumWidth());			nargs = 4;			receiverType = receiverInterfaceType;		}		else if (operatorType == TIMESTAMPADD || operatorType == TIMESTAMPDIFF)        {            Object intervalType = leftOperand.getConstantValueAsObject();            if( SanityManager.DEBUG)                SanityManager.ASSERT( intervalType != null && intervalType instanceof Integer,                                      "Invalid interval type used for " + operator);            mb.push( ((Integer) intervalType).intValue());            rightOperand.generateExpression( acb, mb);            mb.upCast( TernaryArgType[ operatorType][2]);            acb.getCurrentDateExpression( mb);			mb.getField(field);			nargs = 4;			receiverType = receiverInterfaceType;        }            		mb.callMethod(VMOpcode.INVOKEINTERFACE, receiverType, methodName, resultInterfaceType, nargs);		/*		** Store the result of the method call in the field, so we can re-use		** the object.		*/		mb.putField(field);	}	/**	 * Set the leftOperand to the specified ValueNode	 *	 * @param newLeftOperand	The new leftOperand	 *	 * @return None.	 */	public void setLeftOperand(ValueNode newLeftOperand)	{		leftOperand = newLeftOperand;	}	/**	 * Get the leftOperand	 *	 * @return The current leftOperand.	 */	public ValueNode getLeftOperand()	{		return leftOperand;	}	/**	 * Set the rightOperand to the specified ValueNode	 *	 * @param newRightOperand	The new rightOperand	 *	 * @return None.	 */	public void setRightOperand(ValueNode newRightOperand)	{		rightOperand = newRightOperand;	}	/**	 * Get the rightOperand	 *	 * @return The current rightOperand.	 */	public ValueNode getRightOperand()	{		return rightOperand;	}	/**	 * Categorize this predicate.  Initially, this means	 * building a bit map of the referenced tables for each predicate.	 * If the source of this ColumnReference (at the next underlying level) 	 * is not a ColumnReference or a VirtualColumnNode then this predicate	 * will not be pushed down.	 *	 * For example, in:	 *		select * from (select 1 from s) a (x) where x = 1	 * we will not push down x = 1.	 * NOTE: It would be easy to handle the case of a constant, but if the	 * inner SELECT returns an arbitrary expression, then we would have to copy	 * that tree into the pushed predicate, and that tree could contain	 * subqueries and method calls.	 * RESOLVE - revisit this issue once we have views.	 *	 * @param referencedTabs	JBitSet with bit map of referenced FromTables	 * @param simplePredsOnly	Whether or not to consider method	 *							calls, field references and conditional nodes	 *							when building bit map	 *	 * @return boolean		Whether or not source.expression is a ColumnReference	 *						or a VirtualColumnNode.	 * @exception StandardException			Thrown on error	 */	public boolean categorize(JBitSet referencedTabs, boolean simplePredsOnly)		throws StandardException	{		boolean pushable;		pushable = receiver.categorize(referencedTabs, simplePredsOnly);		pushable = (leftOperand.categorize(referencedTabs, simplePredsOnly) && pushable);		if (rightOperand != null)		{			pushable = (rightOperand.categorize(referencedTabs, simplePredsOnly) && pushable);		}		return pushable;	}	/**	 * Remap all ColumnReferences in this tree to be clones of the	 * underlying expression.	 *	 * @return ValueNode			The remapped expression tree.	 *	 * @exception StandardException			Thrown on error	 */	public ValueNode remapColumnReferencesToExpressions()		throws StandardException	{		receiver = receiver.remapColumnReferencesToExpressions();		leftOperand = leftOperand.remapColumnReferencesToExpressions();		if (rightOperand != null)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -