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

Refactor/2024 12 rename executor #344

Open
wants to merge 8 commits into
base: master
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
14 changes: 7 additions & 7 deletions docs/src/main/scala/00-Setup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import ldbc.dsl.io.*
// #given

// #setupDatabase
val createDatabase: Executor[IO, Int] =
val createDatabase: DBIO[Int] =
sql"CREATE DATABASE IF NOT EXISTS sandbox_db".update
// #setupDatabase

// #setupUser
val createUser: Executor[IO, Int] =
val createUser: DBIO[Int] =
sql"""
CREATE TABLE IF NOT EXISTS `user` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Expand All @@ -40,7 +40,7 @@ import ldbc.dsl.io.*
// #setupUser

// #setupProduct
val createProduct: Executor[IO, Int] =
val createProduct: DBIO[Int] =
sql"""
CREATE TABLE IF NOT EXISTS `product` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Expand All @@ -53,7 +53,7 @@ import ldbc.dsl.io.*
// #setupProduct

// #setupOrder
val createOrder: Executor[IO, Int] =
val createOrder: DBIO[Int] =
sql"""
CREATE TABLE IF NOT EXISTS `order` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Expand All @@ -70,7 +70,7 @@ import ldbc.dsl.io.*
// #setupOrder

// #insertUser
val insertUser: Executor[IO, Int] =
val insertUser: DBIO[Int] =
sql"""
INSERT INTO user (name, email) VALUES
('Alice', '[email protected]'),
Expand All @@ -80,7 +80,7 @@ import ldbc.dsl.io.*
// #insertUser

// #insertProduct
val insertProduct: Executor[IO, Int] =
val insertProduct: DBIO[Int] =
sql"""
INSERT INTO product (name, price) VALUES
('Laptop', 999.99),
Expand All @@ -91,7 +91,7 @@ import ldbc.dsl.io.*
// #insertProduct

// #insertOrder
val insertOrder: Executor[IO, Int] =
val insertOrder: DBIO[Int] =
sql"""
INSERT INTO `order` (user_id, product_id, quantity) VALUES
(1, 1, 1), -- Alice ordered 1 Laptop
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/scala/01-Program.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import ldbc.dsl.io.*
// #given

// #program
val program: Executor[IO, Int] = Executor.pure[IO, Int](1)
val program: DBIO[Int] = DBIO.pure[IO, Int](1)
// #program

// #connection
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/scala/02-Program.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import ldbc.dsl.io.*
// #given

// #program
val program: Executor[IO, Option[Int]] = sql"SELECT 2".query[Int].to[Option]
val program: DBIO[Option[Int]] = sql"SELECT 2".query[Int].to[Option]
// #program

// #connection
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/scala/03-Program.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import ldbc.dsl.io.*
// #given

// #program
val program: Executor[IO, (List[Int], Option[Int], Int)] =
val program: DBIO[(List[Int], Option[Int], Int)] =
for
result1 <- sql"SELECT 1".query[Int].to[List]
result2 <- sql"SELECT 2".query[Int].to[Option]
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/scala/04-Program.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import ldbc.dsl.io.*
// #given

// #program
val program: Executor[IO, Int] =
val program: DBIO[Int] =
sql"INSERT INTO user (name, email) VALUES ('Carol', '[email protected]')".update
// #program

Expand Down
4 changes: 2 additions & 2 deletions docs/src/main/scala/05-Program.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ import ldbc.dsl.codec.*
case Status.Active => true
case Status.InActive => false

val program1: Executor[IO, Int] =
val program1: DBIO[Int] =
sql"INSERT INTO user (name, email, status) VALUES (${ "user 1" }, ${ "[email protected]" }, ${ Status.Active })".update

given Decoder.Elem[Status] = Decoder.Elem.mapping[Boolean, Status] {
case true => Status.Active
case false => Status.InActive
}

val program2: Executor[IO, (String, String, Status)] =
val program2: DBIO[(String, String, Status)] =
sql"SELECT name, email, status FROM user WHERE id = 1".query[(String, String, Status)].unsafe

