Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fragments.in with 3 parameters #1739

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/core/src/main/scala/doobie/util/fragments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ object fragments {
def in[F[_]: Reducible, A: util.Put, B: util.Put](f: Fragment, fs: F[(A,B)]): Fragment =
fs.toList.map { case (a,b) => fr0"($a,$b)" }.foldSmash1(f ++ fr0"IN (", fr",", fr")")

/** Returns `f IN ((fs0-A, fs0-B, fs0-C), (fs1-A, fs1-B, fs1-C), ...)`. */
def in[F[_]: Reducible, A: util.Put, B: util.Put, C: util.Put](f: Fragment, fs: F[(A,B,C)]): Fragment =
fs.toList.map { case (a,b,c) => fr0"($a,$b,$c)" }.foldSmash1(f ++ fr0"IN (", fr",", fr")")

/** Returns `f NOT IN (fs0, fs1, ...)`. */
def notIn[F[_]: Reducible, A: util.Put](f: Fragment, fs: F[A]): Fragment =
fs.toList.map(a => fr0"$a").foldSmash1(f ++ fr0"NOT IN (", fr",", fr")")
Expand Down
4 changes: 4 additions & 0 deletions modules/core/src/test/scala/doobie/util/FragmentsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class FragmentsSuite extends munit.FunSuite {
assertEquals(in(fr"foo", NonEmptyList.of((1, true), (2, false))).query[Unit].sql, "foo IN ((?,?), (?,?)) ")
}

test("in for three columns") {
assertEquals(in(fr"foo", NonEmptyList.of((1, true, 3), (2, false, 4))).query[Unit].sql, "foo IN ((?,?,?), (?,?,?)) ")
}

test("notIn") {
assertEquals(notIn(fr"foo", nel).query[Unit].sql, "foo NOT IN (?, ?, ?) ")
}
Expand Down