本文主要是介绍细谈Slick(6)- Projection:ProvenShape,强类型的Query结果类型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在Slick官方文档中描述:连接后台数据库后,需要通过定义Projection,即def * 来进行具体库表列column的选择和排序。通过Projection我们可以选择库表中部分列、也可以增加一些自定义列computed column。具体来说Projection提供了数据库表列与Scala值的对应。例如def * = (column1,column2)把库表的column1和column2与(Int,String)对应,column1[Int],column2[String]。也可以说是与定义column的类参数进行对应。从Slick源代码中我们可以找到Projection定义:
abstract class AbstractTable[T](val tableTag: Tag, val schemaName: Option[String], val tableName: String) extends Rep[T] {/** The client-side type of the table as defined by its * projection */type TableElementType
.../** The * projection of the table used as default for queries and inserts.* Should include all columns as a tuple, HList or custom shape and optionally* map them to a custom entity type using the <> operator.* The `ProvenShape` return type ensures that* there is a `Shape` available for translating between the `Column`-based* type in * and the client-side type without `Column` in the table's type* parameter. */def * : ProvenShape[T]
...
}
我们看到Projection是个ProvenShape[T]类。再看看ProvenShape是怎么定义的:
/** A limited version of ShapedValue which can be constructed for every type* that has a valid shape. We use it to enforce that a table's * projection* has a valid shape. A ProvenShape has itself a Shape so it can be used in* place of the value that it wraps for purposes of packing and unpacking. */
trait ProvenShape[U] {def value: Anyval shape: Shape[_ <: FlatShapeLevel, _, U, _]def packedValue[R](implicit ev: Shape[_ <: FlatShapeLevel, _, U, R]): ShapedValue[R, U]def toNode = packedValue(shape).toNode
}object ProvenShape {/** Convert an appropriately shaped value to a ProvenShape */implicit def proveShapeOf[T, U](v: T)(implicit sh: Shape[_ <: FlatShapeLevel, T, U, _]): ProvenShape[U] =new ProvenShape[U] {def value = vval shape: Shape[_ <: FlatShapeLevel, _, U, _] = sh.asInstanceOf[Shape[FlatShapeLevel, _, U, _]]def packedValue[R](implicit ev: Shape[_ <: FlatShapeLevel, _, U, R]): ShapedValue[R, U] = ShapedValue(sh.pack(value).asInstanceOf[R], sh.packedShape.asInstanceOf[Shape[FlatShapeLevel, R, U, _]])}/** The Shape for a ProvenShape */implicit def provenShapeShape[T, P](implicit shape: Shape[_ <: FlatShapeLevel, T, T, P]): Shape[FlatShapeLevel, ProvenShape[T], T, P] = new Shape[FlatShapeLevel, ProvenShape[T], T, P] {def pack(value: Mixed): Packed =value.shape.pack(value.value.asInstanceOf[value.shape.Mixed]).asInstanceOf[Packed]def packedShape: Shape[FlatShapeLevel, Packed, Unpacked, Packed] =shape.packedShape.asInstanceOf[Shape[FlatShapeLevel, Packed, Unpacked, Packed]]def buildParams(extract: Any => Unpacked): Packed =shape.buildParams(extract.asInstanceOf[Any => shape.Unpacked])def encodeRef(value: Mixed, path: Node) =value.shape.encodeRef(value.value.asInstanceOf[value.shape.Mixed], path)def toNode(value: Mixed): Node =value.shape.toNode(value.value.asInstanceOf[value.shape.Mixed])}
}
/** A type class that encodes the unpacking `Mixed => Unpacked` of a* `Query[Mixed]` to its result element type `Unpacked` and the packing to a* fully packed type `Packed`, i.e. a type where everything which is not a* transparent container is wrapped in a `Column[_]`.** =Example:=* - Mixed: (Column[Int], Column[(Int, String)], (Int, Option[Double]))* - Unpacked: (Int, (Int, String), (Int, Option[Double]))* - Packed: (Column[Int], Column[(Int, String)], (Column[Int], Column[Option[Double]]))* - Linearized: (Int, Int, String, Int, Option[Double])*/
abstract class Shape[Level <: ShapeLevel, -Mixed, Unpacked_, Packed_] {...}
import scala.reflect.ClassTag// A custom record classcase class Pair[A, B](a: A, b: B)// A Shape implementation for Pairfinal class PairShape[Level <: ShapeLevel, M <: Pair[_,_], U <: Pair[_,_] : ClassTag, P <: Pair[_,_]](val shapes: Seq[Shape[_, _, _, _]])extends MappedScalaProductShape[Level, Pair[_,_], M, U, P] {def buildValue(elems: IndexedSeq[Any]) = Pair(elems(0), elems(1))def copy(shapes: Seq[Shape[_ <: ShapeLevel, _, _, _]]) = new PairShape(shapes)}implicit def pairShape[Level <: ShapeLevel, M1, M2, U1, U2, P1, P2](implicit s1: Shape[_ <: Level, M1, U1, P1], s2: Shape[_ <: Level, M2, U2, P2]) = new PairShape[Level, Pair[M1, M2], Pair[U1, U2], Pair[P1, P2]](Seq(s1, s2))// Use it in a table definitionclass A(tag: Tag) extends Table[Pair[Int, String]](tag, "shape_a") {def id = column[Int]("id", O.PrimaryKey)def s = column[String]("s")def * = Pair(id, s)}val as = TableQuery[A]
trait TupleShapeImplicits {@inlineimplicit final def tuple1Shape[Level <: ShapeLevel, M1, U1, P1](implicit u1: Shape[_ <: Level, M1, U1, P1]): Shape[Level, Tuple1[M1], Tuple1[U1], Tuple1[P1]] =new TupleShape[Level, Tuple1[M1], Tuple1[U1], Tuple1[P1]](u1)@inlineimplicit final def tuple2Shape[Level <: ShapeLevel, M1,M2, U1,U2, P1,P2](implicit u1: Shape[_ <: Level, M1, U1, P1], u2: Shape[_ <: Level, M2, U2, P2]): Shape[Level, (M1,M2), (U1,U2), (P1,P2)] =new TupleShape[Level, (M1,M2), (U1,U2), (P1,P2)](u1,u2)
...
class TupleTypedPerson(tag: Tag) extends Table[(Option[Int],String,Int,Option[String])](tag,"PERSON") {def id = column[Int]("id",O.PrimaryKey,O.AutoInc)def name = column[String]("name")def age = column[Int]("age")def alias = column[Option[String]]("alias")def * = (id.?,name,age,alias)}val tupleTypedPerson = TableQuery[TupleTypedPerson]val db = Database.forURL("jdbc:h2:mem:test1;DB_CLOSE_DELAY=-1", driver = "org.h2.Driver")val createSchemaAction = tupleTypedPerson.schema.createAwait.ready(db.run(createSchemaAction),Duration.Inf)val initDataAction = DBIO.seq {tupleTypedPerson ++= Seq((Some(0),"Tiger Chan", 45, Some("Tiger_XC")),(Some(0),"Johnny Cox", 17, None),(Some(0),"Cathy Williams", 18, Some("Catty")),(Some(0),"David Wong", 43, None))}Await.ready(db.run(initDataAction),Duration.Inf)val queryAction = tupleTypedPerson.resultAwait.result(db.run(queryAction),Duration.Inf).foreach {row =>println(s"${row._1.get} ${row._2} ${row._4.getOrElse("")}, ${row._3}")}
class Person(val id: Option[Int], val name: String, val age: Int, val alias: Option[String])def toPerson(t: (Option[Int],String,Int,Option[String])) = new Person (t._1,t._2,t._3,t._4)def fromPerson(p: Person) = Some((p.id,p.name,p.age,p.alias))class TupleMappedPerson(tag: Tag) extends Table[Person](tag,"PERSON") {def id = column[Int]("id",O.PrimaryKey,O.AutoInc)def name = column[String]("name")def age = column[Int]("age")def alias = column[Option[String]]("alias")def * = (id.?,name,age,alias) <> (toPerson,fromPerson)}val tupleMappedPerson = TableQuery[TupleMappedPerson]Await.result(db.run(tupleMappedPerson.result),Duration.Inf).foreach {row =>println(s"${row.id.get} ${row.name} ${row.alias.getOrElse("")}, ${row.age}")}
我们用<>函数进行了Tuple=>Person转换。注意toPerson和fromPerson这两个相互转换函数。如果Person是个case class,那么Person.tupled和Person.unapply就是它自备的转换函数,我们可以用case class来构建MappedProjection:
case class Person(id: Option[Int]=None, name: String, age: Int, alias: Option[String])class MappedTypePerson(tag: Tag) extends Table[Person](tag,"PERSON") {def id = column[Int]("id",O.PrimaryKey,O.AutoInc)def name = column[String]("name")def age = column[Int]("age")def alias = column[Option[String]]("alias")def * = (id.?,name,age,alias) <> (Person.tupled,Person.unapply)}val mappedPeople = TableQuery[MappedTypePerson]
从上面两个例子里我们似乎可以得出ProvenShape[T]的T类型就是Table[T]的T,也就是返回结果行的类型了。我们可以用同样方式来进行HList与Person转换:
def hlistToPerson(hl: Option[Int]::String::Int::(Option[String])::HNil) =new Person(hl(0),hl(1),hl(2),hl(3))def personToHList(p: Person) = Some(p.id::p.name::p.age::p.alias::HNil)class HListPerson(tag: Tag) extends Table[Person](tag,"PERSON") {def id = column[Int]("id",O.PrimaryKey,O.AutoInc)def name = column[String]("name")def age = column[Int]("age")def alias = column[Option[String]]("alias")def * = (id.?)::name::age::alias::HNil <> (hlistToPerson,personToHList)}val hlistPerson = TableQuery[HListPerson]Await.result(db.run(hlistPerson.result),Duration.Inf).foreach {row =>println(s"${row.id.get} ${row.name} ${row.alias.getOrElse("")}, ${row.age}")}
case class YR(name: String, yr: Int)val qYear = for {p <- hlistPerson} yield ((p.name, p.age) <> (YR.tupled,YR.unapply))Await.result(db.run(qYear.result),Duration.Inf).foreach {row =>println(s"${row.name} ${row.yr}")}
case class Title(id: Int, title: String)class PersonTitle(tag: Tag) extends Table[Title](tag,"TITLE") {def id = column[Int]("id")def title = column[String]("title")def * = (id,title) <> (Title.tupled,Title.unapply)}val personTitle = TableQuery[PersonTitle]val createTitleAction = personTitle.schema.createAwait.ready(db.run(createTitleAction),Duration.Inf)val initTitleData = DBIO.seq {personTitle ++= Seq(Title(1,"Manager"),Title(2,"Programmer"),Title(3,"Clerk"))}Await.ready(db.run(initTitleData),Duration.Inf)case class Titles(id: Int, name: String, title: String)val qPersonWithTitle = for {p <- hlistPersont <- personTitle if p.id === t.id} yield ((p.id,p.name,t.title) <> (Titles.tupled,Titles.unapply))Await.result(db.run(qPersonWithTitle.result),Duration.Inf).foreach {row =>println(s"${row.id} ${row.name}, ${row.title}")}
下面是本次示范的源代码:
import slick.collection.heterogeneous.{ HList, HCons, HNil }
import slick.collection.heterogeneous.syntax._
import slick.driver.H2Driver.api._import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}object chkProjection {class TupleTypedPerson(tag: Tag) extends Table[(Option[Int],String,Int,Option[String])](tag,"PERSON") {def id = column[Int]("id",O.PrimaryKey,O.AutoInc)def name = column[String]("name")def age = column[Int]("age")def alias = column[Option[String]]("alias")def * = (id.?,name,age,alias)}val tupleTypedPerson = TableQuery[TupleTypedPerson]val db = Database.forURL("jdbc:h2:mem:test1;DB_CLOSE_DELAY=-1", driver = "org.h2.Driver")val createSchemaAction = tupleTypedPerson.schema.createAwait.ready(db.run(createSchemaAction),Duration.Inf)val initDataAction = DBIO.seq {tupleTypedPerson ++= Seq((Some(0),"Tiger Chan", 45, Some("Tiger_XC")),(Some(0),"Johnny Cox", 17, None),(Some(0),"Cathy Williams", 18, Some("Catty")),(Some(0),"David Wong", 43, None))}Await.ready(db.run(initDataAction),Duration.Inf)val queryAction = tupleTypedPerson.resultAwait.result(db.run(queryAction),Duration.Inf).foreach {row =>println(s"${row._1.get} ${row._2} ${row._4.getOrElse("")}, ${row._3}")}class Person(val id: Option[Int],val name: String, val age: Int, val alias: Option[String])def toPerson(t: (Option[Int],String,Int,Option[String])) = new Person (t._1,t._2,t._3,t._4)def fromPerson(p: Person) = Some((p.id,p.name,p.age,p.alias))class TupleMappedPerson(tag: Tag) extends Table[Person](tag,"PERSON") {def id = column[Int]("id",O.PrimaryKey,O.AutoInc)def name = column[String]("name")def age = column[Int]("age")def alias = column[Option[String]]("alias")def * = (id.?,name,age,alias) <> (toPerson,fromPerson)}val tupleMappedPerson = TableQuery[TupleMappedPerson]Await.result(db.run(tupleMappedPerson.result),Duration.Inf).foreach {row =>println(s"${row.id.get} ${row.name} ${row.alias.getOrElse("")}, ${row.age}")}def hlistToPerson(hl: Option[Int]::String::Int::(Option[String])::HNil) =new Person(hl(0),hl(1),hl(2),hl(3))def personToHList(p: Person) = Some(p.id::p.name::p.age::p.alias::HNil)class HListPerson(tag: Tag) extends Table[Person](tag,"PERSON") {def id = column[Int]("id",O.PrimaryKey,O.AutoInc)def name = column[String]("name")def age = column[Int]("age")def alias = column[Option[String]]("alias")def * = (id.?)::name::age::alias::HNil <> (hlistToPerson,personToHList)}val hlistPerson = TableQuery[HListPerson]Await.result(db.run(hlistPerson.result),Duration.Inf).foreach {row =>println(s"${row.id.get} ${row.name} ${row.alias.getOrElse("")}, ${row.age}")}case class YR(name: String, yr: Int)val qYear = for {p <- hlistPerson} yield ((p.name, p.age) <> (YR.tupled,YR.unapply))Await.result(db.run(qYear.result),Duration.Inf).foreach {row =>println(s"${row.name} ${row.yr}")}case class Title(id: Int, title: String)class PersonTitle(tag: Tag) extends Table[Title](tag,"TITLE") {def id = column[Int]("id")def title = column[String]("title")def * = (id,title) <> (Title.tupled,Title.unapply)}val personTitle = TableQuery[PersonTitle]val createTitleAction = personTitle.schema.createAwait.ready(db.run(createTitleAction),Duration.Inf)val initTitleData = DBIO.seq {personTitle ++= Seq(Title(1,"Manager"),Title(2,"Programmer"),Title(3,"Clerk"))}Await.ready(db.run(initTitleData),Duration.Inf)case class Titles(id: Int, name: String, title: String)val qPersonWithTitle = for {p <- hlistPersont <- personTitle if p.id === t.id} yield ((p.id,p.name,t.title) <> (Titles.tupled,Titles.unapply))Await.result(db.run(qPersonWithTitle.result),Duration.Inf).foreach {row =>println(s"${row.id} ${row.name}, ${row.title}")}}
这篇关于细谈Slick(6)- Projection:ProvenShape,强类型的Query结果类型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!