Search in sources :

Example 1 with ResultsetRows

use of com.mysql.cj.protocol.ResultsetRows 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 ResultsetRows

use of com.mysql.cj.protocol.ResultsetRows in project ABC by RuiPinto96274.

the class TextResultsetReader method read.

@Override
public Resultset read(int maxRows, boolean streamResults, NativePacketPayload resultPacket, ColumnDefinition metadata, ProtocolEntityFactory<Resultset, NativePacketPayload> resultSetFactory) throws IOException {
    Resultset rs = null;
    // try {
    long columnCount = resultPacket.readInteger(IntegerDataType.INT_LENENC);
    if (columnCount > 0) {
        // Build a result set with rows.
        // Read in the column information
        ColumnDefinition cdef = this.protocol.read(ColumnDefinition.class, new ColumnDefinitionFactory(columnCount, metadata));
        // There is no EOF packet after fields when CLIENT_DEPRECATE_EOF is set
        if (!this.protocol.getServerSession().isEOFDeprecated()) {
            this.protocol.skipPacket();
        // this.protocol.readServerStatusForResultSets(this.protocol.readPacket(this.protocol.getReusablePacket()), true);
        }
        ResultsetRows rows = null;
        if (!streamResults) {
            TextRowFactory trf = new TextRowFactory(this.protocol, cdef, resultSetFactory.getResultSetConcurrency(), false);
            ArrayList<ResultsetRow> rowList = new ArrayList<>();
            ResultsetRow row = this.protocol.read(ResultsetRow.class, trf);
            while (row != null) {
                if ((maxRows == -1) || (rowList.size() < maxRows)) {
                    rowList.add(row);
                }
                row = this.protocol.read(ResultsetRow.class, trf);
            }
            rows = new ResultsetRowsStatic(rowList, cdef);
        } else {
            rows = new ResultsetRowsStreaming<>(this.protocol, cdef, false, resultSetFactory);
            this.protocol.setStreamingData(rows);
        }
        /*
             * Build ResultSet from ResultsetRows
             */
        rs = resultSetFactory.createFromProtocolEntity(rows);
    } else {
        // check for file request
        if (columnCount == NativePacketPayload.NULL_LENGTH) {
            String charEncoding = this.protocol.getPropertySet().getStringProperty(PropertyKey.characterEncoding).getValue();
            String fileName = resultPacket.readString(StringSelfDataType.STRING_TERM, this.protocol.getServerSession().getCharsetSettings().doesPlatformDbCharsetMatches() ? charEncoding : null);
            resultPacket = this.protocol.sendFileToServer(fileName);
        }
        /*
             * Build ResultSet with no ResultsetRows
             */
        // read and parse OK packet
        // oldStatus set in sendCommand()
        OkPacket ok = this.protocol.readServerStatusForResultSets(resultPacket, false);
        rs = resultSetFactory.createFromProtocolEntity(ok);
    }
    return rs;
// } catch (IOException ioEx) {
// throw SQLError.createCommunicationsException(this.protocol.getConnection(), this.protocol.getPacketSentTimeHolder().getLastPacketSentTime(),
// this.protocol.getPacketReceivedTimeHolder().getLastPacketReceivedTime(), ioEx, this.protocol.getExceptionInterceptor());
// }
}
Also used : OkPacket(com.mysql.cj.protocol.a.result.OkPacket) ArrayList(java.util.ArrayList) ResultsetRows(com.mysql.cj.protocol.ResultsetRows) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) ResultsetRow(com.mysql.cj.protocol.ResultsetRow) ResultsetRowsStatic(com.mysql.cj.protocol.a.result.ResultsetRowsStatic) Resultset(com.mysql.cj.protocol.Resultset)

Example 3 with ResultsetRows

use of com.mysql.cj.protocol.ResultsetRows 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 4 with ResultsetRows

use of com.mysql.cj.protocol.ResultsetRows in project JavaSegundasQuintas by ecteruel.

the class BinaryResultsetReader method read.

