简体   繁体   中英

Is it possible to update multiple rows with Quill using IN

It is possible to produce something like

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

with Quill? I don't find and example in the documentation, and batch update seems to be something else .

It works for me (try on https://scastie.scala-lang.org/ ):

Static query:

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)

Dynamic query

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)

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM