温馨提示:本文翻译自stackoverflow.com,查看原文请点击:quill.io - Is it possible to update multiple rows with Quill using IN
quill.io

quill.io - 是否可以使用IN使用Quill更新多行

发布于 2020-03-29 22:09:14

可能会产生类似

UPDATE employees SET gender = 'Male' WHERE id IN ('asfd','bleh');

用鹅毛笔?我在文档中没有找到示例,而批处理更新似乎是另外一回事

查看更多

提问者
Tae
被浏览
12
Andrzej Jozwik 2020-01-31 19:11

它对我有用(尝试https://scastie.scala-lang.org/):

静态查询:

import io.getquill._

val ctx = new SqlMirrorContext(PostgresDialect, SnakeCase)

import ctx._

case class Product(name: String, price: Int)

def products(names: Seq[String], name: String) =
    ctx.run {
      ctx
        .query[Product]
        .filter(person => quote(liftQuery(names).contains(person.name)))
        .update(_.name -> lift(name))
}

val m = products(Seq("AA", "BB"), "EE")

println(m.string)

动态查询

import io.getquill._

val ctx = new SqlMirrorContext(PostgresDialect, SnakeCase)

import ctx._

case class Product(name: String, price: Int)

def products(names: Seq[String], name: String) =
    ctx.run {
      ctx
        .dynamicQuery[Product]
        .filter(person => quote(liftQuery(names).contains(person.name)))
        .update(setValue(_.name, name))
}

val m = products(Seq("AA", "BB"), "EE")

println(m.string)

输出:

UPDATE product SET name = ? WHERE name IN (?, ?)