*/
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:
case StoredFormatIds.NATIONAL_CHAR_TYPE_ID:
case StoredFormatIds.NATIONAL_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)
{
switch (formatId)
{
// For NCHAR we must pad the result, saves on normilization later if all
// constants are of the correct size
case StoredFormatIds.NATIONAL_CHAR_TYPE_ID:
if (sourceWidth < maxWidth)
{
StringBuffer stringBuffer = new StringBuffer(sourceValue);
int needed = maxWidth - sourceWidth;
char blankArray[] = new char[needed];
for (int i = 0; i < needed; i++)
blankArray[i] = ' ';
stringBuffer.append(blankArray, 0,
maxWidth - sourceWidth);
sourceValue = stringBuffer.toString();
}
return dvf.getNationalCharDataValue(sourceValue);
case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID:
return dvf.getNationalVarcharDataValue(sourceValue);
case 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;
switch (formatId)
{
case StoredFormatIds.NATIONAL_CHAR_TYPE_ID:
typeName = TypeId.NATIONAL_CHAR_NAME;
break;
case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID:
typeName = TypeId.NATIONAL_VARCHAR_NAME;
break;
case StoredFormatIds.VARCHAR_TYPE_ID:
typeName = TypeId.VARCHAR_NAME;
break;
}
throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION,
typeName,
StringUtil.formatForPrint(sourceValue),
String.valueOf(maxWidth));
}
}
switch (formatId)
{
case StoredFormatIds.NATIONAL_CHAR_TYPE_ID:
return dvf.getNationalCharDataValue(sourceValue.substring(0, maxWidth));
case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID:
return dvf.getNationalVarcharDataValue(sourceValue.substring(0, maxWidth));
case 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());
case StoredFormatIds.NATIONAL_LONGVARCHAR_TYPE_ID:
//No need to check widths here (unlike varchar), since no max width
return dvf.getNationalLongvarcharDataValue(constantValue.getString());
}
}