def connection = Connection[IO](
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/scala/0X-Cleanup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import ldbc.dsl.io.*
// #given

// #cleanupDatabase
val dropDatabase: Executor[IO, Int] =
val dropDatabase: DBIO[Int] =
sql"DROP DATABASE IF EXISTS sandbox_db".update
// #cleanupDatabase

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import ldbc.dsl.logging.*
* @tparam T
* The result type of the query
*/
trait Executor[F[_]: Temporal, T]:
trait DBIO[F[_]: Temporal, T]:

private[ldbc] def execute(connection: Connection[F])(using logHandler: LogHandler[F]): F[T]

Expand Down Expand Up @@ -64,60 +64,60 @@ trait Executor[F[_]: Temporal, T]:
.makeCase(acquire)(release)
.use(execute)

object Executor:
object DBIO:

private[ldbc] case class Impl[F[_]: Temporal, T](
statement: String,
params: List[Parameter],
run: Connection[F] => F[T]
) extends Executor[F, T]:
) extends DBIO[F, T]:

private[ldbc] def execute(connection: Connection[F])(using logHandler: LogHandler[F]): F[T] =
run(connection)
.onError(ex => logHandler.run(LogEvent.ProcessingFailure(statement, params.map(_.value), ex)))
<* logHandler.run(LogEvent.Success(statement, params.map(_.value)))

def pure[F[_]: Temporal, T](value: T): Executor[F, T] =
new Executor[F, T]:
def pure[F[_]: Temporal, T](value: T): DBIO[F, T] =
new DBIO[F, T]:
override private[ldbc] def execute(connection: Connection[F])(using LogHandler[F]): F[T] = Monad[F].pure(value)
override def readOnly(connection: Connection[F])(using LogHandler[F]): F[T] = Monad[F].pure(value)
override def commit(connection: Connection[F])(using LogHandler[F]): F[T] = Monad[F].pure(value)
override def rollback(connection: Connection[F])(using LogHandler[F]): F[T] = Monad[F].pure(value)
override def transaction(connection: Connection[F])(using LogHandler[F]): F[T] = Monad[F].pure(value)

def raiseError[F[_]: Temporal, A](e: Throwable): Executor[F, A] =
new Executor[F, A]:
def raiseError[F[_]: Temporal, A](e: Throwable): DBIO[F, A] =
new DBIO[F, A]:
override private[ldbc] def execute(connection: Connection[F])(using LogHandler[F]): F[A] =
MonadError[F, Throwable].raiseError(e)

given [F[_]: Temporal]: Functor[[T] =>> Executor[F, T]] with
override def map[A, B](fa: Executor[F, A])(f: A => B): Executor[F, B] =
new Executor[F, B]:
given [F[_]: Temporal]: Functor[[T] =>> DBIO[F, T]] with
override def map[A, B](fa: DBIO[F, A])(f: A => B): DBIO[F, B] =
new DBIO[F, B]:
override private[ldbc] def execute(connection: Connection[F])(using LogHandler[F]): F[B] =
fa.execute(connection).map(f)

given [F[_]: Temporal]: MonadError[[T] =>> Executor[F, T], Throwable] with
override def pure[A](x: A): Executor[F, A] = Executor.pure(x)
given [F[_]: Temporal]: MonadError[[T] =>> DBIO[F, T], Throwable] with
override def pure[A](x: A): DBIO[F, A] = DBIO.pure(x)

override def flatMap[A, B](fa: Executor[F, A])(f: A => Executor[F, B]): Executor[F, B] =
new Executor[F, B]:
override def flatMap[A, B](fa: DBIO[F, A])(f: A => DBIO[F, B]): DBIO[F, B] =
new DBIO[F, B]:
override private[ldbc] def execute(connection: Connection[F])(using LogHandler[F]): F[B] =
fa.execute(connection).flatMap(a => f(a).execute(connection))

override def tailRecM[A, B](a: A)(f: A => Executor[F, Either[A, B]]): Executor[F, B] =
new Executor[F, B]:
override def tailRecM[A, B](a: A)(f: A => DBIO[F, Either[A, B]]): DBIO[F, B] =
new DBIO[F, B]:
override private[ldbc] def execute(connection: Connection[F])(using logHandler: LogHandler[F]): F[B] =
MonadError[F, Throwable].tailRecM(a)(a => f(a).execute(connection))

override def ap[A, B](ff: Executor[F, A => B])(fa: Executor[F, A]): Executor[F, B] =
new Executor[F, B]:
override def ap[A, B](ff: DBIO[F, A => B])(fa: DBIO[F, A]): DBIO[F, B] =
new DBIO[F, B]:
override private[ldbc] def execute(connection: Connection[F])(using logHandler: LogHandler[F]): F[B] =
(ff.execute(connection), fa.execute(connection)).mapN(_(_))

