how to get a key from rxjava insert query in vert.

2019-08-25 02:32发布

I have an insert method in my vert.x project written in rxjava style. In my method I run an insert query to insert the record in ms sql server. I want to get the auto incremented key of newly inserted record. How I can get it?

Here is my code.

 @Override
public Single<Record> save(Record record) {
    return new AsyncResultSingle<Record>(resultHandler -> {
        jdbcClient.rxGetConnection()
        .subscribe(connection -> {
            String sql = "INSERT into record (ani, template_id) values (?, ?)";
            JsonArray params = new JsonArray().add(record.ani).add(record.templateId);
            connection.rxQueryWithParams(sql, params)
            .doAfterTerminate(connection::close)
            .subscribe(resultSet -> {
                List<JsonObject> rows = resultSet.getRows();
                //how I can get a key or Record object which has been inserted?
                resultHandler.handle(Future.succeededFuture());
            }, onError -> {
                resultHandler.handle(Future.failedFuture(onError));
            });
        }, onError -> {
            resultHandler.handle(Future.failedFuture(onError));
        });
    });
}

2条回答
乱世女痞
2楼-- · 2019-08-25 03:12

I think, instead of

List<JsonObject> rows = resultSet.getRows();

you are looking for

List<JsonArray> results = resultSet.getResults();

EDIT: you already figured it out, but to correct my answer here: you also have to use rxUpdateWithParams instead of rxQueryWithParams.

查看更多
看我几分像从前
3楼-- · 2019-08-25 03:13

After I have changed

 connection.rxQueryWithParams(sql, params)

to

connection.rxUpdateWithParams(sql, params)

then, I am able to get the key as;

 JsonArray rows = resultSet.getKeys();

Thank you @taygetos for the hint.

查看更多
登录 后发表回答