1

当使用 JOOQ 创建查询并使用 Jasync 执行查询时,如何将 RowData 恢复到 JOOQ 记录中?

这是我的 notransaction 助手,它执行可暂停的非异步查询

    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()
    }

我已经定义了两个表及其相关记录:


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
    }

}

生成了一个 SQL 查询

    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))

在此处输入图像描述

如何将 RowData 映射回 JOOQ?有什么方法可以在 select 语句中获取列的映射及其索引,还是有其他方法可以将结果映射回来?我假设我需要以某种方式将其映射回记录,以便我可以做到

listing.id.get(ListingRecord)
4

0 回答 0