设为首页 加入收藏

TOP

细谈Slick(6)- Projection:ProvenShape,强类型的Query结果类型(四)
2017-10-10 12:10:48 】 浏览:1678
Tags:细谈 Slick Projection ProvenShape 类型 Query 结果
于case class YR的projection。在join table query情况下只能通过这种方式来构建Projection,看看下面这个例子:

 1   case class Title(id: Int, title: String)  2   class PersonTitle(tag: Tag) extends Table[Title](tag,"TITLE") {  3     def id = column[Int]("id")  4     def title = column[String]("title")  5     def * = (id,title) <> (Title.tupled,Title.unapply)  6  }  7   val personTitle = TableQuery[PersonTitle]  8   val createTitleAction = personTitle.schema.create  9  Await.ready(db.run(createTitleAction),Duration.Inf) 10    val initTitleData = DBIO.seq { 11      personTitle ++= Seq( 12        Title(1,"Manager"), 13        Title(2,"Programmer"), 14        Title(3,"Clerk") 15  ) 16  } 17  Await.ready(db.run(initTitleData),Duration.Inf) 18  
19   case class Titles(id: Int, name: String, title: String) 20   val qPersonWithTitle = for { 21     p <- hlistPerson 22     t <- personTitle if p.id === t.id 23   } yield ((p.id,p.name,t.title) <> (Titles.tupled,Titles.unapply)) 24   Await.result(db.run(qPersonWithTitle.result),Duration.Inf).foreach {row =>
25     println(s"${row.id} ${row.name}, ${row.title}") 26   }

现在对任何形式的Query结果我们都能使用强类型(strong typed)的字段名称来进行操作了。

下面是本次示范的源代码:

 1 import slick.collection.heterogeneous.{ HList, HCons, HNil }  2 import slick.collection.heterogeneous.syntax._  3 import slick.driver.H2Driver.api._  4 
 5 import scala.concurrent.ExecutionContext.Implicits.global
 6 import scala.concurrent.duration._  7 import scala.concurrent.{Await, Future}  8 
 9 
 10 object chkProjection {  11   
 12   class TupleTypedPerson(tag: Tag) extends Table[(  13      Option[Int],String,Int,Option[String])](tag,"PERSON") {  14     def id = column[Int]("id",O.PrimaryKey,O.AutoInc)  15     def name = column[String]("name")  16     def age = column[Int]("age")  17     def alias = column[Option[String]]("alias")  18     def * = (id.?,name,age,alias)  19  }  20   val tupleTypedPerson = TableQuery[TupleTypedPerson]  21 
 22   val db = Database.forURL("jdbc:h2:mem:test1;DB_CLOSE_DELAY=-1", driver = "org.h2.Driver")  23   val createSchemaAction = tupleTypedPerson.schema.create  24  Await.ready(db.run(createSchemaAction),Duration.Inf)  25   val initDataAction = DBIO.seq {  26     tupleTypedPerson ++= Seq(  27       (Some(0),"Tiger Chan", 45, Some("Tiger_XC")),  28       (Some(0),"Johnny Cox", 17, None),  29       (Some(0),"Cathy Williams", 18, Some("Catty")),  30       (Some(0),"David Wong", 43, None)  31  )  32  }  33  Await.ready(db.run(initDataAction),Duration.Inf)  34 
 35   val queryAction = tupleTypedPerson.result  36 
 37   Await.result(db.run(queryAction),Duration.Inf).foreach {row =>
 38     println(s"${row._1.get} ${row._2} ${row._4.getOrElse("")}, ${row._3}")  39  }  40 
 41   class Person(val id: Option[Int],  42  val name: String, val age: Int, val alias: Option[String])  43   def toPerson(t: (Option[Int],String,Int,Option[String])) = new Person (  44  t._1,t._2,t._3,t._4  45  )  46   def fromPerson(p: Person) = Some((p.id,p.name,p.age,p.alias))  47   class TupleMappedPerson(tag: Tag) extends Table[  48     Person](tag,"PERSON") {  49     def id = column[Int]("id",O.PrimaryKey,O.AutoInc)  50     def name = column[String]("name")  51     def age = column[Int]("age")  52     def alias = column[Option[String]]("alias")  53     def * = (id.?,name,age,ali
首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇play for scala 实现SessionFilte.. 下一篇函数式中的 currying

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目