Search in sources :

Example 1 with MysqlDataTruncation

use of com.mysql.cj.jdbc.exceptions.MysqlDataTruncation in project ABC by RuiPinto96274.

the class NativeProtocol method convertShowWarningsToSQLWarnings.

/**
 * 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(getCommandBuilder().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;
        ValueFactory<String> svf = new StringValueFactory(this.propertySet);
        ValueFactory<Integer> ivf = new IntegerValueFactory(this.propertySet);
        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();
        }
    }
}
Also used : SQLWarning(java.sql.SQLWarning) StringValueFactory(com.mysql.cj.result.StringValueFactory) MysqlDataTruncation(com.mysql.cj.jdbc.exceptions.MysqlDataTruncation) IntegerValueFactory(com.mysql.cj.result.IntegerValueFactory) LazyString(com.mysql.cj.util.LazyString) IOException(java.io.IOException) ResultsetRows(com.mysql.cj.protocol.ResultsetRows) DataTruncation(java.sql.DataTruncation) MysqlDataTruncation(com.mysql.cj.jdbc.exceptions.MysqlDataTruncation) Resultset(com.mysql.cj.protocol.Resultset) ResultsetRow(com.mysql.cj.protocol.ResultsetRow) Row(com.mysql.cj.result.Row)

Example 2 with MysqlDataTruncation

use of com.mysql.cj.jdbc.exceptions.MysqlDataTruncation in project JavaSegundasQuintas by ecteruel.

the class NativeProtocol method convertShowWarningsToSQLWarnings.

/**
 * 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(getCommandBuilder().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;
        ValueFactory<String> svf = new StringValueFactory(this.propertySet);
        ValueFactory<Integer> ivf = new IntegerValueFactory(this.propertySet);
        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();
        }
    }
}
Also used : SQLWarning(java.sql.SQLWarning) StringValueFactory(com.mysql.cj.result.StringValueFactory) MysqlDataTruncation(com.mysql.cj.jdbc.exceptions.MysqlDataTruncation) IntegerValueFactory(com.mysql.cj.result.IntegerValueFactory) LazyString(com.mysql.cj.util.LazyString) IOException(java.io.IOException) ResultsetRows(com.mysql.cj.protocol.ResultsetRows) DataTruncation(java.sql.DataTruncation) MysqlDataTruncation(com.mysql.cj.jdbc.exceptions.MysqlDataTruncation) Resultset(com.mysql.cj.protocol.Resultset) ResultsetRow(com.mysql.cj.protocol.ResultsetRow) Row(com.mysql.cj.result.Row)

Example 3 with MysqlDataTruncation

use of com.mysql.cj.jdbc.exceptions.MysqlDataTruncation in project aws-mysql-jdbc by awslabs.

the class NativeProtocol method convertShowWarningsToSQLWarnings.

/**
 * 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 forTruncationOnly
 *            if this method should only scan for data truncation warnings
 *
 * @return the SQLWarning chain (or null if no warnings)
 */
public SQLWarning convertShowWarningsToSQLWarnings(boolean forTruncationOnly) {
    if (this.warningCount == 0) {
        return null;
    }
    SQLWarning currentWarning = null;
    ResultsetRows rows = null;
    try {
        /*
             * +---------+------+---------------------------------------------+
             * | Level ..| Code | Message ....................................|
             * +---------+------+---------------------------------------------+
             * | Warning | 1265 | Data truncated for column 'field1' at row 1 |
             * +---------+------+---------------------------------------------+
             */
        NativePacketPayload resultPacket = sendCommand(getCommandBuilder().buildComQuery(getSharedSendPacket(), "SHOW WARNINGS"), false, 0);
        Resultset warnRs = readAllResults(-1, this.warningCount > 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;
        ValueFactory<String> svf = new StringValueFactory(this.propertySet);
        ValueFactory<Integer> ivf = new IntegerValueFactory(this.propertySet);
        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();
        }
    }
}
Also used : SQLWarning(java.sql.SQLWarning) StringValueFactory(com.mysql.cj.result.StringValueFactory) MysqlDataTruncation(com.mysql.cj.jdbc.exceptions.MysqlDataTruncation) IntegerValueFactory(com.mysql.cj.result.IntegerValueFactory) LazyString(com.mysql.cj.util.LazyString) IOException(java.io.IOException) ResultsetRows(com.mysql.cj.protocol.ResultsetRows) DataTruncation(java.sql.DataTruncation) MysqlDataTruncation(com.mysql.cj.jdbc.exceptions.MysqlDataTruncation) Resultset(com.mysql.cj.protocol.Resultset) ResultsetRow(com.mysql.cj.protocol.ResultsetRow) Row(com.mysql.cj.result.Row)

Example 4 with MysqlDataTruncation

use of com.mysql.cj.jdbc.exceptions.MysqlDataTruncation in project aws-mysql-jdbc by awslabs.

the class ExceptionsTest method testConstructors.

