Search code examples
javapostgresqlrx-java

Get result after an update query


I am making the following query which works and updates the info into the database as expected. But is there a way I can get an output from Single < UpdateResult >?

public Single<UpdateResult> update(UpdateEventRequest request) {
    return client.rxUpdateWithParams(
            "UPDATE mydb.sample SET sta_cd=?, some_ts=current_timestamp WHERE id=? RETURNING sta_cd",
            new JsonArray(Arrays.asList(sta_cd, id)));
}

From the following e variable, I was hoping to get the value "10012". But it doesn't seem possible. Tried with map, flatmap and just see options available in e. The only result data in e is 'keys' which is an empty list and 'updated' which is an integer value of 1. My DB is postgres and was expecting results from from Single < UpdateResult > since am using 'RETURNING' in the query.

I have done the same for an insert operation which works but that is via the method rxQueryWithParams() and that returns a Single < ResultSet > instead. Thus wondering if this is even possible. Been having a look at docs and maybe this is not possible as an update query is returning a Single < UpdateResult > . Looking for advice if this is possible, to return data from an update query or a way around this. Please advice. Thanks.

    Single<UpdateResult> result = someClass.update("10012", "78632");

    result.subscribe(
            e -> {
                System.out.println("success: " + e); // I land here as expected
            },
            error -> {
                System.out.println("error: " + error);
            }
    );

Solution

  • Because you are using RETURNING in these commands, treat these INSERT and UPDATE commands as queries.

    Run them through rxQueryWithParams() so you can retrieve the results.

    When you run rxUpdateWithParams(), the UpdateResult contains only the number of rows affected.