📄 ternaryoperatornode.java
字号:
{ rightOperand = rightOperand.remapColumnReferencesToExpressions(); } return this; } /** * Return whether or not this expression tree represents a constant expression. * * @return Whether or not this expression tree represents a constant expression. */ public boolean isConstantExpression() { return (receiver.isConstantExpression() && leftOperand.isConstantExpression() && (rightOperand == null || rightOperand.isConstantExpression())); } /** @see ValueNode#constantExpression */ public boolean constantExpression(PredicateList whereClause) { return (receiver.constantExpression(whereClause) && leftOperand.constantExpression(whereClause) && (rightOperand == null || rightOperand.constantExpression(whereClause))); } /** * Accept a visitor, and call v.visit() * on child nodes as necessary. * * @param v the visitor * * @exception StandardException on error */ public Visitable accept(Visitor v) throws StandardException { Visitable returnNode = v.visit(this); if (v.skipChildren(this)) { return returnNode; } if (receiver != null && !v.stopTraversal()) { receiver = (ValueNode)receiver.accept(v); } if (leftOperand != null && !v.stopTraversal()) { leftOperand = (ValueNode)leftOperand.accept(v); } if (rightOperand != null && !v.stopTraversal()) { rightOperand = (ValueNode)rightOperand.accept(v); } return returnNode; } /** * Bind trim expression. * @return The new top of the expression tree. * * @exception StandardException Thrown on error */ private ValueNode trimBind() throws StandardException { TypeId receiverType; TypeId resultType = TypeId.getBuiltInTypeId(Types.VARCHAR); // handle parameters here /* Is there a ? parameter for the receiver? */ if (receiver.isParameterNode()) { /* ** According to the SQL standard, if trim has a ? receiver, ** its type is varchar with the implementation-defined maximum length ** for a varchar. */ ((ParameterNode) receiver).setDescriptor(getVarcharDescriptor()); } /* Is there a ? parameter on the left? */ if (leftOperand.isParameterNode()) { /* Set the left operand type to varchar. */ ((ParameterNode) leftOperand).setDescriptor(getVarcharDescriptor()); } bindToBuiltIn(); /* ** Check the type of the receiver - this function is allowed only on ** string value types. */ receiverType = receiver.getTypeId(); if (receiverType.userType()) throwBadType("trim", receiverType.getSQLTypeName()); receiver = castArgToString(receiver); if ((receiverType.getTypeFormatId() == StoredFormatIds.CLOB_TYPE_ID) || (receiverType.getTypeFormatId() == StoredFormatIds.NCLOB_TYPE_ID)) { // special case for CLOBs: if we start with a CLOB, we have to get // a CLOB as a result (as opposed to a VARCHAR), because we can have a // CLOB that is beyond the max length of VARCHAR (ex. "clob(100k)"). // This is okay because CLOBs, like VARCHARs, allow variable-length // values (which is a must for the trim to actually work). resultType = receiverType; } /* ** Check the type of the leftOperand (trimSet). ** The leftOperand should be a string value type. */ TypeId leftCTI; leftCTI = leftOperand.getTypeId(); if (leftCTI.userType()) throwBadType("trim", leftCTI.getSQLTypeName()); leftOperand = castArgToString(leftOperand); /* ** The result type of trim is varchar. */ setResultType(resultType); return this; } /* ** set result type for operator */ private void setResultType(TypeId resultType) throws StandardException { setType(new DataTypeDescriptor( resultType, true, receiver.getTypeServices().getMaximumWidth() ) ); } /** * Bind locate operator * * @return The new top of the expression tree. * * @exception StandardException Thrown on error */ public ValueNode locateBind() throws StandardException { TypeId firstOperandType, secondOperandType, offsetType; /* * Is there a ? parameter for the first arg. Copy the * left/firstOperand's. If the left/firstOperand are both parameters, * both will be max length. */ if( receiver.isParameterNode()) { if( leftOperand.isParameterNode()) { ((ParameterNode) receiver).setDescriptor(getVarcharDescriptor()); } else { if( leftOperand.getTypeId().isStringTypeId() ) { ((ParameterNode) receiver).setDescriptor( leftOperand.getTypeServices()); } } } /* * Is there a ? parameter for the second arg. Copy the receiver's. * If the receiver are both parameters, both will be max length. */ if(leftOperand.isParameterNode()) { if(receiver.isParameterNode()) { ((ParameterNode) leftOperand).setDescriptor(getVarcharDescriptor()); } else { if( receiver.getTypeId().isStringTypeId() ) { ((ParameterNode) leftOperand).setDescriptor( receiver.getTypeServices()); } } } /* * Is there a ? paramter for the third arg. It will be an int. */ if( rightOperand.isParameterNode()) { ((ParameterNode) rightOperand).setDescriptor( new DataTypeDescriptor(TypeId.INTEGER_ID, true)); } bindToBuiltIn(); /* ** Check the type of the operand - this function is allowed only ** for: receiver = CHAR ** firstOperand = CHAR ** secondOperand = INT */ secondOperandType = leftOperand.getTypeId(); offsetType = rightOperand.getTypeId(); firstOperandType = receiver.getTypeId(); if (!firstOperandType.isStringTypeId() || !secondOperandType.isStringTypeId() || offsetType.getJDBCTypeId() != Types.INTEGER) throw StandardException.newException(SQLState.LANG_DB2_FUNCTION_INCOMPATIBLE, "LOCATE", "FUNCTION"); /* ** The result type of a LocateFunctionNode is an integer. */ setType(new DataTypeDescriptor(TypeId.INTEGER_ID, receiver.getTypeServices().isNullable())); return this; } /* cast arg to a varchar */ protected ValueNode castArgToString(ValueNode vn) throws StandardException { TypeCompiler vnTC = vn.getTypeCompiler(); if (! vn.getTypeId().isStringTypeId()) { ValueNode newNode = (ValueNode) getNodeFactory().getNode( C_NodeTypes.CAST_NODE, vn, DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.VARCHAR, true, vnTC.getCastToCharWidth( vn.getTypeServices())), getContextManager()); ((CastNode) newNode).bindCastNodeOnly(); return newNode; } return vn; } /** * Bind substr expression. * * @return The new top of the expression tree. * * @exception StandardException Thrown on error */ public ValueNode substrBind() throws StandardException { TypeId receiverType; TypeId resultType; // handle parameters here /* Is there a ? parameter for the receiver? */ if (receiver.isParameterNode()) { /* ** According to the SQL standard, if substr has a ? receiver, ** its type is varchar with the implementation-defined maximum length ** for a varchar. */ ((ParameterNode) receiver).setDescriptor(getVarcharDescriptor()); } /* Is there a ? parameter on the left? */ if (leftOperand.isParameterNode()) { /* Set the left operand type to int. */ ((ParameterNode) leftOperand).setDescriptor( new DataTypeDescriptor(TypeId.INTEGER_ID, true)); } /* Is there a ? parameter on the right? */ if ((rightOperand != null) && rightOperand.isParameterNode()) { /* Set the right operand type to int. */ ((ParameterNode) rightOperand).setDescriptor( new DataTypeDescriptor(TypeId.INTEGER_ID, true)); } bindToBuiltIn(); if (!leftOperand.getTypeId().isNumericTypeId() || (rightOperand != null && !rightOperand.getTypeId().isNumericTypeId())) throw StandardException.newException(SQLState.LANG_DB2_FUNCTION_INCOMPATIBLE, "SUBSTR", "FUNCTION"); /* ** Check the type of the receiver - this function is allowed only on ** string value types. */ resultType = receiverType = receiver.getTypeId(); switch (receiverType.getJDBCTypeId()) { case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.CLOB: break; default: { throwBadType("SUBSTR", receiverType.getSQLTypeName()); } } // Determine the maximum length of the result int resultLen = receiver.getTypeServices().getMaximumWidth(); if (rightOperand != null && rightOperand instanceof ConstantNode) { if (((ConstantNode)rightOperand).getValue().getInt() < resultLen) resultLen = ((ConstantNode)rightOperand).getValue().getInt(); } /* ** The result type of substr is a string type */ setType(new DataTypeDescriptor( resultType, true, resultLen )); return this; } /** * Bind TIMESTAMPADD expression. * * @return The new top of the expression tree. * * @exception StandardException Thrown on error */ private ValueNode timestampAddBind() throws StandardException { if( ! bindParameter( rightOperand, Types.INTEGER)) { int jdbcType = rightOperand.getTypeId().getJDBCTypeId(); if( jdbcType != Types.TINYINT && jdbcType != Types.SMALLINT && jdbcType != Types.INTEGER && jdbcType != Types.BIGINT) throw StandardException.newException(SQLState.LANG_INVALID_FUNCTION_ARG_TYPE, rightOperand.getTypeId().getSQLTypeName(), ReuseFactory.getInteger( 2), operator); } bindDateTimeArg( receiver, 3); setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor( Types.TIMESTAMP)); return this; } // end of timestampAddBind /** * Bind TIMESTAMPDIFF expression. * * @return The new top of the expression tree. * * @exception StandardException Thrown on error */ private ValueNode timestampDiffBind() throws StandardException { bindDateTimeArg( rightOperand, 2); bindDateTimeArg( receiver, 3); setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor( Types.INTEGER)); return this; } // End of timestampDiffBind private void bindDateTimeArg( ValueNode arg, int argNumber) throws StandardException { if( ! bindParameter( arg, Types.TIMESTAMP)) { if( ! arg.getTypeId().isDateTimeTimeStampTypeId()) throw StandardException.newException(SQLState.LANG_INVALID_FUNCTION_ARG_TYPE, arg.getTypeId().getSQLTypeName(), ReuseFactory.getInteger( argNumber), operator); } } // end of bindDateTimeArg private boolean bindParameter( ValueNode arg, int jdbcType) throws StandardException { if( arg.isParameterNode() && arg.getTypeId() == null) { ((ParameterNode) arg).setDescriptor( new DataTypeDescriptor(TypeId.getBuiltInTypeId( jdbcType), true)); return true; } return false; } // end of bindParameter public ValueNode getReceiver() { return receiver; } /* throw bad type message */ private void throwBadType(String funcName, String type) throws StandardException { throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, funcName, type); } /* bind arguments to built in types */ protected void bindToBuiltIn() throws StandardException { /* If the receiver is not a built-in type, then generate a bound conversion * tree to a built-in type. */ if (! receiver.getTypeId().systemBuiltIn()) { receiver = receiver.genSQLJavaSQLTree(); } /* If the left operand is not a built-in type, then generate a bound conversion * tree to a built-in type. */ if (! leftOperand.getTypeId().systemBuiltIn()) { leftOperand = leftOperand.genSQLJavaSQLTree(); } /* If the right operand is not a built-in type, then generate a bound conversion * tree to a built-in type. */ if (rightOperand != null) { if (! rightOperand.getTypeId().systemBuiltIn()) { rightOperand = rightOperand.genSQLJavaSQLTree(); } } } private DataTypeDescriptor getVarcharDescriptor() { return new DataTypeDescriptor(TypeId.getBuiltInTypeId(Types.VARCHAR), true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -