Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Groupped statements might not notify queries properly #5001

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

Closed
vitorhugods opened this issue Feb 2, 2024 · 2 comments
Closed

Groupped statements might not notify queries properly #5001

vitorhugods opened this issue Feb 2, 2024 · 2 comments

Comments

@vitorhugods
Copy link
Contributor

vitorhugods commented Feb 2, 2024

SQLDelight Version

2.0.1

Application Operating System

Android, JVM, Native

Describe the Bug

Take the following grouped statements:

updateUser {
UPDATE User
SET name = ?
WHERE qualified_id = ?;
SELECT changes();
}

It generates the following query in Kotlin:

  private inner class UpdateUserQuery<out T : Any>(
    public val name: String?,
    public val qualified_id: QualifiedIDEntity,
    mapper: (SqlCursor) -> T,
  ) : ExecutableQuery<T>(mapper) {
    override fun <R> execute(mapper: (SqlCursor) -> QueryResult<R>): QueryResult<R> =
        transactionWithResult {
      driver.execute(-1_154_160_775, """
          |UPDATE User
          |SET name = ?
          |WHERE qualified_id = ?
          """.trimMargin(), 2) {
            bindString(0, name)
            bindString(10, UserAdapter.qualified_idAdapter.encode(qualified_id))
          }
      driver.executeQuery(-1_154_160_774, """SELECT changes()""", mapper, 0)
    }

    override fun toString(): String = "Users.sq:updateUser"
  }

Even though the User table was changed, there is no notifyQueries(x) { emit("User") }

So, flows won't emit new values when the updateUser query is called.

observeUserById(userId).collect { user ->
    // This won't be called
}

Workaround

Use transactionWithResult and split the query in two:

updateUser:
UPDATE User
SET name = ?
WHERE qualified_id = ?;

selectChanges:
SELECT changes();
userQueries.transactionWithResult {
    userQueries.updateUser(
        name = update.name,
        userId = update.id
    )
    userQueries.selectChanges()
}

I am not sure if this is intended, but it was definitely unexpected and took a bit to figure out what was going on. It definitely isn't mentioned in the Grouping Statements documentation, or in the Coroutines extension documentation

Stacktrace

No response

@vitorhugods
Copy link
Contributor Author

vitorhugods commented Feb 2, 2024

I guess this is a duplicate of:

I'm working on a PR. So far it seems to work, but I'm not sure of some details. I'm investigating.

@dellisd
Copy link
Collaborator

dellisd commented Mar 20, 2025

Fixed in #5006

@dellisd dellisd closed this as completed Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants