Search in sources :

Example 1 with Message

use of com.mysql.cj.protocol.Message in project aws-mysql-jdbc by awslabs.

the class ClientPreparedStatement method execute.

@Override
public boolean execute() throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        JdbcConnection locallyScopedConn = this.connection;
        if (!this.doPingInstead && !checkReadOnlySafeStatement()) {
            throw SQLError.createSQLException(Messages.getString("PreparedStatement.20") + Messages.getString("PreparedStatement.21"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
        }
        ResultSetInternalMethods rs = null;
        this.lastQueryIsOnDupKeyUpdate = false;
        if (this.retrieveGeneratedKeys) {
            this.lastQueryIsOnDupKeyUpdate = containsOnDuplicateKeyUpdateInSQL();
        }
        this.batchedGeneratedKeys = null;
        resetCancelledState();
        implicitlyCloseAllOpenResults();
        clearWarnings();
        if (this.doPingInstead) {
            doPingInstead();
            return true;
        }
        setupStreamingTimeout(locallyScopedConn);
        Message sendPacket = ((PreparedQuery<?>) this.query).fillSendPacket();
        String oldDb = null;
        if (!locallyScopedConn.getDatabase().equals(this.getCurrentDatabase())) {
            oldDb = locallyScopedConn.getDatabase();
            locallyScopedConn.setDatabase(this.getCurrentDatabase());
        }
        // 
        // Check if we have cached metadata for this query...
        // 
        CachedResultSetMetaData cachedMetadata = null;
        boolean cacheResultSetMetadata = locallyScopedConn.getPropertySet().getBooleanProperty(PropertyKey.cacheResultSetMetadata).getValue();
        if (cacheResultSetMetadata) {
            cachedMetadata = locallyScopedConn.getCachedMetaData(((PreparedQuery<?>) this.query).getOriginalSql());
        }
        // 
        // Only apply max_rows to selects
        // 
        locallyScopedConn.setSessionMaxRows(getParseInfo().getFirstStmtChar() == 'S' ? this.maxRows : -1);
        rs = executeInternal(this.maxRows, sendPacket, createStreamingResultSet(), (getParseInfo().getFirstStmtChar() == 'S'), cachedMetadata, false);
        if (cachedMetadata != null) {
            locallyScopedConn.initializeResultsMetadataFromCache(((PreparedQuery<?>) this.query).getOriginalSql(), cachedMetadata, rs);
        } else {
            if (rs.hasRows() && cacheResultSetMetadata) {
                locallyScopedConn.initializeResultsMetadataFromCache(((PreparedQuery<?>) this.query).getOriginalSql(), null, /* will be created */
                rs);
            }
        }
        if (this.retrieveGeneratedKeys) {
            rs.setFirstCharOfQuery(getParseInfo().getFirstStmtChar());
        }
        if (oldDb != null) {
            locallyScopedConn.setDatabase(oldDb);
        }
        if (rs != null) {
            this.lastInsertId = rs.getUpdateID();
            this.results = rs;
        }
        return ((rs != null) && rs.hasRows());
    }
}
Also used : ResultSetInternalMethods(com.mysql.cj.jdbc.result.ResultSetInternalMethods) Message(com.mysql.cj.protocol.Message) CachedResultSetMetaData(com.mysql.cj.jdbc.result.CachedResultSetMetaData) ClientPreparedQuery(com.mysql.cj.ClientPreparedQuery) PreparedQuery(com.mysql.cj.PreparedQuery)

Example 2 with Message

use of com.mysql.cj.protocol.Message in project aws-mysql-jdbc by awslabs.

the class ClientPreparedStatement method executeQuery.

