-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help: Example of returnGeneratedKeys within a transaction #57
Comments
Putting the transactions aside, I simply use the good 'ol Observable<Long> insertIds = db.update("insert into users...")
.parameters(...)
.returnGeneratedKeys()
.getAs(Long.class)
.first();
Observable<User> newUsers = insertIds.flatMap(id ->
db.select("select from users where id = ?")
.parameter(id)
.getAs(String.class)
) You can also pass the Observable<User> newUsers = db.select("select from users where id = ?")
.parameters(insertIds)
.getAs(String.class) |
You can then compose the appropriate transaction calls into those two |
Thank you. |
I tried a few variations but I still don't know where to put the |
Sorry, I didn't mean compose as in the I don't use the transactions as much, but I think you might do it like this... Observable<Long> insertIds = db.update("insert into users...")
.parameters(...)
.dependsOn(db.beginTransaction())
.returnGeneratedKeys()
.getAs(Long.class)
.first();
Observable<User> newUsers = db.select("select from users where id = ?")
.parameters(insertIds)
.getAs(String.class)
db.commit(newUsers).subscribe(v -> System.out.println("Received item " + v)); |
If I do it that way, with how can I get the result of |
Darn, you're right. Forgive my pseudo-code. One moment... |
I'll show an example later when I get to a computer, but I would use the cache() operator to capture and replay the inserted ids so they are not inserted twice, but can be called for the commit() as well as the select(). |
This is working for me but I'm not sure if it's the right way to do it. return db.update(queryInsert).dependsOn(db.beginTransaction())
.parameters(model.getValues())
.returnGeneratedKeys()
.getAs(Long.class)
.first()
.cache()
.compose(idObs -> db.commit(idObs).flatMap(aBoolean -> idObs))
.flatMap(id -> db.select(querySelect)
.dependsOnLastTransaction()
.parameter(id)
.getAs(String.class)); without return db.update(queryInsert)
.dependsOn(db.beginTransaction())
.parameters(model.getValues())
.returnGeneratedKeys()
.getAs(Long.class)
.first()
.flatMap(id ->
db.select(querySelect).parameter(id).getAs(String.class)
.compose(result -> db.commit(result).flatMap(ok -> result))
); |
I think this should work possibly too... Observable<Integer> insertIds = db.update(queryInsert).dependsOn(db.beginTransaction())
.parameters(model.getValues())
.returnGeneratedKeys()
.getAs(Long.class)
.first()
.cache();
Observable<Boolean> commit = db.commit(insertIds);
db.select(querySelect)
.parameters(insertIds)
.getAs(String.class); |
@gahissy If it works for you, then it is the right way to do it. |
Hello,
I'm trying to chain two queries, an
insert
followed by aselect
using the generated id of the first query as a parameter of the second query.Can you provide an example to achieve that ? Thank you.
Here is my code so far :
The text was updated successfully, but these errors were encountered: