Skip to content

Commit

Permalink
Remove where clause
Browse files Browse the repository at this point in the history
  • Loading branch information
matsluni committed Nov 3, 2024
1 parent 63c053e commit 403e021
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
9 changes: 3 additions & 6 deletions modules/core/src/main/scala/doobie/util/fragments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ object fragments {
def values[F[_]: Reducible, A](fs: F[A])(implicit w: util.Write[A]): Fragment =
fr"VALUES" ++ comma(fs.toNonEmptyList.map(f => parentheses(values(f))))

/** Returns `UPDATE tableName SET columnUpdate0, columnUpdate1, ... WHERE (whereAnd0) AND (whereAnd1) ...`. */
/** Returns `UPDATE tableName SET columnUpdate0, columnUpdate1, ...`. */
def updateSetOpt[F[_]: Foldable](
tableName: Fragment,
columnUpdates: F[Fragment],
condition: Fragment
columnUpdates: F[Fragment]
): Option[Fragment] = {
NonEmptyList.fromFoldable(columnUpdates).map(cs =>
fr"UPDATE" ++ tableName ++ set(cs) ++ (if (condition == Fragment.empty) condition
else fr"" ++ whereAnd(condition)))
NonEmptyList.fromFoldable(columnUpdates).map(cs => fr"UPDATE" ++ tableName ++ set(cs))
}

/** Returns `(f IN (fs0, fs1, ...))`. */
Expand Down
18 changes: 2 additions & 16 deletions modules/core/src/test/scala/doobie/util/FragmentsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,12 @@ class FragmentsSuite extends munit.FunSuite {

test("updateSetOpt for three column") {
assertEquals(
updateSetOpt(Fragment.const("Foo"), sqlKv, Fragment.empty).map(_.query[Unit].sql),
updateSetOpt(Fragment.const("Foo"), sqlKv).map(_.query[Unit].sql),
Some("UPDATE Foo SET a = ?, b = ?, c = ?"))
}

test("updateSetOpt for empty columns") {
assertEquals(
updateSetOpt(Fragment.const("Foo"), List.empty[Fragment], Fragment.empty).map(_.query[Unit].sql),
None)
}

test("updateSetOpt for three column and defined where clause") {
assertEquals(
updateSetOpt(Fragment.const("Foo"), sqlKv, fr0"id = 1").map(_.query[Unit].sql),
Some("UPDATE Foo SET a = ?, b = ?, c = ? WHERE (id = 1)"))
}

test("updateSetOpt for empty columns but defined where clause") {
assertEquals(
updateSetOpt(Fragment.const("Foo"), List.empty[Fragment], fr0"id = 1").map(_.query[Unit].sql),
None)
assertEquals(updateSetOpt(Fragment.const("Foo"), List.empty[Fragment]).map(_.query[Unit].sql), None)
}

test("in (1-column varargs)") {
Expand Down

0 comments on commit 403e021

Please sign in to comment.