Java Code Examples for com.mysql.cj.jdbc.result.ResultSetInternalMethods#getUpdateCount()

The following examples show how to use com.mysql.cj.jdbc.result.ResultSetInternalMethods#getUpdateCount() . 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: StatementImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected long executeUpdateInternal(String sql, boolean isBatch, boolean returnGeneratedKeys) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        JdbcConnection locallyScopedConn = this.connection;

        checkNullOrEmptyQuery(sql);

        resetCancelledState();

        char firstStatementChar = StringUtils.firstAlphaCharUc(sql, findStartOfStatement(sql));

        this.retrieveGeneratedKeys = returnGeneratedKeys;

        this.lastQueryIsOnDupKeyUpdate = returnGeneratedKeys && firstStatementChar == 'I' && containsOnDuplicateKeyInString(sql);

        ResultSetInternalMethods rs = null;

        if (this.doEscapeProcessing) {
            Object escapedSqlResult = EscapeProcessor.escapeSQL(sql, this.session.getServerSession().getDefaultTimeZone(),
                    this.session.getServerSession().getCapabilities().serverSupportsFracSecs(), getExceptionInterceptor());
            sql = escapedSqlResult instanceof String ? (String) escapedSqlResult : ((EscapeProcessorResult) escapedSqlResult).escapedSql;
        }

        if (locallyScopedConn.isReadOnly(false)) {
            throw SQLError.createSQLException(Messages.getString("Statement.42") + Messages.getString("Statement.43"),
                    MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
        }

        if (StringUtils.startsWithIgnoreCaseAndWs(sql, "select")) {
            throw SQLError.createSQLException(Messages.getString("Statement.46"), "01S03", getExceptionInterceptor());
        }

        implicitlyCloseAllOpenResults();

        // The checking and changing of catalogs must happen in sequence, so synchronize on the same mutex that _conn is using

        CancelQueryTask timeoutTask = null;

        String oldCatalog = null;

        try {
            timeoutTask = startQueryTimer(this, getTimeoutInMillis());

            if (!locallyScopedConn.getCatalog().equals(getCurrentCatalog())) {
                oldCatalog = locallyScopedConn.getCatalog();
                locallyScopedConn.setCatalog(getCurrentCatalog());
            }

            //
            // Only apply max_rows to selects
            //
            locallyScopedConn.setSessionMaxRows(-1);

            statementBegins();

            // null catalog: force read of field info on DML
            rs = ((NativeSession) locallyScopedConn.getSession()).execSQL(this, sql, -1, null, false, getResultSetFactory(), getCurrentCatalog(), null,
                    isBatch);

            if (timeoutTask != null) {
                stopQueryTimer(timeoutTask, true, true);
                timeoutTask = null;
            }

        } catch (CJTimeoutException | OperationCancelledException e) {
            throw SQLExceptionsMapping.translateException(e, this.exceptionInterceptor);

        } finally {
            stopQueryTimer(timeoutTask, false, false);

            if (oldCatalog != null) {
                locallyScopedConn.setCatalog(oldCatalog);
            }

            if (!isBatch) {
                this.query.getStatementExecuting().set(false);
            }
        }

        this.results = rs;

        rs.setFirstCharOfQuery(firstStatementChar);

        this.updateCount = rs.getUpdateCount();

        this.lastInsertId = rs.getUpdateID();

        return this.updateCount;
    }
}
 
Example 2
Source File: ClientPreparedStatement.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Added to allow batch-updates
 * 
 * @param bindings
 * @param isReallyBatch
 * 
 * @return the update count
 * 
 * @throws SQLException
 *             if a database error occurs
 */