@Test
public void testConstructors() {
    new CommunicationsException(TEST_MESSAGE, new Throwable());
    new CommunicationsException((JdbcConnection) this.conn, new PacketSentTimeHolder() {
    }, new PacketReceivedTimeHolder() {
    }, new Exception());
    new ConnectionFeatureNotAvailableException(TEST_MESSAGE, new Throwable());
    new ConnectionFeatureNotAvailableException((JdbcConnection) this.conn, new PacketSentTimeHolder() {
    }, new Exception());
    new MysqlDataTruncation(TEST_MESSAGE, 0, false, false, 0, 0, 0);
    new MySQLQueryInterruptedException();
    new MySQLQueryInterruptedException(TEST_MESSAGE);
    new MySQLQueryInterruptedException(TEST_MESSAGE, TEST_SQL_STATE);
    new MySQLQueryInterruptedException(TEST_MESSAGE, TEST_SQL_STATE, 0);
    new MySQLStatementCancelledException();
    new MySQLStatementCancelledException(TEST_MESSAGE);
    new MySQLStatementCancelledException(TEST_MESSAGE, TEST_SQL_STATE);
    new MySQLStatementCancelledException(TEST_MESSAGE, TEST_SQL_STATE, 0);
    new MySQLTimeoutException();
    new MySQLTimeoutException(TEST_MESSAGE);
    new MySQLTimeoutException(TEST_MESSAGE, TEST_SQL_STATE);
    new MySQLTimeoutException(TEST_MESSAGE, TEST_SQL_STATE, 0);
    new MySQLTransactionRollbackException();
    new MySQLTransactionRollbackException(TEST_MESSAGE);
    new MySQLTransactionRollbackException(TEST_MESSAGE, TEST_SQL_STATE);
    new MySQLTransactionRollbackException(TEST_MESSAGE, TEST_SQL_STATE, 0);
    new NotUpdatable(TEST_MESSAGE);
    new OperationNotSupportedException();
    new OperationNotSupportedException(TEST_MESSAGE);
    new PacketTooBigException(TEST_MESSAGE);
    new PacketTooBigException(0, 100);
    new SQLError();
}
Also used : OperationNotSupportedException(com.mysql.cj.jdbc.exceptions.OperationNotSupportedException) PacketTooBigException(com.mysql.cj.jdbc.exceptions.PacketTooBigException) ConnectionFeatureNotAvailableException(com.mysql.cj.jdbc.exceptions.ConnectionFeatureNotAvailableException) MysqlDataTruncation(com.mysql.cj.jdbc.exceptions.MysqlDataTruncation) MySQLTransactionRollbackException(com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException) PacketReceivedTimeHolder(com.mysql.cj.protocol.PacketReceivedTimeHolder) MySQLStatementCancelledException(com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException) MySQLQueryInterruptedException(com.mysql.cj.jdbc.exceptions.MySQLQueryInterruptedException) OperationNotSupportedException(com.mysql.cj.jdbc.exceptions.OperationNotSupportedException) ConnectionFeatureNotAvailableException(com.mysql.cj.jdbc.exceptions.ConnectionFeatureNotAvailableException) MySQLTransactionRollbackException(com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException) SQLException(java.sql.SQLException) PacketTooBigException(com.mysql.cj.jdbc.exceptions.PacketTooBigException) MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) CommunicationsException(com.mysql.cj.jdbc.exceptions.CommunicationsException) NotUpdatable(com.mysql.cj.jdbc.exceptions.NotUpdatable) MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) SQLError(com.mysql.cj.jdbc.exceptions.SQLError) PacketSentTimeHolder(com.mysql.cj.protocol.PacketSentTimeHolder) MySQLStatementCancelledException(com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException) CommunicationsException(com.mysql.cj.jdbc.exceptions.CommunicationsException) MySQLQueryInterruptedException(com.mysql.cj.jdbc.exceptions.MySQLQueryInterruptedException) Test(org.junit.jupiter.api.Test)

Example 5 with MysqlDataTruncation

use of com.mysql.cj.jdbc.exceptions.MysqlDataTruncation in project ABC by RuiPinto96274.

the class ExceptionsTest method testConstructors.

