Advertisement
Guest User

Untitled

a guest
May 1st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import cats.implicits._
  2. import doobie.imports._
  3. import fs2.Task
  4. import fs2.interop.cats._
  5.  
  6. object Main extends App {
  7. val xa = DriverManagerTransactor[Task](
  8. "org.postgresql.Driver", "jdbc:postgresql:world", "postgres", ""
  9. )
  10.  
  11. def insert(foos: List[(Int, String)]): ConnectionIO[Int] = {
  12. val q = "insert into foo (id, name) values (?, ?)"
  13. Update[(Int, String)](q).updateMany(foos)
  14. }
  15.  
  16. def delete(min: Long, max: Long): ConnectionIO[Int] = {
  17. (sql"delete from foo where (id between $min and $max)").update.run
  18. }
  19.  
  20. val insertIO: ConnectionIO[Int] =
  21. insert((1 to 100).toList.map((_, "a")))
  22.  
  23. val deleteIO: ConnectionIO[Unit] =
  24. (1 to 50).grouped(10).toStream.map { ids =>
  25. delete(ids.min, ids.max)
  26. }.sequence_
  27.  
  28. val insertTask: Task[Int] = insertIO.transact(xa)
  29. val deleteTask: Task[Unit] = deleteIO.transact(xa)
  30.  
  31. //insertTask.unsafeRun
  32. //deleteTask.unsafeRun
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement