*/
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.requiresTypeFromContext())
{
/*
** According to the SQL standard, if trim has a ? receiver,
** its type is varchar with the implementation-defined maximum length
** for a varchar.
*/
receiver.setType(getVarcharDescriptor());
//check if this parameter can pick up it's collation from the
//character that will be used for trimming. If not(meaning the
//character to be trimmed is also a parameter), then it will take
//it's collation from the compilation schema.
if (!leftOperand.requiresTypeFromContext()) {
receiver.getTypeServices().setCollationDerivation(
leftOperand.getTypeServices().getCollationDerivation());
receiver.getTypeServices().setCollationType(
leftOperand.getTypeServices().getCollationType());
} else {
receiver.setCollationUsingCompilationSchema(
StringDataValue.COLLATION_DERIVATION_IMPLICIT);
}
}
/* Is there a ? parameter on the left? */
if (leftOperand.requiresTypeFromContext())
{
/* Set the left operand type to varchar. */
leftOperand.setType(getVarcharDescriptor());
//collation of ? operand should be picked up from the context.
//By the time we come here, receiver will have correct collation
//set on it and hence we can rely on it to get correct collation
//for the ? for the character that needs to be used for trimming.
leftOperand.getTypeServices().setCollationDerivation(
receiver.getTypeServices().getCollationDerivation());
leftOperand.getTypeServices().setCollationType(
receiver.getTypeServices().getCollationType());
}
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) {
// 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.