Skip to content

Commit

Permalink
Merge pull request #6 from armanbilge/pr/unique
Browse files Browse the repository at this point in the history
Fix `option`, `unique`
  • Loading branch information
armanbilge authored Mar 6, 2023
2 parents e497efc + f964dbc commit 77d88eb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions core/shared/src/main/scala/porcupine/db.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ private abstract class AbstractStatement[F[_], A, B](using F: MonadCancelThrow[F

final def execute(using Unit <:< B)(args: A) = cursor(args).use(_.fetch(1).void)

final def option(args: A) = cursor(args).use(_.fetch(1).flatMap {
case (Nil, false) => F.pure(None)
case (head :: Nil, false) => F.pure(Some(head))
final def option(args: A) = cursor(args).use(_.fetch(2).flatMap {
case (Nil, _) => F.pure(None)
case (head :: Nil, _) => F.pure(Some(head))
case _ => F.raiseError(new RuntimeException("More than 1 row"))
})

final def unique(args: A) = cursor(args).use(_.fetch(1).flatMap {
case (Nil, false) => F.raiseError(new NoSuchElementException)
case (head :: Nil, false) => F.pure(head)
final def unique(args: A) = cursor(args).use(_.fetch(2).flatMap {
case (Nil, _) => F.raiseError(new NoSuchElementException)
case (head :: Nil, _) => F.pure(head)
case _ => F.raiseError(new RuntimeException("More than 1 row"))
})

Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/test/scala/porcupine/PorcupineTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ object PorcupineTest extends IOApp.Simple:
sql"insert into porcupine values(${`null`}, $integer, $real, $text, $blob);".command,
(None, 42L, 3.14, "quill-pig", ByteVector(0, 1, 2, 3)),
) *>
db.execute(
db.unique(
sql"select b, t, r, i, n from porcupine;"
.query(blob *: text *: real *: integer *: `null` *: nil),
).flatMap {
case List((ByteVector(0, 1, 2, 3), "quill-pig", 3.14, 42, None)) => IO.unit
case (ByteVector(0, 1, 2, 3), "quill-pig", 3.14, 42, None) => IO.unit
case other => IO.raiseError(new AssertionError(other))
}
}

0 comments on commit 77d88eb

Please sign in to comment.