protected long executeUpdateInternal(QueryBindings<?> bindings, boolean isReallyBatch) throws SQLException {

    synchronized (checkClosed().getConnectionMutex()) {

        JdbcConnection locallyScopedConn = this.connection;

        if (locallyScopedConn.isReadOnly(false)) {
            throw SQLError.createSQLException(Messages.getString("PreparedStatement.34") + Messages.getString("PreparedStatement.35"),
                    MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
        }

        if ((((PreparedQuery<?>) this.query).getParseInfo().getFirstStmtChar() == 'S') && isSelectQuery()) {
            throw SQLError.createSQLException(Messages.getString("PreparedStatement.37"), "01S03", this.exceptionInterceptor);
        }

        resetCancelledState();

        implicitlyCloseAllOpenResults();

        ResultSetInternalMethods rs = null;

        Message sendPacket = ((PreparedQuery<?>) this.query).fillSendPacket(bindings);

        String oldCatalog = null;

        if (!locallyScopedConn.getCatalog().equals(this.getCurrentCatalog())) {
            oldCatalog = locallyScopedConn.getCatalog();
            locallyScopedConn.setCatalog(this.getCurrentCatalog());
        }

        //
        // Only apply max_rows to selects
        //
        locallyScopedConn.setSessionMaxRows(-1);

        rs = executeInternal(-1, sendPacket, false, false, null, isReallyBatch);

        if (this.retrieveGeneratedKeys) {
            rs.setFirstCharOfQuery(((PreparedQuery<?>) this.query).getParseInfo().getFirstStmtChar());
        }

        if (oldCatalog != null) {
            locallyScopedConn.setCatalog(oldCatalog);
        }

        this.results = rs;

        this.updateCount = rs.getUpdateCount();

        if (containsOnDuplicateKeyUpdateInSQL() && this.compensateForOnDuplicateKeyUpdate) {
            if (this.updateCount == 2 || this.updateCount == 0) {
                this.updateCount = 1;
            }
        }

        this.lastInsertId = rs.getUpdateID();

        return this.updateCount;
    }
}
 
Example 3
Source File: StatementImpl.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
protected long executeUpdateInternal(String sql, boolean isBatch, boolean returnGeneratedKeys) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        JdbcConnection locallyScopedConn = this.connection;

        checkNullOrEmptyQuery(sql);

        resetCancelledState();

        char firstStatementChar = StringUtils.firstAlphaCharUc(sql, findStartOfStatement(sql));

        this.retrieveGeneratedKeys = returnGeneratedKeys;

        this.lastQueryIsOnDupKeyUpdate = returnGeneratedKeys && firstStatementChar == 'I' && containsOnDuplicateKeyInString(sql);

        ResultSetInternalMethods rs = null;

        if (this.doEscapeProcessing) {
            Object escapedSqlResult = EscapeProcessor.escapeSQL(sql, this.session.getServerSession().getDefaultTimeZone(),
                    this.session.getServerSession().getCapabilities().serverSupportsFracSecs(), this.session.getServerSession().isServerTruncatesFracSecs(),
                    getExceptionInterceptor());
            sql = escapedSqlResult instanceof String ? (String) escapedSqlResult : ((EscapeProcessorResult) escapedSqlResult).escapedSql;
        }

        if (locallyScopedConn.isReadOnly(false)) {
            throw SQLError.createSQLException(Messages.getString("Statement.42") + Messages.getString("Statement.43"),
                    MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
        }

        if (StringUtils.startsWithIgnoreCaseAndWs(sql, "select")) {
            throw SQLError.createSQLException(Messages.getString("Statement.46"), "01S03", getExceptionInterceptor());
        }

        implicitlyCloseAllOpenResults();

        // The checking and changing of catalogs must happen in sequence, so synchronize on the same mutex that _conn is using

        CancelQueryTask timeoutTask = null;

        String oldCatalog = null;

        try {
            timeoutTask = startQueryTimer(this, getTimeoutInMillis());

            if (!locallyScopedConn.getCatalog().equals(getCurrentCatalog())) {
                oldCatalog = locallyScopedConn.getCatalog();
                locallyScopedConn.setCatalog(getCurrentCatalog());
            }

            //
            // Only apply max_rows to selects
            //
            locallyScopedConn.setSessionMaxRows(-1);

            statementBegins();

            // null catalog: force read of field info on DML
            rs = ((NativeSession) locallyScopedConn.getSession()).execSQL(this, sql, -1, null, false, getResultSetFactory(), getCurrentCatalog(), null,
                    isBatch);

            if (timeoutTask != null) {
                stopQueryTimer(timeoutTask, true, true);
                timeoutTask = null;
            }

        } catch (CJTimeoutException | OperationCancelledException e) {
            throw SQLExceptionsMapping.translateException(e, this.exceptionInterceptor);

        } finally {
            stopQueryTimer(timeoutTask, false, false);

            if (oldCatalog != null) {
                locallyScopedConn.setCatalog(oldCatalog);
            }

            if (!isBatch) {
                this.query.getStatementExecuting().set(false);
            }
        }

        this.results = rs;

        rs.setFirstCharOfQuery(firstStatementChar);

        this.updateCount = rs.getUpdateCount();

        this.lastInsertId = rs.getUpdateID();

        return this.updateCount;
    }
}
 
Example 4
Source File: ClientPreparedStatement.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Added to allow batch-updates
 * 
 * @param bindings
 *            bindings object
 * @param isReallyBatch
 *            is it a batched statement?
 * 
 * @return the update count
 * 
 * @throws SQLException
 *             if a database error occurs
 */
protected long executeUpdateInternal(QueryBindings<?> bindings, boolean isReallyBatch) throws SQLException {

    synchronized (checkClosed().getConnectionMutex()) {

        JdbcConnection locallyScopedConn = this.connection;

        if (locallyScopedConn.isReadOnly(false)) {
            throw SQLError.createSQLException(Messages.getString("PreparedStatement.34") + Messages.getString("PreparedStatement.35"),
                    MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
        }

        if ((((PreparedQuery<?>) this.query).getParseInfo().getFirstStmtChar() == 'S') && isSelectQuery()) {
            throw SQLError.createSQLException(Messages.getString("PreparedStatement.37"), "01S03", this.exceptionInterceptor);
        }

        resetCancelledState();

        implicitlyCloseAllOpenResults();

        ResultSetInternalMethods rs = null;

        Message sendPacket = ((PreparedQuery<?>) this.query).fillSendPacket(bindings);

        String oldCatalog = null;

        if (!locallyScopedConn.getCatalog().equals(this.getCurrentCatalog())) {
            oldCatalog = locallyScopedConn.getCatalog();
            locallyScopedConn.setCatalog(this.getCurrentCatalog());
        }

        //
        // Only apply max_rows to selects
        //
        locallyScopedConn.setSessionMaxRows(-1);

        rs = executeInternal(-1, sendPacket, false, false, null, isReallyBatch);

        if (this.retrieveGeneratedKeys) {
            rs.setFirstCharOfQuery(((PreparedQuery<?>) this.query).getParseInfo().getFirstStmtChar());
        }

        if (oldCatalog != null) {
            locallyScopedConn.setCatalog(oldCatalog);
        }

        this.results = rs;

        this.updateCount = rs.getUpdateCount();

        if (containsOnDuplicateKeyUpdateInSQL() && this.compensateForOnDuplicateKeyUpdate) {
            if (this.updateCount == 2 || this.updateCount == 0) {
                this.updateCount = 1;
            }
        }

        this.lastInsertId = rs.getUpdateID();

        return this.updateCount;
    }
}