Search in sources :

Example 1 with ServerPreparedQueryBindValue

use of com.mysql.cj.ServerPreparedQueryBindValue in project JavaSegundasQuintas by ecteruel.

the class ServerPreparedStatement method executeBatchSerially.

@Override
protected long[] executeBatchSerially(int batchTimeout) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        JdbcConnection locallyScopedConn = this.connection;
        if (locallyScopedConn.isReadOnly()) {
            throw SQLError.createSQLException(Messages.getString("ServerPreparedStatement.2") + Messages.getString("ServerPreparedStatement.3"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
        }
        clearWarnings();
        // Store this for later, we're going to 'swap' them out
        // as we execute each batched statement...
        ServerPreparedQueryBindValue[] oldBindValues = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
        try {
            long[] updateCounts = null;
            if (this.query.getBatchedArgs() != null) {
                int nbrCommands = this.query.getBatchedArgs().size();
                updateCounts = new long[nbrCommands];
                if (this.retrieveGeneratedKeys) {
                    this.batchedGeneratedKeys = new ArrayList<>(nbrCommands);
                }
                for (int i = 0; i < nbrCommands; i++) {
                    updateCounts[i] = -3;
                }
                SQLException sqlEx = null;
                int commandIndex = 0;
                ServerPreparedQueryBindValue[] previousBindValuesForBatch = null;
                CancelQueryTask timeoutTask = null;
                try {
                    timeoutTask = startQueryTimer(this, batchTimeout);
                    for (commandIndex = 0; commandIndex < nbrCommands; commandIndex++) {
                        Object arg = this.query.getBatchedArgs().get(commandIndex);
                        try {
                            if (arg instanceof String) {
                                updateCounts[commandIndex] = executeUpdateInternal((String) arg, true, this.retrieveGeneratedKeys);
                                // limit one generated key per OnDuplicateKey statement
                                getBatchedGeneratedKeys(this.results.getFirstCharOfQuery() == 'I' && containsOnDuplicateKeyInString((String) arg) ? 1 : 0);
                            } else {
                                ((ServerPreparedQuery) this.query).setQueryBindings((ServerPreparedQueryBindings) arg);
                                ServerPreparedQueryBindValue[] parameterBindings = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
                                if (previousBindValuesForBatch != null) {
                                    for (int j = 0; j < parameterBindings.length; j++) {
                                        if (parameterBindings[j].bufferType != previousBindValuesForBatch[j].bufferType) {
                                            ((ServerPreparedQuery) this.query).getQueryBindings().getSendTypesToServer().set(true);
                                            break;
                                        }
                                    }
                                }
                                try {
                                    updateCounts[commandIndex] = executeUpdateInternal(false, true);
                                } finally {
                                    previousBindValuesForBatch = parameterBindings;
                                }
                                // limit one generated key per OnDuplicateKey statement
                                getBatchedGeneratedKeys(containsOnDuplicateKeyUpdateInSQL() ? 1 : 0);
                            }
                        } catch (SQLException ex) {
                            updateCounts[commandIndex] = EXECUTE_FAILED;
                            if (this.continueBatchOnError && !(ex instanceof MySQLTimeoutException) && !(ex instanceof MySQLStatementCancelledException) && !hasDeadlockOrTimeoutRolledBackTx(ex)) {
                                sqlEx = ex;
                            } else {
                                long[] newUpdateCounts = new long[commandIndex];
                                System.arraycopy(updateCounts, 0, newUpdateCounts, 0, commandIndex);
                                throw SQLError.createBatchUpdateException(ex, newUpdateCounts, this.exceptionInterceptor);
                            }
                        }
                    }
                } finally {
                    stopQueryTimer(timeoutTask, false, false);
                    resetCancelledState();
                }
                if (sqlEx != null) {
                    throw SQLError.createBatchUpdateException(sqlEx, updateCounts, this.exceptionInterceptor);
                }
            }
            return (updateCounts != null) ? updateCounts : new long[0];
        } finally {
            ((ServerPreparedQuery) this.query).getQueryBindings().setBindValues(oldBindValues);
            ((ServerPreparedQuery) this.query).getQueryBindings().getSendTypesToServer().set(true);
            clearBatch();
        }
    }
}
Also used : SQLException(java.sql.SQLException) ServerPreparedQueryBindValue(com.mysql.cj.ServerPreparedQueryBindValue) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) CancelQueryTask(com.mysql.cj.CancelQueryTask) MySQLStatementCancelledException(com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException)

