Skip to content
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

Generator: method call conflicts #24

Closed
tamimattafi opened this issue Jun 3, 2024 · 0 comments · Fixed by #25
Closed

Generator: method call conflicts #24

tamimattafi opened this issue Jun 3, 2024 · 0 comments · Fixed by #25

Comments

@tamimattafi
Copy link
Owner

Problem

The current generator uses parameters to generate method names for queries, and doesn't take into account the query itself. This creates conflicts for situations where parameters and return types are the same, but queries are different.

This can be reproduced inside the sample, here's an example from UserDao:

@Query("SELECT * FROM UserEntity WHERE sampleAge < 18")
suspend fun getUnderageUsers(): List<UserEntity>

@Query("SELECT * FROM UserEntity WHERE sampleAge >= 18")
suspend fun getAdultUsers(): List<UserEntity>

@Query("SELECT * FROM UserEntity")
suspend fun getEntitiesReactive(): Flow<List<UserEntity>>

The generated code looks like this:

override suspend fun getUnderageUsers(): List<UserEntity> = transactionWithResult {
    queries.queryUserEntityByNoParameters().awaitAsListIO()
}

override suspend fun getAdultUsers(): List<UserEntity> = transactionWithResult {
    queries.queryUserEntityByNoParameters().awaitAsListIO()
}

override suspend fun getEntitiesReactive(): Flow<List<UserEntity>> =
    queries.queryUserEntityByNoParameters().asFlowIOList()

As you already noticed, all three methods call the same queryUserEntityByNoParameters, even if they have different queries.
The generated queryUserEntityByNoParameters looks like this:

public fun queryUserEntityByNoParameters(): Query<UserEntity> = object :
      Query<UserEntity>(comAttafitamimKabinLocalEntitiesUserUserEntityKabinMapper::map) {
    override fun addListener(listener: Query.Listener) {
      driver.addListener(
      "UserEntity",
      listener = listener
      )
    }

    override fun removeListener(listener: Query.Listener) {
      driver.removeListener(
      "UserEntity",
      listener = listener
      )
    }

    override fun <R> execute(mapper: SqlCursor.() -> QueryResult<R>): QueryResult<R> {
      val kabinQuery = """SELECT * FROM UserEntity WHERE sampleAge < 18"""
      val kabinParametersCount = 0
      val result = driver.executeQuery(
              -1060829723,
              kabinQuery,
              mapper,
              kabinParametersCount
          )
      return result
    }
}

As we see, it's only related to getUnderageUsers, the other two queries were lost.

Solution

Take into account the actual query when generating method names, using some kind of ID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant