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

make piggy SqlRuntime Future based, like LazyRuntime #28

Merged
merged 1 commit into from
Jan 22, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package dev.wishingtree.branch.piggy

import dev.wishingtree.branch.macaroni.runtimes.BranchExecutors
import dev.wishingtree.branch.piggy.Sql.*
import dev.wishingtree.branch.testkit.testcontainers.PGContainerSuite

import scala.concurrent.ExecutionContext
import scala.concurrent.duration.Duration

class PiggyPostgresqlSpec extends PGContainerSuite {

given ExecutionContext = BranchExecutors.executionContext

override val munitTimeout = Duration(10, "s")

val ddl =
Expand Down Expand Up @@ -39,21 +43,21 @@ class PiggyPostgresqlSpec extends PGContainerSuite {
)
.map(_.map(Person.apply))
} yield (nIns, fetchedPeople)
val result = sql.executePool(using pgPool)
val result = sql.executePool()(using pgPool)
assert(result.isSuccess)
assertEquals(result.get._1, 10)
assertEquals(result.get._2.distinct.size, 10)
}

test("PiggyPostgresql Rollback") {
given PgConnectionPool = pgPool
assert(Sql.statement(ddl).executePool.isSuccess)
assert(Sql.statement(ddl).executePool().isSuccess)

val blowup = for {
nIns <- Sql.prepareUpdate(ins, tenPeople*)
_ <- Sql.statement("this is not valid sql")
} yield nIns
assert(blowup.executePool.isFailure)
assert(blowup.executePool().isFailure)

val sql = for {
fetchedPeople <- Sql
Expand All @@ -65,7 +69,7 @@ class PiggyPostgresqlSpec extends PGContainerSuite {
} yield {
fetchedPeople
}
val result = sql.executePool
val result = sql.executePool()
assert(result.isSuccess)
assertEquals(result.get.size, 0)

Expand All @@ -75,7 +79,7 @@ class PiggyPostgresqlSpec extends PGContainerSuite {
given pool: PgConnectionPool = pgPool

val tple =
Sql.statement(s"SELECT 1, 'two'", _.tupled[(Int, String)]).executePool
Sql.statement(s"SELECT 1, 'two'", _.tupled[(Int, String)]).executePool()

assertEquals(tple.get.get, (1, "two"))
}
Expand All @@ -93,7 +97,7 @@ class PiggyPostgresqlSpec extends PGContainerSuite {
_.tupledList[(Int, String, Int)]
)
} yield fetchedPeople
}.executePool.get
}.executePool().get

assert(readBack.size == 10)
assert(readBack.forall(_._2.startsWith("Mark")))
Expand Down
27 changes: 20 additions & 7 deletions branch/src/main/scala/dev/wishingtree/branch/piggy/Sql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import dev.wishingtree.branch.macaroni.poolers.ResourcePool

import java.sql.{Connection, PreparedStatement, ResultSet}
import scala.compiletime.*
import scala.concurrent.Future
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
import scala.util.*

sealed trait Sql[+A] {
Expand Down Expand Up @@ -60,26 +61,38 @@ object Sql {
/** Execute this Sql operation using the given Connection. See
* [[SqlRuntime.execute]].
*/
def execute(using connection: Connection): Try[A] = {
SqlRuntime.execute(a)
def execute(d: Duration = Duration.Inf)(using
connection: Connection,
executionContext: ExecutionContext
): Try[A] = {
SqlRuntime.execute(a, d)

/** Execute this Sql operation using the given Connection, returning the
* result as a Future. See [[SqlRuntime.executeAsync]].
*/
}
def executeAsync(using connection: Connection): Future[A] =
def executeAsync(using
connection: Connection,
executionContext: ExecutionContext
): Future[A] =
SqlRuntime.executeAsync(a)

/** Execute this Sql operation using the given ResourcePool[Connection]. See
* [[SqlRuntime.executePool]].
*/
def executePool(using pool: ResourcePool[Connection]): Try[A] =
SqlRuntime.executePool(a)
def executePool(d: Duration = Duration.Inf)(using
pool: ResourcePool[Connection],
executionContext: ExecutionContext
): Try[A] =
SqlRuntime.executePool(a, d)

/** Execute this Sql operation using the given ResourcePool[Connection],
* returning the result as a Future. See [[SqlRuntime.executePoolAsync]].
*/
def executePoolAsync(using pool: ResourcePool[Connection]): Future[A] =
def executePoolAsync(using
pool: ResourcePool[Connection],
executionContext: ExecutionContext
): Future[A] =
SqlRuntime.executePoolAsync(a)
}

Expand Down
Loading
Loading