@Override
public java.sql.ResultSet executeQuery() throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        JdbcConnection locallyScopedConn = this.connection;
        if (!this.doPingInstead) {
            QueryReturnType queryReturnType = getParseInfo().getQueryReturnType();
            if (queryReturnType != QueryReturnType.PRODUCES_RESULT_SET && queryReturnType != QueryReturnType.MAY_PRODUCE_RESULT_SET) {
                throw SQLError.createSQLException(Messages.getString("Statement.57"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
            }
        }
        this.batchedGeneratedKeys = null;
        resetCancelledState();
        implicitlyCloseAllOpenResults();
        clearWarnings();
        if (this.doPingInstead) {
            doPingInstead();
            return this.results;
        }
        setupStreamingTimeout(locallyScopedConn);
        Message sendPacket = ((PreparedQuery<?>) this.query).fillSendPacket();
        String oldDb = null;
        if (!locallyScopedConn.getDatabase().equals(this.getCurrentDatabase())) {
            oldDb = locallyScopedConn.getDatabase();
            locallyScopedConn.setDatabase(this.getCurrentDatabase());
        }
        // 
        // Check if we have cached metadata for this query...
        // 
        CachedResultSetMetaData cachedMetadata = null;
        boolean cacheResultSetMetadata = locallyScopedConn.getPropertySet().getBooleanProperty(PropertyKey.cacheResultSetMetadata).getValue();
        String origSql = ((PreparedQuery<?>) this.query).getOriginalSql();
        if (cacheResultSetMetadata) {
            cachedMetadata = locallyScopedConn.getCachedMetaData(origSql);
        }
        locallyScopedConn.setSessionMaxRows(this.maxRows);
        this.results = executeInternal(this.maxRows, sendPacket, createStreamingResultSet(), true, cachedMetadata, false);
        if (oldDb != null) {
            locallyScopedConn.setDatabase(oldDb);
        }
        if (cachedMetadata != null) {
            locallyScopedConn.initializeResultsMetadataFromCache(origSql, cachedMetadata, this.results);
        } else {
            if (cacheResultSetMetadata) {
                locallyScopedConn.initializeResultsMetadataFromCache(origSql, null, /* will be created */
                this.results);
            }
        }
        this.lastInsertId = this.results.getUpdateID();
        return this.results;
    }
}
Also used : Message(com.mysql.cj.protocol.Message) CachedResultSetMetaData(com.mysql.cj.jdbc.result.CachedResultSetMetaData) QueryReturnType(com.mysql.cj.QueryReturnType) ClientPreparedQuery(com.mysql.cj.ClientPreparedQuery) PreparedQuery(com.mysql.cj.PreparedQuery)

Example 3 with Message

use of com.mysql.cj.protocol.Message in project aws-mysql-jdbc by awslabs.

the class ClientPreparedStatement method executeUpdateInternal.

/**
 * 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 (!isNonResultSetProducingQuery()) {
            throw SQLError.createSQLException(Messages.getString("PreparedStatement.37"), "01S03", this.exceptionInterceptor);
        }
        resetCancelledState();
        implicitlyCloseAllOpenResults();
        ResultSetInternalMethods rs = null;
        Message sendPacket = ((PreparedQuery<?>) this.query).fillSendPacket(bindings);
        String oldDb = null;
        if (!locallyScopedConn.getDatabase().equals(this.getCurrentDatabase())) {
            oldDb = locallyScopedConn.getDatabase();
            locallyScopedConn.setDatabase(this.getCurrentDatabase());
        }
        // 
        // Only apply max_rows to selects
        // 
        locallyScopedConn.setSessionMaxRows(-1);
        rs = executeInternal(-1, sendPacket, false, false, null, isReallyBatch);
        if (this.retrieveGeneratedKeys) {
            rs.setFirstCharOfQuery(getParseInfo().getFirstStmtChar());
        }
        if (oldDb != null) {
            locallyScopedConn.setDatabase(oldDb);
        }
        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;
    }
}
Also used : ResultSetInternalMethods(com.mysql.cj.jdbc.result.ResultSetInternalMethods) Message(com.mysql.cj.protocol.Message) ClientPreparedQuery(com.mysql.cj.ClientPreparedQuery) PreparedQuery(com.mysql.cj.PreparedQuery)

Aggregations

ClientPreparedQuery (com.mysql.cj.ClientPreparedQuery)3 PreparedQuery (com.mysql.cj.PreparedQuery)3 Message (com.mysql.cj.protocol.Message)3 CachedResultSetMetaData (com.mysql.cj.jdbc.result.CachedResultSetMetaData)2 ResultSetInternalMethods (com.mysql.cj.jdbc.result.ResultSetInternalMethods)2 QueryReturnType (com.mysql.cj.QueryReturnType)1