Search in sources :

Example 1 with ServerPreparedQueryBindings

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

Aggregations

ServerPreparedQueryBindValue (com.mysql.cj.ServerPreparedQueryBindValue)1 ServerPreparedQueryBindings (com.mysql.cj.ServerPreparedQueryBindings)1 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1