问题描述
考虑以下伪代码:
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"),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中的每个EntityDTO,也包含在给定List中。
也有字段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)