Kotlin Exposed SQL 使用复合主键和 select 查询 Table,所有这些都包含在给定的 DTO 对象列表中

Kotlin Exposed SQL query on Table with compound primary key and select all which are contained in a given List of DTO Objects


考虑以下伪代码:

object EntityTable : Table("ENTITY") {
    val uid = uuid("uid")
    val idCluster = integer("id_cluster")
    val idDataSchema = integer("id_data_schema")
    val value = varchar("value", 1024)

    override val primaryKey = PrimaryKey(uid, idCluster, idDataSchema, name = "ENTITY_PK")
}

var toBeFound = listOf(
    EntityDTO(uid = UUID.fromString("4..9"), idCluster = 1, idDataSchema = 1),
    EntityDTO(uid = UUID.fromString("7..3"), idCluster = 1, idDataSchema = 2),
    EntityDTO(uid = UUID.fromString("6..2"), idCluster = 2, idDataSchema = 1)
)

fun selectManyEntity() : List<EntityDTO> {
    val entityDTOs = transaction {
        val queryResultRows = EntityTable.select {
            (EntityTable.uid, EntityTable.idCluster, EntityTable.idDataSchema) // <-- every row for which the compound key combination of all three
                inList
            toBeFound.map {
                (it.uid, it.idCluster, it.idDataSchema)                        // <-- has an element in 'toBeFound` list with the same compound key combination
            }
        }
        queryResultRows.map { resultRow -> Fillers().newEntityDTO(resultRow) }.toList()
    }
    return entityDTOs
}


我必须如何编写它选择的查询

EntityTable 的所有行(id、idCluster、idDataSchema)的复合主键
也包含在给定的 List 中,假定 List<>
中的每个 EntityDTO 还有字段 id、idCluster、idDataSchema) ???

如果有帮助:EntityDTO 恰好为这三个字段重载了 hash() 和 equals()。

唯一的方法是创建一个复合表达式,如:

fun EntityDTO.searchExpression() = Op.build { 
  (EntityTable.uid eq uid) and (EntityTable.idCluster eq idCluster) and (EntityTable.idDataSchema eq idDataSchema)
}

val fullSearchExpression = toBeFound.map { it.searchExpression() }.compoundOr()

val queryResultRows = EntityTable.select(fullSearchExpression)