private DataValueDescriptor convertConstant(TypeId toTypeId, int maxWidth,
DataValueDescriptor constantValue)
throws StandardException
{
int formatId = toTypeId.getTypeFormatId();
DataValueFactory dvf = getDataValueFactory();
switch (formatId)
{
default:
case StoredFormatIds.CHAR_TYPE_ID:
return constantValue;
case StoredFormatIds.VARCHAR_TYPE_ID:
String sourceValue = constantValue.getString();
int sourceWidth = sourceValue.length();
int posn;
/*
** If the input is already the right length, no normalization is
** necessary - just return the source.
**
*/
if (sourceWidth <= maxWidth)
{
if(formatId == StoredFormatIds.VARCHAR_TYPE_ID)
return dvf.getVarcharDataValue(sourceValue);
}
/*
** Check whether any non-blank characters will be truncated.
*/
for (posn = maxWidth; posn < sourceWidth; posn++)
{
if (sourceValue.charAt(posn) != ' ')
{
String typeName = null;
if (formatId == StoredFormatIds.VARCHAR_TYPE_ID)
typeName = TypeId.VARCHAR_NAME;
throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION,
typeName,
StringUtil.formatForPrint(sourceValue),
String.valueOf(maxWidth));
}
}
if (formatId == StoredFormatIds.VARCHAR_TYPE_ID)
return dvf.getVarcharDataValue(sourceValue.substring(0, maxWidth));
case StoredFormatIds.LONGVARCHAR_TYPE_ID:
//No need to check widths here (unlike varchar), since no max width
return dvf.getLongvarcharDataValue(constantValue.getString());
}
}