📄 castnode.java
字号:
/* Derby - Class org.apache.derby.impl.sql.compile.CastNode 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.services.context.ContextManager;import org.apache.derby.iapi.services.compiler.MethodBuilder;import org.apache.derby.iapi.services.compiler.LocalField;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;import org.apache.derby.iapi.sql.compile.CompilerContext;import org.apache.derby.iapi.sql.compile.C_NodeTypes;import org.apache.derby.iapi.sql.dictionary.DataDictionary;import org.apache.derby.iapi.types.DataTypeUtilities;import org.apache.derby.iapi.types.TypeId;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.types.DataTypeDescriptor;import org.apache.derby.iapi.types.DataValueFactory;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.types.VariableSizeDataValue;import org.apache.derby.iapi.sql.compile.TypeCompiler;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.util.StringUtil;import org.apache.derby.iapi.reference.ClassName;import org.apache.derby.iapi.reference.JDBC30Translation;import org.apache.derby.iapi.services.classfile.VMOpcode;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.services.loader.ClassInspector;import org.apache.derby.iapi.sql.compile.Visitable;import org.apache.derby.iapi.sql.compile.Visitor;import org.apache.derby.iapi.sql.compile.C_NodeTypes;import java.lang.reflect.Modifier;import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;import org.apache.derby.iapi.types.NumberDataType;import org.apache.derby.iapi.util.JBitSet;import org.apache.derby.iapi.util.ReuseFactory;import org.apache.derby.catalog.AliasInfo;import org.apache.derby.catalog.TypeDescriptor;import org.apache.derby.iapi.types.SQLReal;import java.sql.Date;import java.sql.Time;import java.sql.Timestamp;import java.sql.Types;import java.util.Vector;/** * An CastNode represents a cast expressionr. * * @author Jerry Brenner */public class CastNode extends ValueNode{ DataTypeDescriptor castTarget; ValueNode castOperand; int targetCharType; TypeId destCTI = null; TypeId sourceCTI = null; boolean forDataTypeFunction = false; /* ** Static array of valid casts. Dimentions ** produce a single boolean which indicates ** whether the case is possible or not. */ /** * Initializer for a CastNode * * @param castOperand The operand of the node * @param castTarget DataTypeServices (target type of cast) * * @exception StandardException Thrown on error */ public void init(Object castOperand, Object castTarget) throws StandardException { this.castOperand = (ValueNode) castOperand; this.castTarget = (DataTypeDescriptor) castTarget; } /** * Initializer for a CastNode * * @param castOperand The operand of the node * @param charType CHAR or VARCHAR JDBC type as target * @param charLength target type length * * @exception StandardException Thrown on error */ public void init(Object castOperand, Object charType, Object charLength) throws StandardException { this.castOperand = (ValueNode) castOperand; int charLen = ((Integer) charLength).intValue(); targetCharType = ((Integer) charType).intValue(); if (charLen < 0) // unknown, figure out later return; this.castTarget = DataTypeDescriptor.getBuiltInDataTypeDescriptor(targetCharType, charLen); } /** * 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 "castTarget: " + castTarget + "\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 (castOperand != null) { printLabel(depth, "castOperand: "); castOperand.treePrint(depth + 1); } } } protected int getOrderableVariantType() throws StandardException { return castOperand.getOrderableVariantType(); } /** * 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); castOperand.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 { castOperand = castOperand.bindExpression( fromList, subqueryList, aggregateVector); if (castTarget == null) //CHAR or VARCHAR function without specifying target length { DataTypeDescriptor opndType = castOperand.getTypeServices(); int length = -1; TypeId srcTypeId = opndType.getTypeId(); if (opndType != null) { if (srcTypeId.isNumericTypeId()) { length = opndType.getPrecision() + 1; // 1 for the sign if (opndType.getScale() > 0) length += 1; // 1 for the decimal . } else { TypeId typeid = opndType.getTypeId(); if (length < 0) length = DataTypeUtilities.getColumnDisplaySize(typeid.getJDBCTypeId(),-1); } } if (length < 0) length = 1; // same default as in parser castTarget = DataTypeDescriptor.getBuiltInDataTypeDescriptor(targetCharType, length); } /* ** If castOperand is an untyped null, ** then we must set the type. */ if (castOperand instanceof UntypedNullConstantNode) { castOperand.setType(castTarget); } bindCastNodeOnly(); /* We can't chop out cast above an untyped null because * the store can't handle it. */ if ((castOperand instanceof ConstantNode) && !(castOperand instanceof UntypedNullConstantNode)) { /* If the castOperand is a typed constant then we do the cast at * bind time and return a constant of the correct type. * NOTE: This could return an exception, but we're prepared to * deal with that. (NumberFormatException, etc.) * We only worry about the easy (and useful) * converions at bind time. * Here's what we support: * source destination * ------ ----------- * boolean boolean * boolean char * char boolean * char date/time/ts * char non-decimal numeric * date/time/ts char * numeric char * numeric non-decimal numeric */ /* RESOLVE - to be filled in. */ ValueNode retNode = this; int sourceJDBCTypeId = sourceCTI.getJDBCTypeId(); int destJDBCTypeId = destCTI.getJDBCTypeId(); switch (sourceJDBCTypeId) { case Types.BIT: case JDBC30Translation.SQL_TYPES_BOOLEAN: // (BIT is boolean) if (destJDBCTypeId == Types.BIT || destJDBCTypeId == JDBC30Translation.SQL_TYPES_BOOLEAN) { retNode = castOperand; } else if (destJDBCTypeId == Types.CHAR) { BooleanConstantNode bcn = (BooleanConstantNode) castOperand; String booleanString = bcn.getValueAsString(); retNode = (ValueNode) getNodeFactory().getNode( C_NodeTypes.CHAR_CONSTANT_NODE, booleanString, ReuseFactory.getInteger( castTarget.getMaximumWidth()), getContextManager()); } break; case Types.CHAR: retNode = getCastFromCharConstant(destJDBCTypeId); break; case Types.DATE: case Types.TIME: case Types.TIMESTAMP: if (destJDBCTypeId == Types.CHAR) { String castValue = ((UserTypeConstantNode) castOperand). getObjectValue(). toString(); retNode = (ValueNode) getNodeFactory().getNode( C_NodeTypes.CHAR_CONSTANT_NODE, castValue, ReuseFactory.getInteger( castTarget.getMaximumWidth()), getContextManager()); } break; case Types.DECIMAL: // ignore decimal -> decimal casts for now if (destJDBCTypeId == Types.DECIMAL || destJDBCTypeId == Types.NUMERIC) break; // fall through case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: case Types.BIGINT: case Types.DOUBLE: case Types.REAL: retNode = getCastFromNumericType( ((ConstantNode) castOperand).getValue(), destJDBCTypeId); break; } // Return the new constant if the cast was performed return retNode; } return this; } /** * Bind this node but not its child. Caller has already bound * the child. * This is useful for when we generate a CastNode during binding * after having already bound the child. * * @return Nothing * * @exception StandardException Thrown on error */ public void bindCastNodeOnly() throws StandardException { /* ** The result type is always castTarget. */ setType(castTarget); destCTI = castTarget.getTypeId(); sourceCTI = castOperand.getTypeId(); /* ** If it is a java cast, do some work to make sure ** the classes are ok and that they are compatible */ if (! destCTI.systemBuiltIn())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -