Java Code Examples for com.mysql.cj.exceptions.MysqlErrorNumbers#ER_WARN_DATA_OUT_OF_RANGE

The following examples show how to use com.mysql.cj.exceptions.MysqlErrorNumbers#ER_WARN_DATA_OUT_OF_RANGE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NativeProtocol.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Turns output of 'SHOW WARNINGS' into JDBC SQLWarning instances.
 * 
 * If 'forTruncationOnly' is true, only looks for truncation warnings, and
 * actually throws DataTruncation as an exception.
 * 
 * @param warningCountIfKnown
 *            the warning count (if known), otherwise set it to 0.
 * @param forTruncationOnly
 *            if this method should only scan for data truncation warnings
 * 
 * @return the SQLWarning chain (or null if no warnings)
 * 
 * @throws SQLException
 *             if the warnings could not be retrieved, or if data truncation
 *             is being scanned for and truncations were found.
 */
public SQLWarning convertShowWarningsToSQLWarnings(int warningCountIfKnown, boolean forTruncationOnly) {
    SQLWarning currentWarning = null;
    ResultsetRows rows = null;

    try {
        /*
         * +---------+------+---------------------------------------------+
         * | Level ..| Code | Message ....................................|
         * +---------+------+---------------------------------------------+
         * | Warning | 1265 | Data truncated for column 'field1' at row 1 |
         * +---------+------+---------------------------------------------+
         */
        NativePacketPayload resultPacket = sendCommand(this.commandBuilder.buildComQuery(getSharedSendPacket(), "SHOW WARNINGS"), false, 0);

        Resultset warnRs = readAllResults(-1, warningCountIfKnown > 99 /* stream large warning counts */, resultPacket, false, null,
                new ResultsetFactory(Type.FORWARD_ONLY, Concurrency.READ_ONLY));

        int codeFieldIndex = warnRs.getColumnDefinition().findColumn("Code", false, 1) - 1;
        int messageFieldIndex = warnRs.getColumnDefinition().findColumn("Message", false, 1) - 1;
        String enc = warnRs.getColumnDefinition().getFields()[messageFieldIndex].getEncoding();

        ValueFactory<String> svf = new StringValueFactory(enc);
        ValueFactory<Integer> ivf = new IntegerValueFactory();

        rows = warnRs.getRows();
        Row r;
        while ((r = rows.next()) != null) {

            int code = r.getValue(codeFieldIndex, ivf);

            if (forTruncationOnly) {
                if (code == MysqlErrorNumbers.ER_WARN_DATA_TRUNCATED || code == MysqlErrorNumbers.ER_WARN_DATA_OUT_OF_RANGE) {
                    DataTruncation newTruncation = new MysqlDataTruncation(r.getValue(messageFieldIndex, svf), 0, false, false, 0, 0, code);

                    if (currentWarning == null) {
                        currentWarning = newTruncation;
                    } else {
                        currentWarning.setNextWarning(newTruncation);
                    }
                }
            } else {
                //String level = warnRs.getString("Level"); 
                String message = r.getValue(messageFieldIndex, svf);

                SQLWarning newWarning = new SQLWarning(message, MysqlErrorNumbers.mysqlToSqlState(code), code);
                if (currentWarning == null) {
                    currentWarning = newWarning;
                } else {
                    currentWarning.setNextWarning(newWarning);
                }
            }
        }

        if (forTruncationOnly && (currentWarning != null)) {
            throw ExceptionFactory.createException(currentWarning.getMessage(), currentWarning);
        }

        return currentWarning;
    } catch (IOException ex) {
        throw ExceptionFactory.createException(ex.getMessage(), ex);
    } finally {
        if (rows != null) {
            rows.close();
        }
    }
}
 
Example 2
Source File: NativeProtocol.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Turns output of 'SHOW WARNINGS' into JDBC SQLWarning instances.
 * 
 * If 'forTruncationOnly' is true, only looks for truncation warnings, and
 * actually throws DataTruncation as an exception.
 * 
 * @param warningCountIfKnown
 *            the warning count (if known), otherwise set it to 0.
 * @param forTruncationOnly
 *            if this method should only scan for data truncation warnings
 * 
 * @return the SQLWarning chain (or null if no warnings)
 */
public SQLWarning convertShowWarningsToSQLWarnings(int warningCountIfKnown, boolean forTruncationOnly) {
    SQLWarning currentWarning = null;
    ResultsetRows rows = null;

    try {
        /*
         * +---------+------+---------------------------------------------+
         * | Level ..| Code | Message ....................................|
         * +---------+------+---------------------------------------------+
         * | Warning | 1265 | Data truncated for column 'field1' at row 1 |
         * +---------+------+---------------------------------------------+
         */
        NativePacketPayload resultPacket = sendCommand(this.commandBuilder.buildComQuery(getSharedSendPacket(), "SHOW WARNINGS"), false, 0);

        Resultset warnRs = readAllResults(-1, warningCountIfKnown > 99 /* stream large warning counts */, resultPacket, false, null,
                new ResultsetFactory(Type.FORWARD_ONLY, Concurrency.READ_ONLY));

        int codeFieldIndex = warnRs.getColumnDefinition().findColumn("Code", false, 1) - 1;
        int messageFieldIndex = warnRs.getColumnDefinition().findColumn("Message", false, 1) - 1;
        String enc = warnRs.getColumnDefinition().getFields()[messageFieldIndex].getEncoding();

        ValueFactory<String> svf = new StringValueFactory(enc);
        ValueFactory<Integer> ivf = new IntegerValueFactory();

        rows = warnRs.getRows();
        Row r;
        while ((r = rows.next()) != null) {

            int code = r.getValue(codeFieldIndex, ivf);

            if (forTruncationOnly) {
                if (code == MysqlErrorNumbers.ER_WARN_DATA_TRUNCATED || code == MysqlErrorNumbers.ER_WARN_DATA_OUT_OF_RANGE) {
                    DataTruncation newTruncation = new MysqlDataTruncation(r.getValue(messageFieldIndex, svf), 0, false, false, 0, 0, code);

                    if (currentWarning == null) {
                        currentWarning = newTruncation;
                    } else {
                        currentWarning.setNextWarning(newTruncation);
                    }
                }
            } else {
                //String level = warnRs.getString("Level"); 
                String message = r.getValue(messageFieldIndex, svf);

                SQLWarning newWarning = new SQLWarning(message, MysqlErrorNumbers.mysqlToSqlState(code), code);
                if (currentWarning == null) {
                    currentWarning = newWarning;
                } else {
                    currentWarning.setNextWarning(newWarning);
                }
            }
        }

        if (forTruncationOnly && (currentWarning != null)) {
            throw ExceptionFactory.createException(currentWarning.getMessage(), currentWarning);
        }

        return currentWarning;
    } catch (IOException ex) {
        throw ExceptionFactory.createException(ex.getMessage(), ex);
    } finally {
        if (rows != null) {
            rows.close();
        }
    }
}