Search in sources :

Example 21 with StandardException

use of org.apache.derby.shared.common.error.StandardException in project derby by apache.

the class EmbedPreparedStatement method setScale.

/**
 * Set the scale of a parameter.
 *
 * @param parameterIndex The first parameter is 1, the second is 2, ...
 * @param scale	The scale
 * @exception SQLException thrown on failure.
 */
private void setScale(int parameterIndex, int scale) throws SQLException {
    checkStatus();
    if (scale < 0)
        throw newSQLException(SQLState.BAD_SCALE_VALUE, scale);
    try {
        ParameterValueSet pvs = getParms();
        /* JDBC is one-based, DBMS is zero-based */
        DataValueDescriptor value = pvs.getParameter(parameterIndex - 1);
        int origvaluelen = value.getLength();
        ((VariableSizeDataValue) value).setWidth(VariableSizeDataValue.IGNORE_PRECISION, scale, false);
        if (value.getLength() < origvaluelen) {
            activation.addWarning(StandardException.newWarning(SQLState.LANG_VALUE_TRUNCATED, value.getString()));
        }
    } catch (StandardException t) {
        throw EmbedResultSet.noStateChangeException(t);
    }
}
Also used : ParameterValueSet(org.apache.derby.iapi.sql.ParameterValueSet) StandardException(org.apache.derby.shared.common.error.StandardException) VariableSizeDataValue(org.apache.derby.iapi.types.VariableSizeDataValue) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor)

Example 22 with StandardException

use of org.apache.derby.shared.common.error.StandardException 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 23 with StandardException

use of org.apache.derby.shared.common.error.StandardException in project derby by apache.

the class EmbedPreparedStatement method setCharacterStreamInternal.

/**
 * Set the given character 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 reader 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 setCharacterStreamInternal(int parameterIndex, Reader reader, final boolean lengthLess, long length) throws SQLException {
    // Check for negative length if length is specified.
    if (!lengthLess && length < 0)
        throw newSQLException(SQLState.NEGATIVE_STREAM_LENGTH);
    int jdbcTypeId = getParameterJDBCType(parameterIndex);
    if (reader == null) {
        setNull(parameterIndex, jdbcTypeId);
        return;
    }
    /*
           The value stored should not exceed the maximum value that can be 
           stored in an integer 
           This checking needs to be done because currently derby does not
           support Clob sizes greater than 2G-1 
        */
    if (!lengthLess && length > Integer.MAX_VALUE)
        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;
            // for clob below.
            if (jdbcTypeId == Types.CLOB) {
                // 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.
        // This is okay, based on the observation that
        // setValue does not use the value for anything at all.
        getParms().getParameterForSet(parameterIndex - 1).setValue(utfIn, usableLength);
    } catch (StandardException t) {
        throw EmbedResultSet.noStateChangeException(t);
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ReaderToUTF8Stream(org.apache.derby.iapi.types.ReaderToUTF8Stream) StringDataValue(org.apache.derby.iapi.types.StringDataValue)

Example 24 with StandardException

use of org.apache.derby.shared.common.error.StandardException in project derby by apache.

the class EmbedResultSet method adjustScale.

/**
 * <p>
 * Adjust the scale of a type.
 * </p>
 */
protected void adjustScale(int columnIndex, int scale) throws SQLException {
    /*
		* If the parameter type is DECIMAL or NUMERIC, then
		* we need to set them to the passed scale.
		*/
    int colType = getColumnType(columnIndex);
    if ((colType == Types.DECIMAL) || (colType == Types.NUMERIC)) {
        if (scale < 0)
            throw newSQLException(SQLState.BAD_SCALE_VALUE, scale);
        try {
            DataValueDescriptor value = updateRow.getColumn(columnIndex);
            int origvaluelen = value.getLength();
            ((VariableSizeDataValue) value).setWidth(VariableSizeDataValue.IGNORE_PRECISION, scale, false);
        } catch (StandardException t) {
            throw EmbedResultSet.noStateChangeException(t);
        }
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) VariableSizeDataValue(org.apache.derby.iapi.types.VariableSizeDataValue) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor)

Example 25 with StandardException

use of org.apache.derby.shared.common.error.StandardException in project derby by apache.

the class EmbedResultSet method updateCharacterStreamInternal.

/**
 * Set the given character 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 columnIndex the 1-based index of the parameter to set.
 * @param reader 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>.
 * @throws SQLException if reading the data fails, or one of the data
 *      checks fails.
 */
private void updateCharacterStreamInternal(int columnIndex, Reader reader, final boolean lengthLess, long length, String updateMethodName) throws SQLException {
    try {
        if (reader == null) {
            updateNull(columnIndex);
            return;
        }
        final StringDataValue dvd = (StringDataValue) getDVDforColumnToBeUpdated(columnIndex, updateMethodName);
        // In the case of updatable result sets, we cannot guarantee that a
        // 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);
            // (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;
            // for clob below.
            if (getColumnType(columnIndex) == Types.CLOB) {
                // Need column width to figure out if truncation is
                // needed
                int colWidth = getMaxColumnWidth(columnIndex);
                // 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);
    } catch (StandardException t) {
        throw noStateChangeException(t);
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) ReaderToUTF8Stream(org.apache.derby.iapi.types.ReaderToUTF8Stream) StringDataValue(org.apache.derby.iapi.types.StringDataValue)

Aggregations

StandardException (org.apache.derby.shared.common.error.StandardException)276 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)43 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)37 IOException (java.io.IOException)32 Properties (java.util.Properties)29 RawTransaction (org.apache.derby.iapi.store.raw.xact.RawTransaction)27 TransactionController (org.apache.derby.iapi.store.access.TransactionController)26 ContextManager (org.apache.derby.iapi.services.context.ContextManager)22 RawContainerHandle (org.apache.derby.iapi.store.raw.data.RawContainerHandle)20 SQLException (java.sql.SQLException)17 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)17 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)16 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)12 RowLocation (org.apache.derby.iapi.types.RowLocation)11 SQLLongint (org.apache.derby.iapi.types.SQLLongint)11 StorageFile (org.apache.derby.io.StorageFile)10 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)9 ScanController (org.apache.derby.iapi.store.access.ScanController)9 File (java.io.File)8 LogInstant (org.apache.derby.iapi.store.raw.log.LogInstant)8