
doobie.util.fragments.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doobie-core-cats_2.11 Show documentation
Show all versions of doobie-core-cats_2.11 Show documentation
Pure functional JDBC layer for Scala.
The newest version!
package doobie.util
import doobie.imports._
import cats.{ Reducible => Foldable1, _}, cats.implicits._
/** Module of `Fragment` constructors. */
object fragments {
/** Returns `f IN (fs0, fs1, ...)`. */
def in[F[_]: Foldable1, A: Param](f: Fragment, fs: F[A]): Fragment =
fs.toList.map(a => fr0"$a").foldSmash1(f ++ fr0"IN (", fr",", fr")")
/** Returns `f NOT IN (fs0, fs1, ...)`. */
def notIn[F[_]: Foldable1, A: Param](f: Fragment, fs: F[A]): Fragment =
fs.toList.map(a => fr0"$a").foldSmash1(f ++ fr0"NOT IN (", fr",", fr")")
/** Returns `f1 AND f2 AND ... fn`. */
def and(fs: Fragment*): Fragment =
fs.toList.intercalate(fr"AND")
/** Returns `f1 AND f2 AND ... fn` for all defined fragments. */
def andOpt(fs: Option[Fragment]*): Fragment =
and(fs.toList.flatten: _*)
/** Returns `f1 OR f2 OR ... fn`. */
def or(fs: Fragment*): Fragment =
fs.toList.intercalate(fr"OR")
/** Returns `f1 OR f2 OR ... fn` for all defined fragments. */
def orOpt(fs: Option[Fragment]*): Fragment =
or(fs.flatten: _*)
/** Returns `WHERE f1 AND f2 AND ... fn` or the empty fragment if `fs` is empty. */
def whereAnd(fs: Fragment*): Fragment =
if (fs.isEmpty) Fragment.empty else fr"WHERE" ++ and(fs: _*)
/** Returns `WHERE f1 AND f2 AND ... fn` for defined `f`, if any, otherwise the empty fragment. */
def whereAndOpt(fs: Option[Fragment]*): Fragment =
whereAnd(fs.flatten: _*)
/** Returns `WHERE f1 OR f2 OR ... fn` or the empty fragment if `fs` is empty. */
def whereOr(fs: Fragment*): Fragment =
if (fs.isEmpty) Fragment.empty else fr"WHERE" ++ or(fs: _*)
/** Returns `WHERE f1 OR f2 OR ... fn` for defined `f`, if any, otherwise the empty fragment. */
def whereOrOpt(fs: Option[Fragment]*): Fragment =
whereOr(fs.flatten: _*)
/** Returns `SET f1, f2, ... fn` or the empty fragment if `fs` is empty. */
def set(fs: Fragment*): Fragment =
if (fs.isEmpty) Fragment.empty else fr"SET" ++ fs.toList.intercalate(fr",")
/** Returns `SET f1, f2, ... fn` for defined `f`, if any, otherwise the empty fragment. */
def setOpt(fs: Option[Fragment]*): Fragment =
set(fs.flatten: _*)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy