You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.3 KiB

4 years ago
  1. package com.example.user.slick.dbios
  2. import slick.jdbc.JdbcProfile
  3. import com.example.models._
  4. import io.scalaland.chimney.dsl._
  5. import com.example.user.slick.Tables
  6. import javax.inject.Singleton
  7. // import slick.jdbc.H2Profile.api._
  8. // import scala.concurrent.ExecutionContext
  9. @Singleton
  10. class SlickLibraryDbio extends Tables {
  11. override val profile: JdbcProfile = _root_.slick.jdbc.H2Profile
  12. import profile.api._
  13. def findBookById(id: Long): DBIO[Option[BooksRow]] =
  14. Query.bookById(id).result.headOption
  15. def findBookById2(id: Long): DBIO[Option[BookWithoutId]] =
  16. Query.test(id).result.headOption
  17. def findBooksWithAuthor: DBIO[Seq[(BooksRow, AuthorsRow)]] =
  18. Query.booksWithAuthor.result
  19. def insertBook(book: Book): DBIO[BooksRow] =
  20. Query.writeBooks += bookToRow(book)
  21. def insertBook2(book: NewBook): DBIO[Long] =
  22. Query.writeBooks3 += book
  23. //
  24. def insertAuthor(author: Author): DBIO[AuthorsRow] =
  25. Query.writeAuthors += authorToRow(author)
  26. def insertAuthor2(author: NewAuthor): DBIO[Long] =
  27. Query.writeAuthors2 += author
  28. def authorToRow(author: Author) = author.transformInto[AuthorsRow]
  29. def bookToRow(book: Book) = book.transformInto[BooksRow]
  30. def authorsRowToAuthor(author: AuthorsRow) = author.transformInto[Author]
  31. def booksRowToBooks(book: BooksRow) = book.transformInto[Book]
  32. def booksRowToBooks2(book: BooksRow) = book.transformInto[BookWithoutId]
  33. // As mentioned under #2, we do encapsulate our queries
  34. object Query {
  35. // Return the book / author with it's auto incremented
  36. // id instead of an insert count
  37. lazy val writeBooks = Books returning Books
  38. .map(_.id) into ((book, id) => book.copy(id))
  39. lazy val writeBooks2 =
  40. Books.map(b => (b.title, b.authorId).mapTo[BookWithoutId])
  41. lazy val writeBooks3 = Books
  42. .map(b => (b.title, b.authorId).mapTo[NewBook])
  43. .returning(Books.map(_.id))
  44. lazy val writeAuthors = Authors returning Authors
  45. .map(_.id) into ((author, id) => author.copy(id))
  46. lazy val writeAuthors2 =
  47. Authors.map(a => (a.name).mapTo[NewAuthor]) returning Authors.map(_.id)
  48. lazy val test = (givenId: Long) =>
  49. Books
  50. .filter(_.id === givenId)
  51. .map(toBooksWithoutID)
  52. lazy val bookById = Books.findBy(_.id)
  53. lazy val toBooksWithoutID = (table: Books) =>
  54. (table.title, table.authorId).mapTo[BookWithoutId]
  55. lazy val booksWithAuthor = for {
  56. b <- Books
  57. a <- Authors if b.authorId === a.id
  58. } yield (b, a)
  59. lazy val authorOfBook = (bookId: Long) =>
  60. for {
  61. (authors, books) <- Authors join Books on (_.id === _.authorId) filter {
  62. case (authors, books) => books.id === bookId
  63. }
  64. } yield (authors, books)
  65. lazy val authorOfBook2 = (bookId: Long) =>
  66. for {
  67. authorId <- Books.filter(_.id === bookId).take(1).map(_.authorId)
  68. authors <- Authors filter (_.id === authorId)
  69. } yield (authors.name)
  70. // lazy val authorOfBook3 = (bookId: Long) =>
  71. // for {
  72. // authorId <- bookById(bookId).map(_.map(_.authorId))
  73. // (authors) <- Authors filter (_.id === authorId)
  74. // } yield (authors)
  75. }
  76. case class BookWithoutId(title: String, authorId: Long)
  77. // def test() = {
  78. // val maybeBook = findBookById(1)
  79. // val x = maybeBook.map(_.map(_.title))
  80. // db.run(x)
  81. // }
  82. }