Search in sources :

Example 1 with RawToBinaryFormatStream

use of org.apache.derby.iapi.types.RawToBinaryFormatStream in project derby by apache.

the class EmbedPreparedStatement method setBinaryStreamInternal.

/**
 * Set the given stream for the specified parameter.
 *
 * If <code>lengthLess</code> is <code>true</code>, the following
 * conditions are either not checked or verified at the execution time
 * of the prepared statement:
 * <ol><li>If the stream length is negative.
 *     <li>If the stream's actual length equals the specified length.</ol>
 * The <code>lengthLess</code> variable was added to differentiate between
 * streams with invalid lengths and streams without known lengths.
 *
 * @param parameterIndex the 1-based index of the parameter to set.
 * @param x the data.
 * @param lengthLess tells whether we know the length of the data or not.
 * @param length the length of the data. Ignored if <code>lengthLess</code>
 *          is <code>true</code>.
 */
private void setBinaryStreamInternal(int parameterIndex, InputStream x, final boolean lengthLess, long length) throws SQLException {
    if (!lengthLess && length < 0)
        throw newSQLException(SQLState.NEGATIVE_STREAM_LENGTH);
    int jdbcTypeId = getParameterJDBCType(parameterIndex);
    if (x == null) {
        setNull(parameterIndex, jdbcTypeId);
        return;
    }
    // in that case the cast to int would not be appropriate.
    if (!lengthLess && length > Integer.MAX_VALUE) {
        throw newSQLException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, getParameterMetaData().getParameterTypeName(parameterIndex));
    }
    try {
        RawToBinaryFormatStream rawStream;
        if (lengthLess) {
            // Indicate that we don't know the logical length of the stream.
            length = DataValueDescriptor.UNKNOWN_LOGICAL_LENGTH;
            DataTypeDescriptor[] dtd = preparedStatement.getParameterTypes();
            rawStream = new RawToBinaryFormatStream(x, dtd[parameterIndex - 1].getMaximumWidth(), dtd[parameterIndex - 1].getTypeName());
        } else {
            rawStream = new RawToBinaryFormatStream(x, (int) length);
        }
        getParms().getParameterForSet(parameterIndex - 1).setValue(rawStream, (int) length);
    } catch (StandardException t) {
        throw EmbedResultSet.noStateChangeException(t);
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) RawToBinaryFormatStream(org.apache.derby.iapi.types.RawToBinaryFormatStream)

Example 2 with RawToBinaryFormatStream

use of org.apache.derby.iapi.types.RawToBinaryFormatStream in project derby by apache.

the class EmbedResultSet method updateBinaryStreamInternal.

/**
 * Set the given binary stream for the specified parameter.
 *
 * If <code>lengthLess</code> is <code>true</code>, the following
 * conditions are either not checked or verified at the execution time
 * of <code>updateRow</code>/<code>insertRow</code>:
 * <ol><li>If the stream length is negative.
 *     <li>If the stream's actual length equals the specified length.</ol>
 * The <code>lengthLess</code> variable was added to differentiate between
 * streams with invalid lengths and streams without known lengths.
 *
 * @param columnIndex the 1-based index of the parameter to set.
 * @param x the data.
 * @param lengthLess tells whether we know the length of the data or not.
 * @param length the length of the data. Ignored if <code>lengthLess</code>
 *          is <code>true</code>.
 * @param updateMethodName the name of the method calling us. Used in
 *      error messages.
 * @throws SQLException if reading the data fails, or one of the data
 *      checks fails.
 */
private void updateBinaryStreamInternal(int columnIndex, InputStream x, final boolean lengthLess, long length, String updateMethodName) throws SQLException {
    RawToBinaryFormatStream rawStream;
    if (!lengthLess) {
        if (length < 0)
            throw newSQLException(SQLState.NEGATIVE_STREAM_LENGTH);
        // (e.g into a blob column).
        if (length > Integer.MAX_VALUE) {
            throw newSQLException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, getColumnSQLType(columnIndex));
        }
        rawStream = new RawToBinaryFormatStream(x, (int) length);
    } else {
        // Force length to UNKNOWN_LOGICAL_LENGTH if stream is length less.
        length = DataValueDescriptor.UNKNOWN_LOGICAL_LENGTH;
        rawStream = new RawToBinaryFormatStream(x, getMaxColumnWidth(columnIndex), getColumnSQLType(columnIndex));
    }
    try {
        getDVDforColumnToBeUpdated(columnIndex, updateMethodName).setValue(rawStream, (int) length);
    } catch (StandardException t) {
        throw noStateChangeException(t);
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) RawToBinaryFormatStream(org.apache.derby.iapi.types.RawToBinaryFormatStream)

Aggregations

RawToBinaryFormatStream (org.apache.derby.iapi.types.RawToBinaryFormatStream)2 StandardException (org.apache.derby.shared.common.error.StandardException)2 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)1