Skip to content

Commit

Permalink
Merge 8135bc4 into 721f6ed
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincibean committed Dec 9, 2019
2 parents 721f6ed + 8135bc4 commit af75218
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,104 @@ libraryDependencies ++= Seq(
)
```

### Usage

#### Enum

```scala
import enumeratum._

sealed trait ShirtSize extends EnumEntry

case object ShirtSize extends Enum[ShirtSize] with DoobieEnum[ShirtSize] {

case object Small extends ShirtSize
case object Medium extends ShirtSize
case object Large extends ShirtSize

val values = findValues

}

case class Shirt(size: ShirtSize)

import doobie._
import doobie.implicits._
import doobie.util.ExecutionContexts
import cats.effect._

implicit val cs = IO.contextShift(ExecutionContexts.synchronous)

val xa = Transactor.fromDriverManager[IO](
"org.postgresql.Driver",
"jdbc:postgresql:world",
"postgres",
"",
Blocker.liftExecutionContext(ExecutionContexts.synchronous)
)

sql"insert into clothes (shirt) values (${Shirt(ShirtSize.Small)})".update.run
.transact(xa)
.unsafeRunSync

sql"select shirt from clothes"
.query[Shirt]
.to[List]
.transact(xa)
.unsafeRunSync
.take(5)
.foreach(println)
```
- Note that a type ascription to the `EnumEntry` trait (eg. `ShirtSize.Small: ShirtSize`) is required when binding hardcoded `EnumEntry`s

#### ValueEnum

```scala
import enumeratum.values.{ IntDoobieEnum, IntEnum, IntEnumEntry }

sealed abstract class ShirtSize(val value: Int) extends IntEnumEntry

case object ShirtSize extends IntEnum[ShirtSize] with IntDoobieEnum[ShirtSize] {

case object Small extends ShirtSize(1)
case object Medium extends ShirtSize(2)
case object Large extends ShirtSize(3)

val values = findValues

}

case class Shirt(size: ShirtSize)

import doobie._
import doobie.implicits._
import doobie.util.ExecutionContexts
import cats.effect._

implicit val cs = IO.contextShift(ExecutionContexts.synchronous)

val xa = Transactor.fromDriverManager[IO](
"org.postgresql.Driver",
"jdbc:postgresql:world",
"postgres",
"",
Blocker.liftExecutionContext(ExecutionContexts.synchronous)
)

sql"insert into clothes (shirt) values (${Shirt(ShirtSize.Small)})".update.run
.transact(xa)
.unsafeRunSync

sql"select shirt from clothes"
.query[Shirt]
.to[List]
.transact(xa)
.unsafeRunSync
.take(5)
.foreach(println)
```
- Note that a type ascription to the `ValueEnumEntry` abstract class (eg. `ShirtSize.Small: ShirtSize`) is required when binding hardcoded `ValueEnumEntry`s

## Benchmarking

Benchmarking is in the unpublished `benchmarking` project. It uses JMH and you can run them in the sbt console by issuing the following command from your command line:
Expand Down

0 comments on commit af75218

Please sign in to comment.