override def raiseError[A](e: Throwable): Executor[F, A] =
Executor.raiseError(e)
override def raiseError[A](e: Throwable): DBIO[F, A] =
DBIO.raiseError(e)

override def handleErrorWith[A](fa: Executor[F, A])(f: Throwable => Executor[F, A]): Executor[F, A] =
new Executor[F, A]:
override def handleErrorWith[A](fa: DBIO[F, A])(f: Throwable => DBIO[F, A]): DBIO[F, A] =
new DBIO[F, A]:
override private[ldbc] def execute(connection: Connection[F])(using LogHandler[F]): F[A] =
fa.execute(connection).handleErrorWith(e => f(e).execute(connection))
8 changes: 4 additions & 4 deletions module/ldbc-dsl/src/main/scala/ldbc/dsl/Mysql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ case class Mysql[F[_]: Temporal](statement: String, params: List[Parameter.Dynam
* @return
* The number of rows updated
*/
def update: Executor[F, Int] =
Executor.Impl[F, Int](
def update: DBIO[F, Int] =
DBIO.Impl[F, Int](
statement,
params,
connection =>
Expand All @@ -97,10 +97,10 @@ case class Mysql[F[_]: Temporal](statement: String, params: List[Parameter.Dynam
* @return
* The primary key value
*/
def returning[T <: String | Int | Long](using decoder: Decoder.Elem[T]): Executor[F, T] =
def returning[T <: String | Int | Long](using decoder: Decoder.Elem[T]): DBIO[F, T] =
given Decoder[T] = Decoder.one[T]

Executor.Impl[F, T](
DBIO.Impl[F, T](
statement,
params,
connection =>
Expand Down
12 changes: 6 additions & 6 deletions module/ldbc-dsl/src/main/scala/ldbc/dsl/Query.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ trait Query[F[_], T]:
/**
* Functions for safely retrieving data from a database in an array or Option type.
*/
def to[G[_]](using FactoryCompat[T, G[T]]): Executor[F, G[T]]
def to[G[_]](using FactoryCompat[T, G[T]]): DBIO[F, G[T]]

/**
* A method to return the data to be retrieved from the database as is. If the data does not exist, an exception is
* raised. Use the [[to]] method if you want to retrieve individual data.
*/
def unsafe: Executor[F, T]
def unsafe: DBIO[F, T]

object Query:

Expand All @@ -45,8 +45,8 @@ object Query:

given Decoder[T] = decoder

override def to[G[_]](using FactoryCompat[T, G[T]]): Executor[F, G[T]] =
Executor.Impl[F, G[T]](
override def to[G[_]](using FactoryCompat[T, G[T]]): DBIO[F, G[T]] =
DBIO.Impl[F, G[T]](
statement,
params,
connection =>
Expand All @@ -59,8 +59,8 @@ object Query:
yield result
)

override def unsafe: Executor[F, T] =
Executor.Impl[F, T](
override def unsafe: DBIO[F, T] =
DBIO.Impl[F, T](
statement,
params,
connection =>
Expand Down
4 changes: 2 additions & 2 deletions module/ldbc-dsl/src/main/scala/ldbc/dsl/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ package object dsl:
def orderByOpt(s1: Option[SQL], s2: Option[SQL], ss: Option[SQL]*): Mysql[F] =
orderByOpt((s1 :: s2 :: ss.toList).flatten)

type ExecutorIO[T] = ldbc.dsl.Executor[F, T]
export ldbc.dsl.Executor
type DBIO[T] = ldbc.dsl.DBIO[F, T]
val DBIO = ldbc.dsl.DBIO

export ldbc.dsl.logging.LogHandler

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ package object syntax:
case _ => DslQuery.Impl[F, P](query.statement, query.params, Decoder.derivedProduct(m1))

extension (command: Command)
def update: Executor[F, Int] =
Executor.Impl[F, Int](
def update: DBIO[Int] =
DBIO.Impl[F, Int](
command.statement,
command.params,
connection =>
Expand All @@ -55,10 +55,10 @@ package object syntax:
yield result
)

def returning[T <: String | Int | Long](using decoder: Decoder.Elem[T]): Executor[F, T] =
def returning[T <: String | Int | Long](using decoder: Decoder.Elem[T]): DBIO[T] =
given Decoder[T] = Decoder.one[T]

Executor.Impl[F, T](
DBIO.Impl[F, T](
command.statement,
command.params,
connection =>
Expand Down
Loading
Loading