@Test
public void testConstructors() {
    new CommunicationsException(TEST_MESSAGE, new Throwable());
    new CommunicationsException((JdbcConnection) this.conn, new PacketSentTimeHolder() {
    }, new PacketReceivedTimeHolder() {
    }, new Exception());
    new ConnectionFeatureNotAvailableException(TEST_MESSAGE, new Throwable());
    new ConnectionFeatureNotAvailableException((JdbcConnection) this.conn, new PacketSentTimeHolder() {
    }, new Exception());
    new MysqlDataTruncation(TEST_MESSAGE, 0, false, false, 0, 0, 0);
    new MySQLQueryInterruptedException();
    new MySQLQueryInterruptedException(TEST_MESSAGE);
    new MySQLQueryInterruptedException(TEST_MESSAGE, TEST_SQL_STATE);
    new MySQLQueryInterruptedException(TEST_MESSAGE, TEST_SQL_STATE, 0);
    new MySQLStatementCancelledException();
    new MySQLStatementCancelledException(TEST_MESSAGE);
    new MySQLStatementCancelledException(TEST_MESSAGE, TEST_SQL_STATE);
    new MySQLStatementCancelledException(TEST_MESSAGE, TEST_SQL_STATE, 0);
    new MySQLTimeoutException();
    new MySQLTimeoutException(TEST_MESSAGE);
    new MySQLTimeoutException(TEST_MESSAGE, TEST_SQL_STATE);
    new MySQLTimeoutException(TEST_MESSAGE, TEST_SQL_STATE, 0);
    new MySQLTransactionRollbackException();
    new MySQLTransactionRollbackException(TEST_MESSAGE);
    new MySQLTransactionRollbackException(TEST_MESSAGE, TEST_SQL_STATE);
    new MySQLTransactionRollbackException(TEST_MESSAGE, TEST_SQL_STATE, 0);
    new NotUpdatable(TEST_MESSAGE);
    new OperationNotSupportedException();
    new OperationNotSupportedException(TEST_MESSAGE);
    new PacketTooBigException(TEST_MESSAGE);
    new PacketTooBigException(0, 100);
    new SQLError();
}
Also used : OperationNotSupportedException(com.mysql.cj.jdbc.exceptions.OperationNotSupportedException) PacketTooBigException(com.mysql.cj.jdbc.exceptions.PacketTooBigException) ConnectionFeatureNotAvailableException(com.mysql.cj.jdbc.exceptions.ConnectionFeatureNotAvailableException) MysqlDataTruncation(com.mysql.cj.jdbc.exceptions.MysqlDataTruncation) MySQLTransactionRollbackException(com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException) PacketReceivedTimeHolder(com.mysql.cj.protocol.PacketReceivedTimeHolder) MySQLStatementCancelledException(com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException) MySQLQueryInterruptedException(com.mysql.cj.jdbc.exceptions.MySQLQueryInterruptedException) OperationNotSupportedException(com.mysql.cj.jdbc.exceptions.OperationNotSupportedException) ConnectionFeatureNotAvailableException(com.mysql.cj.jdbc.exceptions.ConnectionFeatureNotAvailableException) MySQLTransactionRollbackException(com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException) SQLException(java.sql.SQLException) PacketTooBigException(com.mysql.cj.jdbc.exceptions.PacketTooBigException) MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) CommunicationsException(com.mysql.cj.jdbc.exceptions.CommunicationsException) NotUpdatable(com.mysql.cj.jdbc.exceptions.NotUpdatable) MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) SQLError(com.mysql.cj.jdbc.exceptions.SQLError) PacketSentTimeHolder(com.mysql.cj.protocol.PacketSentTimeHolder) MySQLStatementCancelledException(com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException) CommunicationsException(com.mysql.cj.jdbc.exceptions.CommunicationsException) MySQLQueryInterruptedException(com.mysql.cj.jdbc.exceptions.MySQLQueryInterruptedException) Test(org.junit.jupiter.api.Test)

Aggregations

MysqlDataTruncation (com.mysql.cj.jdbc.exceptions.MysqlDataTruncation)6 CommunicationsException (com.mysql.cj.jdbc.exceptions.CommunicationsException)3 ConnectionFeatureNotAvailableException (com.mysql.cj.jdbc.exceptions.ConnectionFeatureNotAvailableException)3 MySQLQueryInterruptedException (com.mysql.cj.jdbc.exceptions.MySQLQueryInterruptedException)3 MySQLStatementCancelledException (com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException)3 MySQLTimeoutException (com.mysql.cj.jdbc.exceptions.MySQLTimeoutException)3 MySQLTransactionRollbackException (com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException)3 NotUpdatable (com.mysql.cj.jdbc.exceptions.NotUpdatable)3 OperationNotSupportedException (com.mysql.cj.jdbc.exceptions.OperationNotSupportedException)3 PacketTooBigException (com.mysql.cj.jdbc.exceptions.PacketTooBigException)3 SQLError (com.mysql.cj.jdbc.exceptions.SQLError)3 PacketReceivedTimeHolder (com.mysql.cj.protocol.PacketReceivedTimeHolder)3 PacketSentTimeHolder (com.mysql.cj.protocol.PacketSentTimeHolder)3 Resultset (com.mysql.cj.protocol.Resultset)3 ResultsetRow (com.mysql.cj.protocol.ResultsetRow)3 ResultsetRows (com.mysql.cj.protocol.ResultsetRows)3 IntegerValueFactory (com.mysql.cj.result.IntegerValueFactory)3 Row (com.mysql.cj.result.Row)3 StringValueFactory (com.mysql.cj.result.StringValueFactory)3 LazyString (com.mysql.cj.util.LazyString)3