Example 2 with ServerPreparedQueryBindValue

use of com.mysql.cj.ServerPreparedQueryBindValue in project JavaSegundasQuintas by ecteruel.

the class ServerPreparedStatement method asSql.

@Override
public String asSql(boolean quoteStreamsAndUnknowns) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        ClientPreparedStatement pStmtForSub = null;
        try {
            pStmtForSub = ClientPreparedStatement.getInstance(this.connection, ((PreparedQuery<?>) this.query).getOriginalSql(), this.getCurrentDatabase());
            int numParameters = ((PreparedQuery<?>) pStmtForSub.query).getParameterCount();
            int ourNumParameters = ((PreparedQuery<?>) this.query).getParameterCount();
            ServerPreparedQueryBindValue[] parameterBindings = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
            for (int i = 0; (i < numParameters) && (i < ourNumParameters); i++) {
                if (parameterBindings[i] != null) {
                    if (parameterBindings[i].isNull()) {
                        pStmtForSub.setNull(i + 1, MysqlType.NULL);
                    } else {
                        ServerPreparedQueryBindValue bindValue = parameterBindings[i];
                        // 
                        switch(bindValue.bufferType) {
                            case MysqlType.FIELD_TYPE_TINY:
                                pStmtForSub.setByte(i + 1, ((Long) bindValue.value).byteValue());
                                break;
                            case MysqlType.FIELD_TYPE_SHORT:
                                pStmtForSub.setShort(i + 1, ((Long) bindValue.value).shortValue());
                                break;
                            case MysqlType.FIELD_TYPE_LONG:
                                pStmtForSub.setInt(i + 1, ((Long) bindValue.value).intValue());
                                break;
                            case MysqlType.FIELD_TYPE_LONGLONG:
                                pStmtForSub.setLong(i + 1, ((Long) bindValue.value).longValue());
                                break;
                            case MysqlType.FIELD_TYPE_FLOAT:
                                pStmtForSub.setFloat(i + 1, ((Float) bindValue.value).floatValue());
                                break;
                            case MysqlType.FIELD_TYPE_DOUBLE:
                                pStmtForSub.setDouble(i + 1, ((Double) bindValue.value).doubleValue());
                                break;
                            default:
                                pStmtForSub.setObject(i + 1, parameterBindings[i].value);
                                break;
                        }
                    }
                }
            }
            return pStmtForSub.asSql(quoteStreamsAndUnknowns);
        } finally {
            if (pStmtForSub != null) {
                try {
                    pStmtForSub.close();
                } catch (SQLException sqlEx) {
                // ignore
                }
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) PreparedQuery(com.mysql.cj.PreparedQuery) ServerPreparedQueryBindValue(com.mysql.cj.ServerPreparedQueryBindValue)

Example 3 with ServerPreparedQueryBindValue

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

the class ServerPreparedStatement method executeBatchSerially.

@Override
protected long[] executeBatchSerially(int batchTimeout) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        JdbcConnection locallyScopedConn = this.connection;
        if (locallyScopedConn.isReadOnly()) {
            throw SQLError.createSQLException(Messages.getString("ServerPreparedStatement.2") + Messages.getString("ServerPreparedStatement.3"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
        }
        clearWarnings();
        // Store this for later, we're going to 'swap' them out
        // as we execute each batched statement...
        ServerPreparedQueryBindValue[] oldBindValues = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
        try {
            long[] updateCounts = null;
            if (this.query.getBatchedArgs() != null) {
                int nbrCommands = this.query.getBatchedArgs().size();
                updateCounts = new long[nbrCommands];
                if (this.retrieveGeneratedKeys) {
                    this.batchedGeneratedKeys = new ArrayList<>(nbrCommands);
                }
                for (int i = 0; i < nbrCommands; i++) {
                    updateCounts[i] = -3;
                }
                SQLException sqlEx = null;
                int commandIndex = 0;
                ServerPreparedQueryBindValue[] previousBindValuesForBatch = null;
                CancelQueryTask timeoutTask = null;
                try {
                    timeoutTask = startQueryTimer(this, batchTimeout);
                    for (commandIndex = 0; commandIndex < nbrCommands; commandIndex++) {
                        Object arg = this.query.getBatchedArgs().get(commandIndex);
                        try {
                            if (arg instanceof String) {
                                updateCounts[commandIndex] = executeUpdateInternal((String) arg, true, this.retrieveGeneratedKeys);
                                // limit one generated key per OnDuplicateKey statement
                                getBatchedGeneratedKeys(this.results.getFirstCharOfQuery() == 'I' && containsOnDuplicateKeyInString((String) arg) ? 1 : 0);
                            } else {
                                ((ServerPreparedQuery) this.query).setQueryBindings((ServerPreparedQueryBindings) arg);
                                ServerPreparedQueryBindValue[] parameterBindings = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
                                if (previousBindValuesForBatch != null) {
                                    for (int j = 0; j < parameterBindings.length; j++) {
                                        if (parameterBindings[j].bufferType != previousBindValuesForBatch[j].bufferType) {
                                            ((ServerPreparedQuery) this.query).getQueryBindings().getSendTypesToServer().set(true);
                                            break;
                                        }
                                    }
                                }
                                try {
                                    updateCounts[commandIndex] = executeUpdateInternal(false, true);
                                } finally {
                                    previousBindValuesForBatch = parameterBindings;
                                }
                                // limit one generated key per OnDuplicateKey statement
                                getBatchedGeneratedKeys(containsOnDuplicateKeyUpdateInSQL() ? 1 : 0);
                            }
                        } catch (SQLException ex) {
                            updateCounts[commandIndex] = EXECUTE_FAILED;
                            if (this.continueBatchOnError && !(ex instanceof MySQLTimeoutException) && !(ex instanceof MySQLStatementCancelledException) && !hasDeadlockOrTimeoutRolledBackTx(ex)) {
                                sqlEx = ex;
                            } else {
                                long[] newUpdateCounts = new long[commandIndex];
                                System.arraycopy(updateCounts, 0, newUpdateCounts, 0, commandIndex);
                                throw SQLError.createBatchUpdateException(ex, newUpdateCounts, this.exceptionInterceptor);
                            }
                        }
                    }
                } finally {
                    stopQueryTimer(timeoutTask, false, false);
                    resetCancelledState();
                }
                if (sqlEx != null) {
                    throw SQLError.createBatchUpdateException(sqlEx, updateCounts, this.exceptionInterceptor);
                }
            }
            return (updateCounts != null) ? updateCounts : new long[0];
        } finally {
            ((ServerPreparedQuery) this.query).getQueryBindings().setBindValues(oldBindValues);
            ((ServerPreparedQuery) this.query).getQueryBindings().getSendTypesToServer().set(true);
            clearBatch();
        }
    }
}
Also used : SQLException(java.sql.SQLException) ServerPreparedQueryBindValue(com.mysql.cj.ServerPreparedQueryBindValue) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) CancelQueryTask(com.mysql.cj.CancelQueryTask) MySQLStatementCancelledException(com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException)

