pushtorefresh / storio

Reactive API for SQLiteDatabase and ContentResolver.
Apache License 2.0
2.55k stars 182 forks source link

Make a query to update multiple rows by different values #805

Closed nikiJava closed 7 years ago

nikiJava commented 7 years ago

As I mention in title, what I have to use to update multiple rows by different values? For example, I have table like this:

 class Language {
        private String name;
        private int status;
    }

I want to change status of one of the languages to 0, and in another - to 2, using one Query (UpdateQuery or something else).

Originnally I wanted to use two UpdateQuery instances - the one reset language to 0, the second - set 2 in another row, but did not find how to concatenate PutResults of those querries.

Thanks!

nikitin-da commented 7 years ago

Hi, @nikiJava ! I think using 2 different queries for 2 different values is the right way. Don't you use storIO.put().objects(Collection) or storIO.put().contentValueses(Iterable) to put them? In common case one of values may already exist in db unlike another. So the first PutResult will be for update, and the second for insertion. And you can combine it with PutResults like this

nikiJava commented 7 years ago

Guys, I'm gonna use one custom PutResolver to perform operation, that was mention in my first comment in the issue - change status of one of the languages to 0, and in another - to 2. So, how can I merge that PutResults from two different operation, cause method performPut have to return PutResult, but not a PutResults.

nikitin-da commented 7 years ago

PutResolver should be used to map single object into contentValues. So if you want to put two object you can use storIO.put().objects(Collection) Will it applicable in your case?

Language l1 = new Language("en", 0);
Language l2 = new Language("ru", 2);
PutResults results = storIO.put()
   .objects(list(l1, l2))
   .prepare()
   .executeAsBlocking();
Log.d("English put result: ", results.results().get("en").wasInserted() + "");
nikitin-da commented 7 years ago

Closing. Feel free to reopen if storIO.put().objects(Collection) doesn't work in your case