Warm tip: This article is reproduced from stackoverflow.com, please click
quill.io

Is it possible to update multiple rows with Quill using IN

发布于 2020-03-29 21:03:59

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.

Questioner
Tae
Viewed
27
Andrzej Jozwik 2020-01-31 19:11

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 (?, ?)