LoginSignup
1
1

More than 5 years have passed since last update.

Quillでパラメータがある時だけwhereに条件追加したい

Last updated at Posted at 2018-02-23

filter条件が入力されてればwhereに追加したいけどquillではどうやるのかメモ

フィルター条件にユーザ名が入力されるかもしれない場合を例にしてみる

quoteでそれぞれのクエリ生成部分を囲んで繋げる

val name: Option[String] = Some("alderweireld") // ユーザ名 ※ある場合
val limit: Int = 20

val q1 = quote(users)

val q2 = name match {
  case Some(n: String) => quote { q1.filter(_.name == lift(n) } // 値があれば条件に追加
  case None => quote(q1) // 値が無ければそのままq1を返す
}

val q = quote { // その他の処理
  q2.sortBy(_.createdAt)(Ord.desc)
    .take(lift(limit))
    .map(_.id)
}

db.run(q)

ポイントはname match {}ごとquoteで囲まないようにするくらい
以上

参照

explicit bindings using lift

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1