sqldelight icon indicating copy to clipboard operation
sqldelight copied to clipboard

Inserts not emitted to listeners when using grouping statement

Open rsktash opened this issue 2 years ago • 10 comments

SQLDelight Version

2.0.0-alpha02

Operating System

Android

Gradle Version

7.5

Kotlin Version

1.7.20

Dialect

default

AGP Version

No response

Describe the Bug

telegram-cloud-photo-size-2-5267409847173890973-y

telegram-cloud-photo-size-2-5267409847173890974-y

Stacktrace

No response

Gradle Build Script

No response

rsktash avatar Dec 21 '22 18:12 rsktash

There is an extra code generated for normal insert statements

telegram-cloud-photo-size-2-5267409847173890979-y

rsktash avatar Dec 21 '22 18:12 rsktash

I am hitting the same issue when using SQLite 3.35 RETURNING statement in an insert e.g.

INSERT INTO ... VALUES(...) RETURNING _id;

adrientetar avatar Apr 08 '23 16:04 adrientetar

this is intentional, the alternative to whats there right now is returning an observable query which means it will execute the update/insert whenever the underlying table changes. I assume you only want to observe the SELECT or returned type, so you need to make a separate query for the reactive query

AlecKazakova avatar Apr 08 '23 18:04 AlecKazakova

nvm misread

AlecKazakova avatar Apr 08 '23 19:04 AlecKazakova

Is there any plan to fix this issue?

rsktash avatar Sep 05 '23 09:09 rsktash

Hi @griffio 9 months passed and nothing is done? Why it's allowed to insert or upsert into a table if the changes are not notified to listeners?

rsktash avatar Sep 11 '23 08:09 rsktash

🕊️ Sadly, I am not familiar with this issue. Though on quick inspection 🔍 the problem looks to be in SqlDelightQueriesFile

I can have a look and see if there is a fix - I would have to try a few things but can't promise 🎀

🤔 Looks like only when there is a SELECT statement in the group does it not emit the notification. This means that calling notifyQueries only makes sense when used after transaction statements that don't return a QueryResult from a transactionWithResult(). For example - if you look at the generated source code and try manually adding the notifyQueries, where would it go?

griffio avatar Sep 11 '23 11:09 griffio

Thank you for your answer. I know that if the query returns any data it won't notify listeners. But it's a bug. It means "If you want to use a query with returning clause don't use flows".

rsktash avatar Sep 12 '23 11:09 rsktash

Yes - indeed

Currently, that I have tried myself, only a grouping used with a flow like this will work.

Any fix, would also have to work for INSERT INTO ...VALUES(...)RETURNING _id; - we can't generate "nested" transactions for a single statement - maybe it's possible to generate something like...

  transactionWithResult {
      driver.execute(...)
      ...
      driver.executeQuery(...)
    }.also {
            notifyQueries(...) { emit ->
                emit("subscription")
           }
    }

Maybe create a 📣 discussion question so other contributors can see it better ? https://github.com/cashapp/sqldelight/discussions

griffio avatar Sep 13 '23 08:09 griffio

I created a PR, #5006 that should fix it.

I notice now that I also came up with the same idea @griffio brought (using also). Let's see how it goes :)

vitorhugods avatar Feb 05 '24 09:02 vitorhugods