// context is pushed when the header needs to be generated. To fix
// this, tell the DVD/generator which header format to use.
dvd.setStreamHeaderFormat(Boolean.valueOf(
!getEmbedConnection().getDatabase().getDataDictionary().
checkVersion(DataDictionary.DD_VERSION_DERBY_10_5, null)));
ReaderToUTF8Stream utfIn;
int usableLength = DataValueDescriptor.UNKNOWN_LOGICAL_LENGTH;
if (!lengthLess) {
// check for -ve length here
if (length < 0)
throw newSQLException(SQLState.NEGATIVE_STREAM_LENGTH);
// max number of characters that can be set to be inserted
// in Derby is 2Gb-1 (ie Integer.MAX_VALUE).
// (e.g into a CLOB column).
if (length > Integer.MAX_VALUE ) {
throw newSQLException(
SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE,
getColumnSQLType(columnIndex));
}
// length is +ve. at this point, all checks for negative
// length has already been done
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 (getColumnType(columnIndex) == Types.CLOB) {
// Need column width to figure out if truncation is
// needed
int colWidth = getMaxColumnWidth(columnIndex);
// 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 used which is min(colWidth,length) provided
// length - colWidth has trailing blanks only
if (usableLength > colWidth) {
truncationLength = usableLength - colWidth;
usableLength = colWidth;
}
}
utfIn = new ReaderToUTF8Stream(reader, usableLength,
truncationLength, getColumnSQLType(columnIndex),
dvd.getStreamHeaderGenerator());
} else {
int colWidth = getMaxColumnWidth(columnIndex);
utfIn = new ReaderToUTF8Stream(reader, colWidth,
getColumnSQLType(columnIndex),
dvd.getStreamHeaderGenerator());
}
dvd.setValue(utfIn, usableLength);