在我在应用程序中的一个查询中,我刚刚意识到Slick在select中添加了这个add case
语句。
以下是我构建查询的方法:
def search(searchCriteria: SearchCriteria,
drop: Long = 0,
take: Long = 100): Future[Seq[(School, Option[Address])]] = {
val query = Schools.schools joinLeft
Addresses.addresses on ((s, a) => s.addressId === a.id && a.deletedAt.isEmpty)
val q = for {
(school, address) <- query.filter{ case (s, a) =>
List(
Some(s.deletedAt.isEmpty),
searchCriteria.school.name.map(n => s.name.toLowerCase like s"%${n.toLowerCase}%"),
searchCriteria.school.ready.map(r => s.ready === r)
).collect({ case Some(criteria) => criteria }).reduceLeftOption(_ && _).getOrElse(true: Rep[Boolean])
}
} yield (school, address)
db.run(q.drop(drop).take(take).result)
}
这是一个非常简单的连接,有一些过滤。事实上,一切正常。但是,当我检查执行的查询时,我看到了:
SELECT
x2."id",
x2."address_id",
x2."name",
x2."about",
x2."number_of_students",
x2."website_url",
x2."media_id",
x2."slug",
x2."short_description",
x2."ready",
x2."classrooms",
x2."year_established",
x2."display_copyright",
x2."createdat",
x2."updatedat",
x2."deletedat",
x2."createdby",
x2."updatedby",
x2."dli_number",
(CASE WHEN (x3."id" IS NULL)
THEN NULL
ELSE 1 END),
x3."id",
x3."country_id",
x3."continent_id",
x3."state_id",
x3."address1",
x3."address2",
x3."city",
x3."zip",
x3."createdat",
x3."updatedat",
x3."deletedat",
x3."createdby",
x3."updatedby"
FROM "school" x2 LEFT OUTER JOIN "address" x3 ON (x2."address_id" = x3."id") AND (x3."deletedat" IS NULL)
WHERE ((x2."deletedat" IS NULL) AND (lower(x2."name") LIKE '%a%')) AND (x2."ready" = TRUE)
LIMIT 100
OFFSET 0
为什么Slick添加了这个:
(CASE WHEN (x3."id" IS NULL) THEN NULL ELSE 1 END)
我无法以任何方式看到这有用。
谢谢,