@Override
public Resultset read(int maxRows, boolean streamResults, NativePacketPayload resultPacket, ColumnDefinition metadata, ProtocolEntityFactory<Resultset, NativePacketPayload> resultSetFactory) throws IOException {
    Resultset rs = null;
    // try {
    long columnCount = resultPacket.readInteger(IntegerDataType.INT_LENENC);
    if (columnCount > 0) {
        // Build a result set with rows.
        // Read in the column information
        ColumnDefinition cdef = this.protocol.read(ColumnDefinition.class, new MergingColumnDefinitionFactory(columnCount, metadata));
        boolean isCursorPossible = this.protocol.getPropertySet().getBooleanProperty(PropertyKey.useCursorFetch).getValue() && resultSetFactory.getResultSetType() == Type.FORWARD_ONLY && resultSetFactory.getFetchSize() > 0;
        // If CLIENT_DEPRECATE_EOF is set, there is no way to tell which one, OK or ResultsetRow, is the next packet, so it should be read with a special caching method.
        if (isCursorPossible || !this.protocol.getServerSession().isEOFDeprecated()) {
            // Read the next packet but leave it in the reader cache. In case it's not the OK or EOF one it will be read again by ResultSet factories.
            NativePacketPayload rowPacket = this.protocol.probeMessage(this.protocol.getReusablePacket());
            this.protocol.checkErrorMessage(rowPacket);
            if (rowPacket.isResultSetOKPacket() || rowPacket.isEOFPacket()) {
                // Consume the OK/EOF packet from the reader cache and read the status flags from it;
                // The SERVER_STATUS_CURSOR_EXISTS flag should indicate the cursor state in this case.
                rowPacket = this.protocol.readMessage(this.protocol.getReusablePacket());
                this.protocol.readServerStatusForResultSets(rowPacket, true);
            } else {
                // If it's not an OK/EOF then the cursor is not created and this recent packet is a row.
                // Retain the packet in the reader cache.
                isCursorPossible = false;
            }
        }
        ResultsetRows rows = null;
        if (isCursorPossible && this.protocol.getServerSession().cursorExists()) {
            rows = new ResultsetRowsCursor(this.protocol, cdef);
        } else if (!streamResults) {
            BinaryRowFactory brf = new BinaryRowFactory(this.protocol, cdef, resultSetFactory.getResultSetConcurrency(), false);
            ArrayList<ResultsetRow> rowList = new ArrayList<>();
            ResultsetRow row = this.protocol.read(ResultsetRow.class, brf);
            while (row != null) {
                if ((maxRows == -1) || (rowList.size() < maxRows)) {
                    rowList.add(row);
                }
                row = this.protocol.read(ResultsetRow.class, brf);
            }
            rows = new ResultsetRowsStatic(rowList, cdef);
        } else {
            rows = new ResultsetRowsStreaming<>(this.protocol, cdef, true, resultSetFactory);
            this.protocol.setStreamingData(rows);
        }
        /*
             * Build ResultSet from ResultsetRows
             */
        rs = resultSetFactory.createFromProtocolEntity(rows);
    } else {
        // check for file request
        if (columnCount == NativePacketPayload.NULL_LENGTH) {
            String charEncoding = this.protocol.getPropertySet().getStringProperty(PropertyKey.characterEncoding).getValue();
            String fileName = resultPacket.readString(StringSelfDataType.STRING_TERM, this.protocol.getServerSession().getCharsetSettings().doesPlatformDbCharsetMatches() ? null : charEncoding);
            resultPacket = this.protocol.sendFileToServer(fileName);
        }
        /*
             * Build ResultSet with no ResultsetRows
             */
        // read and parse OK packet
        // oldStatus set in sendCommand()
        OkPacket ok = this.protocol.readServerStatusForResultSets(resultPacket, false);
        rs = resultSetFactory.createFromProtocolEntity(ok);
    }
    return rs;
}
Also used : OkPacket(com.mysql.cj.protocol.a.result.OkPacket) ArrayList(java.util.ArrayList) ResultsetRows(com.mysql.cj.protocol.ResultsetRows) ResultsetRowsStreaming(com.mysql.cj.protocol.a.result.ResultsetRowsStreaming) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) ResultsetRow(com.mysql.cj.protocol.ResultsetRow) ResultsetRowsCursor(com.mysql.cj.protocol.a.result.ResultsetRowsCursor) ResultsetRowsStatic(com.mysql.cj.protocol.a.result.ResultsetRowsStatic) Resultset(com.mysql.cj.protocol.Resultset)

Example 5 with ResultsetRows

use of com.mysql.cj.protocol.ResultsetRows 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)

Aggregations

Resultset (com.mysql.cj.protocol.Resultset)9 ResultsetRow (com.mysql.cj.protocol.ResultsetRow)9 ResultsetRows (com.mysql.cj.protocol.ResultsetRows)9 ColumnDefinition (com.mysql.cj.protocol.ColumnDefinition)6 OkPacket (com.mysql.cj.protocol.a.result.OkPacket)6 ResultsetRowsStatic (com.mysql.cj.protocol.a.result.ResultsetRowsStatic)6 ArrayList (java.util.ArrayList)6 MysqlDataTruncation (com.mysql.cj.jdbc.exceptions.MysqlDataTruncation)3 ResultsetRowsCursor (com.mysql.cj.protocol.a.result.ResultsetRowsCursor)3 ResultsetRowsStreaming (com.mysql.cj.protocol.a.result.ResultsetRowsStreaming)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 IOException (java.io.IOException)3 DataTruncation (java.sql.DataTruncation)3 SQLWarning (java.sql.SQLWarning)3