Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. import slick.dbio.Effect.Write
  2. import slick.lifted.{CanBeQueryCondition, Rep, Tag}
  3. import slick.jdbc.PostgresProfile.api._
  4. import slick.sql.FixedSqlAction
  5.  
  6. import scala.concurrent.Future
  7. import scala.reflect._
  8.  
  9. trait BaseEntity {
  10. val id: Long
  11. val isDeleted: Boolean
  12. }
  13.  
  14. abstract class BaseTable[E: ClassTag](tag: Tag, schemaName: Option[String], tableName: String)
  15. extends Table[E](tag, schemaName, tableName) {
  16.  
  17. val classOfEntity = classTag[E].runtimeClass
  18.  
  19. val id: Rep[Long] = column[Long]("Id", O.PrimaryKey, O.AutoInc)
  20. val isDeleted: Rep[Boolean] = column[Boolean]("IsDeleted", O.Default(false))
  21. }
  22.  
  23. trait BaseRepositoryComponent[T <: BaseTable[E], E <: BaseEntity] {
  24. def getById(id: Long): Future[Option[E]]
  25.  
  26. def getAll: Future[Seq[E]]
  27.  
  28. def filter[C <: Rep[_]](expr: T => C)(implicit wt: CanBeQueryCondition[C]): Future[Seq[E]]
  29.  
  30. def save(row: E): Future[E]
  31.  
  32. def deleteById(id: Long): Future[Int]
  33.  
  34. def updateById(id: Long, row: E): Future[Int]
  35. }
  36.  
  37. trait BaseRepositoryQuery[T <: BaseTable[E], E <: BaseEntity] {
  38.  
  39. val query: slick.jdbc.PostgresProfile.api.type#TableQuery[T]
  40.  
  41. def getByIdQuery(id: Long): Query[T, E, Seq] = {
  42. query.filter(_.id === id).filter(_.isDeleted === false)
  43. }
  44.  
  45. def getAllQuery: Query[T, E, Seq] = {
  46. query.filter(_.isDeleted === false)
  47. }
  48.  
  49. def filterQuery[C <: Rep[_]](expr: T => C)(implicit wt: CanBeQueryCondition[C]): Query[T, E, Seq] = {
  50. query.filter(expr).filter(_.isDeleted === false)
  51. }
  52.  
  53. def saveQuery(row: E): FixedSqlAction[E, NoStream, Write] = {
  54. query returning query += row
  55. }
  56.  
  57. def deleteByIdQuery(id: Long): FixedSqlAction[Int, NoStream, Write]={
  58. query.filter(_.id === id).map(_.isDeleted).update(true)
  59. }
  60.  
  61. def updateByIdQuery(id: Long, row: E): FixedSqlAction[Int, NoStream, Write] = {
  62. query.filter(_.id === id).filter(_.isDeleted === false).update(row)
  63. }
  64.  
  65. }
  66.  
  67. abstract class BaseRepository[T <: BaseTable[E], E <: BaseEntity : ClassTag](clazz: TableQuery[T])
  68. extends BaseRepositoryQuery[T, E]
  69. with BaseRepositoryComponent[T, E] {
  70. val clazzTable: TableQuery[T] = clazz
  71. lazy val clazzEntity = classTag[E].runtimeClass
  72. val query: slick.jdbc.PostgresProfile.api.type#TableQuery[T] = clazz
  73. val user = "postgres"
  74. val url = "jdbc:postgresql://localhost:5432/learning"
  75. val password = "admin"
  76. val driver = "org.postgresql.Driver"
  77.  
  78.  
  79. val db = Database.forURL(url, user = user, password = password, driver = driver)
  80.  
  81. def getAll: Future[Seq[E]] = {
  82. db.run(getAllQuery.result)
  83. }
  84.  
  85. def getById(id: Long): Future[Option[E]] = {
  86. db.run(getByIdQuery(id).result.headOption)
  87. }
  88.  
  89. def filter[C <: Rep[_]](expr: T => C)(implicit wt: CanBeQueryCondition[C]): Future[Seq[E]] = {
  90. db.run(filterQuery(expr).result)
  91. }
  92.  
  93. def save(row: E): Future[E] = {
  94. db.run(saveQuery(row))
  95. }
  96.  
  97. def updateById(id: Long, row: E): Future[Int] = {
  98. db.run(updateByIdQuery(id, row))
  99. }
  100.  
  101. def deleteById(id: Long): Future[Int] = {
  102. db.run(deleteByIdQuery(id))
  103. }
  104.  
  105. }
  106.  
  107. import slick.jdbc.PostgresProfile.api._
  108. import slick.lifted.ProvenShape.proveShapeOf
  109. import slick.lifted.{Rep, Tag}
  110.  
  111. class EmployeeTable(_tableTag: Tag) extends BaseTable[Employee] (_tableTag, Some("learning"), "Employee") {
  112.  
  113. def * = (id, firstName, isDeleted) <>(Employee.tupled, Employee.unapply)
  114.  
  115. def ? = (Rep.Some(id), Rep.Some(firstName), Rep.Some(isDeleted)).shaped.<>({ r => import r._; _1.map(_ => Employee.tupled((_1.get, _2.get, _3.get))) }, (_: Any) => throw new Exception("Inserting into ? projection not supported."))
  116.  
  117. override val id: Rep[Long] = column[Long]("EmployeeId", O.AutoInc, O.PrimaryKey)
  118. val firstName: Rep[String] = column[String]("FirstName")
  119. override val isDeleted: Rep[Boolean] = column[Boolean]("IsDeleted")
  120. lazy val employeeTable = new TableQuery(tag => new EmployeeTable(tag))
  121.  
  122. }
  123.  
  124.  
  125. case class Employee(id: Long, firstName: String, isDeleted: Boolean) extends BaseEntity
  126.  
  127. name := "SlickAkka"
  128.  
  129. version := "1.0"
  130.  
  131. scalaVersion := "2.12.0"
  132.  
  133. libraryDependencies ++= Seq(
  134. "com.typesafe.slick" % "slick_2.11" % "3.2.0-M1",
  135. "org.postgresql" % "postgresql" % "9.4.1211"
  136. )
  137.  
  138. import slick.lifted.TableQuery
  139. import scala.concurrent.ExecutionContext.Implicits.global
  140. abstract class EmployeeRepository
  141. extends BaseRepository[EmployeeTable, Employee](TableQuery[EmployeeTable]){
  142.  
  143. def insertItem(row: Employee) = {
  144. super.save(row)
  145. }
  146.  
  147. }
  148.  
  149. object ImplEmployeeRepository extends EmployeeRepository
  150.  
  151. object TestEmp extends App {
  152.  
  153. val emp = Employee(0L, "aamir", false)
  154.  
  155. for {
  156. result <- ImplEmployeeRepository.insertItem(emp)
  157. _ = println(result)
  158. } yield result
  159. Thread.sleep(5000)
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement