throw newSQLException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE,
getParameterSQLType(parameterIndex));
try {
ReaderToUTF8Stream utfIn;
final StringDataValue dvd = (StringDataValue)
getParms().getParameter(parameterIndex -1);
dvd.setStreamHeaderFormat(usePreTenFiveHdrFormat());
// Need column width to figure out if truncation is needed
DataTypeDescriptor dtd[] = preparedStatement
.getParameterTypes();
int colWidth = dtd[parameterIndex - 1].getMaximumWidth();
// Holds either UNKNOWN_LOGICAL_LENGTH or the exact logical length.
int usableLength = DataValueDescriptor.UNKNOWN_LOGICAL_LENGTH;
if (!lengthLess) {
// We cast the length from long to int. This wouldn't be
// appropriate if the limit of 2G-1 is decided to be increased
// at a later stage.
usableLength = (int)length;
int truncationLength = 0;
// Currently long varchar does not allow for truncation of
// trailing blanks.
// For char and varchar types, current mechanism of
// materializing when using streams seems fine given their max
// limits.
// This change is fix for DERBY-352: Insert of clobs using
// streams should not materialize the entire stream into memory
// In case of clobs, the truncation of trailing blanks is
// factored in when reading from the stream without
// materializing the entire stream, and so the special casing
// for clob below.
if (jdbcTypeId == Types.CLOB)
{
// It is possible that the length of the stream passed in
// is greater than the column width, in which case the data
// from the stream needs to be truncated.
// usableLength is the length of the data from stream that
// can be inserted which is min(colWidth,length) provided
// length - colWidth has trailing blanks only
if (usableLength > colWidth) {
truncationLength = usableLength - colWidth;
usableLength = colWidth;
}
}
// Create a stream with truncation.
utfIn = new ReaderToUTF8Stream(reader, usableLength,
truncationLength, getParameterSQLType(parameterIndex),
dvd.getStreamHeaderGenerator());
} else {
// Create a stream without exactness checks,
// but with a maximum limit.
utfIn = new ReaderToUTF8Stream(reader, colWidth,
getParameterSQLType(parameterIndex),
dvd.getStreamHeaderGenerator());
}
// JDBC is one-based, DBMS is zero-based.
// Note that for lengthless stream, usableLength will be
// the maximum length for the column.