-
Notifications
You must be signed in to change notification settings - Fork 541
Notify queries when executing group statements with result #5006
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
Conversation
Any updates on this? |
Hello! We have migrated to SQLDelight following the Realm deprecation, but this is a major issue for us. This bug makes it much more complex to build a reactive app that responds to upserted data. We have many nested relationships in our data and using the SQLite upsert makes it easy to establish these relationships using the returned row ID while hiding whether a new row was added or an existing row updated (since it doesn't actually matter for most of our app). To get live updates to the DB data without this bug fixed, we will have to explicitly query a row, check if it's null, call an insert query if missing and then grab the last inserted rowid via another query; and if it's not null, grab the existing key and perform an update query instead for every single entity relationship we have. Being able to return the inserted/updated key hides this complexity from the app and keeps DB operations neatly in the SQL. Any updates on this PR would be really appreciated! |
I think this approach makes sense to me.
This isn't something we need to be concerned about. Kotlinpoet will output what it outputs, it doesn't need to be pretty-printed.
The Query ID is a hash based on the name of the query and is stable between runs. Getting it from the compiler is "ideal" in the sense that if we were to ever change the way these values were hashed, we wouldn't need to go through and update dozens of tests. In this case, you can just get it from The query ID refers to the whole grouped query and is used to help dedupe notifications in transactions where the grouped query could be invoked multiple times, so there's no extra work needed to isolate it to one or either of the statements. |
If we can get this rebased and the tests fixed up, I think this is ready to merge 👍 |
The space is because control flow is the wrong mechanism to use. But it's good enough for now, and produces close enough output. |
Rebased. Also replaced with hardcoded IDs with |
| Package | Type | Package file | Manager | Update | Change | |---|---|---|---|---|---| | [com.github.jsqlparser:jsqlparser](https://github.com/JSQLParser/JSqlParser) | dependencies | misk/gradle/libs.versions.toml | gradle | minor | `5.2` -> `5.3` | | [app.cash.sqldelight](https://github.com/sqldelight/sqldelight) | plugin | misk/gradle/libs.versions.toml | gradle | minor | `2.0.2` -> `2.1.0` | | [app.cash.sqldelight:runtime](https://github.com/sqldelight/sqldelight) | dependencies | misk/gradle/libs.versions.toml | gradle | minor | `2.0.2` -> `2.1.0` | | [app.cash.sqldelight:mysql-dialect](https://github.com/sqldelight/sqldelight) | dependencies | misk/gradle/libs.versions.toml | gradle | minor | `2.0.2` -> `2.1.0` | | [app.cash.sqldelight:jdbc-driver](https://github.com/sqldelight/sqldelight) | dependencies | misk/gradle/libs.versions.toml | gradle | minor | `2.0.2` -> `2.1.0` | | [software.amazon.awssdk:sdk-core](https://aws.amazon.com/sdkforjava) | dependencies | misk/gradle/libs.versions.toml | gradle | patch | `2.31.44` -> `2.31.45` | | [software.amazon.awssdk:sqs](https://aws.amazon.com/sdkforjava) | dependencies | misk/gradle/libs.versions.toml | gradle | patch | `2.31.44` -> `2.31.45` | | [software.amazon.awssdk:dynamodb-enhanced](https://aws.amazon.com/sdkforjava) | dependencies | misk/gradle/libs.versions.toml | gradle | patch | `2.31.44` -> `2.31.45` | | [software.amazon.awssdk:dynamodb](https://aws.amazon.com/sdkforjava) | dependencies | misk/gradle/libs.versions.toml | gradle | patch | `2.31.44` -> `2.31.45` | | [software.amazon.awssdk:aws-core](https://aws.amazon.com/sdkforjava) | dependencies | misk/gradle/libs.versions.toml | gradle | patch | `2.31.44` -> `2.31.45` | | [software.amazon.awssdk:bom](https://aws.amazon.com/sdkforjava) | dependencies | misk/gradle/libs.versions.toml | gradle | patch | `2.31.44` -> `2.31.45` | | [software.amazon.awssdk:auth](https://aws.amazon.com/sdkforjava) | dependencies | misk/gradle/libs.versions.toml | gradle | patch | `2.31.44` -> `2.31.45` | --- ### Release Notes <details> <summary>sqldelight/sqldelight (app.cash.sqldelight)</summary> ### [`v2.1.0`](https://github.com/sqldelight/sqldelight/blob/HEAD/CHANGELOG.md#210---2025-05-16) [Compare Source](sqldelight/sqldelight@2.0.2...2.1.0) ##### Added - \[WASM Driver] Add support for wasmJs to web worker driver ([#​5534](sqldelight/sqldelight#5534) by \[Ilya Gulya]\[IlyaGulya]) - \[PostgreSQL Dialect] Support PostgreSql UnNest Array to rows ([#​5673](sqldelight/sqldelight#5673) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql TSRANGE/TSTZRANGE support ([#​5297](sqldelight/sqldelight#5297) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql Right Full Join ([#​5086](sqldelight/sqldelight#5086) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] Postrgesql extract from temporal types ([#​5273](sqldelight/sqldelight#5273) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql array contains operators ([#​4933](sqldelight/sqldelight#4933) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql drop constraint ([#​5288](sqldelight/sqldelight#5288) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] Postgresql type casting ([#​5089](sqldelight/sqldelight#5089) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql lateral join operator for subquery ([#​5122](sqldelight/sqldelight#5122) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] Postgresql ILIKE operator ([#​5330](sqldelight/sqldelight#5330) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql XML type ([#​5331](sqldelight/sqldelight#5331) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql AT TIME ZONE ([#​5243](sqldelight/sqldelight#5243) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] Support postgresql order by nulls ([#​5199](sqldelight/sqldelight#5199) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] Add PostgreSQL current date/time function support ([#​5226](sqldelight/sqldelight#5226) by \[Drew Dobson]\[drewd]) - \[PostgreSQL Dialect] PostgreSql Regex operators ([#​5137](sqldelight/sqldelight#5137) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] add brin gist ([#​5059](sqldelight/sqldelight#5059) by \[Griffio]\[griffio]) - \[MySQL Dialect] Support RENAME INDEX for MySql dialect ([#​5212](sqldelight/sqldelight#5212) by \[Oren Kislev]\[orenkislev-faire]) - \[JSON Extension] Add alias to json table function ([#​5372](sqldelight/sqldelight#5372) by \[Griffio]\[griffio]) ##### Changed - \[Compiler] Generated query files return row counts for simple mutators ([#​4578](sqldelight/sqldelight#4578) by \[Marius Volkhart]\[MariusV]) - \[Native Driver] Update NativeSqlDatabase.kt to change readonly flag for DELETE, INSERT, and UPDATE statements ([#​5680](sqldelight/sqldelight#5680) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] Change PgInterval to String ([#​5403](sqldelight/sqldelight#5403) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] Support SqlDelight modules to implement PostgreSql extensions ([#​5677](sqldelight/sqldelight#5677) by \[Griffio]\[griffio]) ##### Fixed - \[Compiler] fix: notify queries when executing group statements with result ([#​5006](sqldelight/sqldelight#5006) by \[Vitor Hugo Schwaab]\[vitorhugods]) - \[Compiler] Fix SqlDelightModule type resolver ([#​5625](sqldelight/sqldelight#5625) by \[Griffio]\[griffio]) - \[Compiler] Fix 5501 insert object escaped column ([#​5503](sqldelight/sqldelight#5503) by \[Griffio]\[griffio]) - \[Compiler] Compiler: Improve error message such that path links are clickable with the correct line & char position. ([#​5604](sqldelight/sqldelight#5604) by \[Niklas Baudy]\[vanniktech]) - \[Compiler] Fix issue 5298: allow keywords to be used as table names - \[Compiler] fix named executes and add test - \[Compiler] Consider foreign key table constraints when sorting initialization statements ([#​5325](sqldelight/sqldelight#5325) by \[Leon Linhart]\[TheMrMilchmann]) - \[Compiler] Align error underlines properly when tabs are involved ([#​5224](sqldelight/sqldelight#5224) by \[Drew Dobson]\[drewd]) - \[JDBC Driver] Fix memory leak for connectionManager during end of transaction - \[JDBC Driver] Run SQLite migrations inside transaction as mentioned in documentation ([#​5218](sqldelight/sqldelight#5218) by \[Lukáš Moravec]\[morki]) - \[JDBC Driver] Fix leaking connections after transaction commit / rollback ([#​5205](sqldelight/sqldelight#5205) by \[Lukáš Moravec]\[morki]) - \[Gradle Plugin] Execute `DriverInitializer` before `GenerateSchemaTask` ([#​5562](sqldelight/sqldelight#5562) by \[Emeka Nwagu]\[nwagu]) - \[Runtime] Fix crash in LogSqliteDriver when real driver is Async ([#​5723](sqldelight/sqldelight#5723) by \[Eric Denman]\[edenman]) - \[Runtime] Fix StringBuilder capacity ([#​5192](sqldelight/sqldelight#5192) by \[Jan Bína]\[janbina]) - \[PostgreSQL Dialect] PostgreSql create or replace view ([#​5407](sqldelight/sqldelight#5407) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] Postgresql to_json ([#​5606](sqldelight/sqldelight#5606) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql numeric resolver ([#​5399](sqldelight/sqldelight#5399) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] sqlite windows function ([#​2799](sqldelight/sqldelight#2799) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql SELECT DISTINCT ON ([#​5345](sqldelight/sqldelight#5345) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] alter table add column if not exists ([#​5309](sqldelight/sqldelight#5309) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] Postgresql async bind parameter ([#​5313](sqldelight/sqldelight#5313) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql boolean literals ([#​5262](sqldelight/sqldelight#5262) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql window functions ([#​5155](sqldelight/sqldelight#5155) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql isNull isNotNull types ([#​5173](sqldelight/sqldelight#5173) by \[Griffio]\[griffio]) - \[PostgreSQL Dialect] PostgreSql select distinct ([#​5172](sqldelight/sqldelight#5172) by \[Griffio]\[griffio]) - \[Paging Extension] paging refresh initial load fix ([#​5615](sqldelight/sqldelight#5615) by \[Eva]\[evant]) - \[Paging Extension] Add MacOS native targets ([#​5324](sqldelight/sqldelight#5324) by \[Vitor Hugo Schwaab]\[vitorhugods]) - \[IntelliJ Plugin] K2 Support </details> --- ### Configuration 📅 **Schedule**: Branch creation - "after 6pm every weekday,before 2am every weekday" in timezone Australia/Melbourne, Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Never, or you tick the rebase/retry checkbox. 👻 **Immortal**: This PR will be recreated if closed unmerged. Get [config help](https://github.com/renovatebot/renovate/discussions) if that's undesired. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). GitOrigin-RevId: 2c67f3c06e13d86329db5149abfceba98e3701a4
Fixes:
INSERT INTO ...VALUES(...) RETURNING id;
not working with coroutines flow #4975Proposed solution
By modifying the
QueryGenerator
to make sure all child classes have access totablesUpdated()
, and centralising the query notification into theexecuteBlock
function, make sure the following is added to the generated code of grouped statements.I am not completely sure of this approach and I'd love some feedback before I dedicate more time into it.
To be solved yet
Annoying space before
.also {
. I'll learn a bit more of Kotlinpoet :)Questions
notifyQueriesBlock()
to accept the QueryId and make it match one of the statements.Remark
I am not sure about the
executeBlock
. I find its control flow a bit hard to follow and I'd love to simplify a bit and maybe link theQueryGenerator
's implementations more closely to implement it depending on what the query looks like, instead of having too many ifs and elses.