Skip to content

R2dbcConverter should provide read functionality that accepts alias prefixes for columns #448

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

Open
pmaedel opened this issue Sep 7, 2020 · 1 comment
Labels
type: enhancement A general enhancement

Comments

@pmaedel
Copy link

pmaedel commented Sep 7, 2020

There is an example project available here

I used the r2dbc mapping functionality to map joined entities via alias prefixes:

    override suspend fun findByIdJoined(id: Long): Fruit? {

        return databaseClient.execute("select f.*, t.id as t_id, t.kind as t_kind from fruit f left join tree t on t.id = f.tree_id where f.id = :id")
            .bind("id", id)
            .map { row, rowMetaData ->
                val fruitEntity = converter.read(Fruit::class.java, row, rowMetaData)
                val joinedTreeEntity = converter.read<Tree>(row, "t_")
                fruitEntity.copy(joinedTree = joinedTreeEntity)
            }.awaitFirstOrNull()
    }

    private inline fun <reified T> R2dbcConverter.read(row: Row, aliasPrefix: String): T = read(T::class.java,
        object : Row {
            override fun <T : Any?> get(index: Int, type: Class<T>) = row[index, type]
            override fun <T : Any?> get(name: String, type: Class<T>) = row["$aliasPrefix$name", type]
        }
    )

I can easily map the root entity (tree) via read(type, row, rowMetadata). Mapping the aliased joined entity (fruit) however seems only to be possible by hacking a customized read functionality, which would better be provided via R2dbcConverter. I have seen that functionality for reading values with column prefixes is available internally via MappingR2dbcConverter#readFrom but could not find a way to make use of it for my use case.

@mp911de
Copy link
Member

mp911de commented Sep 8, 2020

We already use a prefix internally and with #288, we plan to extend on reading objects with a prefix applied so it makes sense to surface the functionality a bit more. We should extend EntityRowMapper and R2dbcConverter to accept both column prefixes.

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

No branches or pull requests

2 participants