Example 4 with ServerPreparedQueryBindValue

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

the class ServerPreparedStatement method setOneBatchedParameterSet.

@Override
protected int setOneBatchedParameterSet(java.sql.PreparedStatement batchedStatement, int batchedParamIndex, Object paramSet) throws SQLException {
    ServerPreparedQueryBindValue[] paramArg = ((ServerPreparedQueryBindings) paramSet).getBindValues();
    for (int j = 0; j < paramArg.length; j++) {
        if (paramArg[j].isNull()) {
            batchedStatement.setNull(batchedParamIndex++, MysqlType.NULL.getJdbcType());
        } else {
            if (paramArg[j].isStream()) {
                Object value = paramArg[j].value;
                if (value instanceof byte[]) {
                    batchedStatement.setBytes(batchedParamIndex++, (byte[]) value);
                } else if (value instanceof InputStream) {
                    batchedStatement.setBinaryStream(batchedParamIndex++, (InputStream) value, paramArg[j].getStreamLength());
                } else if (value instanceof java.sql.Blob) {
                    try {
                        batchedStatement.setBinaryStream(batchedParamIndex++, ((java.sql.Blob) value).getBinaryStream(), paramArg[j].getStreamLength());
                    } catch (Throwable t) {
                        throw ExceptionFactory.createException(t.getMessage(), this.session.getExceptionInterceptor());
                    }
                } else if (value instanceof Reader) {
                    batchedStatement.setCharacterStream(batchedParamIndex++, (Reader) value, paramArg[j].getStreamLength());
                } else {
                    throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("ServerPreparedStatement.18") + value.getClass().getName() + "'", this.session.getExceptionInterceptor());
                }
            } else {
                switch(paramArg[j].bufferType) {
                    case MysqlType.FIELD_TYPE_TINY:
                        batchedStatement.setByte(batchedParamIndex++, ((Long) paramArg[j].value).byteValue());
                        break;
                    case MysqlType.FIELD_TYPE_SHORT:
                        batchedStatement.setShort(batchedParamIndex++, ((Long) paramArg[j].value).shortValue());
                        break;
                    case MysqlType.FIELD_TYPE_LONG:
                        batchedStatement.setInt(batchedParamIndex++, ((Long) paramArg[j].value).intValue());
                        break;
                    case MysqlType.FIELD_TYPE_LONGLONG:
                        batchedStatement.setLong(batchedParamIndex++, ((Long) paramArg[j].value).longValue());
                        break;
                    case MysqlType.FIELD_TYPE_FLOAT:
                        batchedStatement.setFloat(batchedParamIndex++, ((Float) paramArg[j].value).floatValue());
                        break;
                    case MysqlType.FIELD_TYPE_DOUBLE:
                        batchedStatement.setDouble(batchedParamIndex++, ((Double) paramArg[j].value).doubleValue());
                        break;
                    case MysqlType.FIELD_TYPE_TIME:
                        batchedStatement.setTime(batchedParamIndex++, (Time) paramArg[j].value);
                        break;
                    case MysqlType.FIELD_TYPE_DATE:
                        batchedStatement.setObject(batchedParamIndex++, paramArg[j].value, MysqlType.DATE);
                        break;
                    case MysqlType.FIELD_TYPE_DATETIME:
                        batchedStatement.setObject(batchedParamIndex++, paramArg[j].value);
                        break;
                    case MysqlType.FIELD_TYPE_TIMESTAMP:
                        batchedStatement.setTimestamp(batchedParamIndex++, (Timestamp) paramArg[j].value);
                        break;
                    case MysqlType.FIELD_TYPE_VAR_STRING:
                    case MysqlType.FIELD_TYPE_STRING:
                    case MysqlType.FIELD_TYPE_VARCHAR:
                    case MysqlType.FIELD_TYPE_DECIMAL:
                    case MysqlType.FIELD_TYPE_NEWDECIMAL:
                        Object value = paramArg[j].value;
                        if (value instanceof byte[]) {
                            batchedStatement.setBytes(batchedParamIndex, (byte[]) value);
                        } else {
                            batchedStatement.setString(batchedParamIndex, (String) value);
                        }
                        if (batchedStatement instanceof ServerPreparedStatement) {
                            ServerPreparedQueryBindValue asBound = ((ServerPreparedStatement) batchedStatement).getBinding(batchedParamIndex, false);
                            asBound.bufferType = paramArg[j].bufferType;
                        }
                        batchedParamIndex++;
                        break;
                    default:
                        throw new IllegalArgumentException(Messages.getString("ServerPreparedStatement.26", new Object[] { batchedParamIndex }));
                }
            }
        }
    }
    return batchedParamIndex;
}
Also used : InputStream(java.io.InputStream) ServerPreparedQueryBindings(com.mysql.cj.ServerPreparedQueryBindings) Reader(java.io.Reader) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) ServerPreparedQueryBindValue(com.mysql.cj.ServerPreparedQueryBindValue)

