Jan Vladimir Mostert
Jan Vladimir Mostert

Reputation: 12972

Extracting data from a JOOQ query when using Jasync

When using JOOQ to create the queries and Jasync to execute the queries, how does one get the RowData back into JOOQ Records?

This is my notransaction helper which does a suspendable non-async query

    override suspend fun <T> notransaction(f: suspend (ConnectionPool<*>) -> T): T {
        val cdf = CompletableDeferred<T>()
        try {
            GlobalScope.launch {
                cdf.complete(f(connectionPool))
            }
        } catch (e: Throwable) {
            log.error(e.message ?: "", e)
            cdf.completeExceptionally(e)
        }
        return cdf.await()
    }

I have defined two tables with their relevant records:


class ListingRecord : CustomRecord<ListingRecord>(listing)
class ListingTable : CustomTable<ListingRecord>(DSL.name("listing")) {

    val id: TableField<ListingRecord, UUID> = createField(DSL.name("id"), SQLDataType.UUID)
    val title: TableField<ListingRecord, String> = createField(DSL.name("title"), SQLDataType.VARCHAR)

    companion object {
        val listing = ListingTable()
    }

    override fun getRecordType(): Class<out ListingRecord> {
        return ListingRecord::class.java
    }

}

class ListingImageRecord : CustomRecord<ListingImageRecord>(listingImage)
class ListingImageTable : CustomTable<ListingImageRecord>(DSL.name("listing_image")) {

    val id: TableField<ListingImageRecord, UUID> = createField(DSL.name("id"), SQLDataType.UUID)
    val name: TableField<ListingImageRecord, String> = createField(DSL.name("name"), SQLDataType.VARCHAR)
    val listingId: TableField<ListingImageRecord, UUID> = createField(DSL.name("listing"), SQLDataType.UUID)

    companion object {
        val listingImage = ListingImageTable()
    }

    override fun getRecordType(): Class<out ListingImageRecord> {
        return ListingImageRecord::class.java
    }

}

Generated an SQL query

    val dsl = DSL.using(SQLDialect.POSTGRES)
    val query = dsl.select(
        listing.id, listing.title,
        listingImage.id, listingImage.listingId
    ).from(listing).leftJoin(listingImage).on(listing.id.eq(listingImage.listingId))

enter image description here

how does one map the RowData back to JOOQ? Is there some way to get a map of columns in the select statement and their indices or some other way to map results back? I'm assuming i need to map it back to a Record somehow so that i can do

listing.id.get(ListingRecord)

Upvotes: 1

Views: 195

Answers (0)

Related Questions