Created
June 16, 2018 03:02
-
-
Save isyufu/c2136288a41f3567aace09f030a83edd to your computer and use it in GitHub Desktop.
Doobie with SQLite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* library dependecies | |
"org.xerial" % "sqlite-jdbc" % "3.23.1", | |
"org.tpolecat" %% "doobie-core" % "0.5.3", | |
"org.tpolecat" %% "doobie-hikari" % "0.5.3", // HikariCP transactor. | |
"org.tpolecat" %% "doobie-specs2" % "0.5.3", // Specs2 support for typechecking statements. | |
"org.tpolecat" %% "doobie-scalatest" % "0.5.3", // ScalaTest support for typechecking statements. | |
*/ | |
object TryDoobie extends App { | |
import doobie._ | |
import doobie.implicits._ | |
import cats._ | |
import cats.data._ | |
import cats.effect.IO | |
import cats.implicits._ | |
val xa = Transactor.fromDriverManager[IO]( | |
"org.sqlite.JDBC", "jdbc:sqlite:sample.db", "", "" | |
) | |
val y = xa.yolo | |
import y._ | |
val drop = | |
sql""" | |
DROP TABLE IF EXISTS person | |
""".update.run | |
val create = | |
sql""" | |
CREATE TABLE person ( | |
name TEXT NOT NULL UNIQUE, | |
age INTEGER | |
) | |
""".update.run | |
val res = (drop, create).mapN(_ + _).transact(xa).unsafeRunSync | |
println(res) | |
def insert1(name: String, age: Option[Short]): Update0 = | |
sql"insert into person (name, age) values ($name, $age)".update | |
insert1("Alice", Some(12)).run.transact(xa).unsafeRunSync | |
insert1("Bob", None).quick.unsafeRunSync // switch to YOLO mode | |
case class Person(id: Long, name: String, age: Option[Short]) | |
val l = sql"select rowid, name, age from person".query[Person].to[List].transact(xa).unsafeRunSync | |
l.foreach(println) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add
import cats.effect.unsafe.implicits.global