Example 5 with ServerPreparedQueryBindValue

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

the class ServerPreparedStatement method asSql.

@Override
public String asSql(boolean quoteStreamsAndUnknowns) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        ClientPreparedStatement pStmtForSub = null;
        try {
            pStmtForSub = ClientPreparedStatement.getInstance(this.connection, ((PreparedQuery<?>) this.query).getOriginalSql(), this.getCurrentDatabase());
            int numParameters = ((PreparedQuery<?>) pStmtForSub.query).getParameterCount();
            int ourNumParameters = ((PreparedQuery<?>) this.query).getParameterCount();
            ServerPreparedQueryBindValue[] parameterBindings = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
            for (int i = 0; (i < numParameters) && (i < ourNumParameters); i++) {
                if (parameterBindings[i] != null) {
                    if (parameterBindings[i].isNull()) {
                        pStmtForSub.setNull(i + 1, MysqlType.NULL);
                    } else {
                        ServerPreparedQueryBindValue bindValue = parameterBindings[i];
                        // 
                        switch(bindValue.bufferType) {
                            case MysqlType.FIELD_TYPE_TINY:
                                pStmtForSub.setByte(i + 1, ((Long) bindValue.value).byteValue());
                                break;
                            case MysqlType.FIELD_TYPE_SHORT:
                                pStmtForSub.setShort(i + 1, ((Long) bindValue.value).shortValue());
                                break;
                            case MysqlType.FIELD_TYPE_LONG:
                                pStmtForSub.setInt(i + 1, ((Long) bindValue.value).intValue());
                                break;
                            case MysqlType.FIELD_TYPE_LONGLONG:
                                pStmtForSub.setLong(i + 1, ((Long) bindValue.value).longValue());
                                break;
                            case MysqlType.FIELD_TYPE_FLOAT:
                                pStmtForSub.setFloat(i + 1, ((Float) bindValue.value).floatValue());
                                break;
                            case MysqlType.FIELD_TYPE_DOUBLE:
                                pStmtForSub.setDouble(i + 1, ((Double) bindValue.value).doubleValue());
                                break;
                            default:
                                pStmtForSub.setObject(i + 1, parameterBindings[i].value);
                                break;
                        }
                    }
                }
            }
            return pStmtForSub.asSql(quoteStreamsAndUnknowns);
        } finally {
            if (pStmtForSub != null) {
                try {
                    pStmtForSub.close();
                } catch (SQLException sqlEx) {
                // ignore
                }
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) PreparedQuery(com.mysql.cj.PreparedQuery) ServerPreparedQueryBindValue(com.mysql.cj.ServerPreparedQueryBindValue)

Aggregations

ServerPreparedQueryBindValue (com.mysql.cj.ServerPreparedQueryBindValue)9 ServerPreparedQuery (com.mysql.cj.ServerPreparedQuery)6 SQLException (java.sql.SQLException)6 CancelQueryTask (com.mysql.cj.CancelQueryTask)3 PreparedQuery (com.mysql.cj.PreparedQuery)3 ServerPreparedQueryBindings (com.mysql.cj.ServerPreparedQueryBindings)3 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)3 MySQLStatementCancelledException (com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException)3 MySQLTimeoutException (com.mysql.cj.jdbc.exceptions.MySQLTimeoutException)3 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3