Scala Slick 2.0:如何将提升的查询结果转换为案例类?
为了实现ReSTfull API堆栈,我需要将从DB提取的数据转换为JSON格式。我认为最好的方法是从数据库中提取数据,然后在定义隐式序列化程序(writes)后,使用JSON.toJson()作为参数传递case类,将行集转换为JSON 下面是我的case类和伴随对象:Scala Slick 2.0:如何将提升的查询结果转换为案例类?,scala,slick,Scala,Slick,为了实现ReSTfull API堆栈,我需要将从DB提取的数据转换为JSON格式。我认为最好的方法是从数据库中提取数据,然后在定义隐式序列化程序(writes)后,使用JSON.toJson()作为参数传递case类,将行集转换为JSON 下面是我的case类和伴随对象: package deals.db.interf.slick2 import scala.slick.driver.MySQLDriver.simple._ import play.api.libs.json.Json ca
package deals.db.interf.slick2
import scala.slick.driver.MySQLDriver.simple._
import play.api.libs.json.Json
case class PartnerInfo(
id: Int,
name: String,
site: String,
largeLogo: String,
smallLogo: String,
publicationSite: String
)
object PartnerInfo {
def toCaseClass( ?? ) = { // what type are the arguments to be passed?
PartnerInfo( fx(??) ) // how to transform the input types (slick) to Scala types?
}
// Notice I'm using slick 2.0.0 RC1
class PartnerInfoTable(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "PARTNER"){
def id = column[Int]("id")
def name = column[String]("name")
def site = column[String]("site")
def largeLogo = column[String]("large_logo")
def smallLogo = column[String]("small_logo")
def publicationSite = column[String]("publication_site")
def * = (id, name, site, largeLogo, smallLogo, publicationSite)
}
val partnerInfos = TableQuery[PartnerInfoTable]
def qPartnerInfosForPuglisher(publicationSite: String) = {
for (
pi <- partnerInfos if ( pi.publicationSite == publicationSite )
) yield toCaseClass( _ ) // Pass all the table columns to toCaseClass()
}
implicit val partnerInfoWrites = Json.writes[PartnerInfo]
}
package deals.db.interf.2
导入scala.slick.driver.MySQLDriver.simple_
导入play.api.libs.json.json
案例类PartnerInfo(
id:Int,
名称:String,
地点:String,,
拉格洛戈:字符串,
smallLogo:String,
publicationSite:String
)
对象伙伴信息{
def toCaseClass(??={//要传递的参数是什么类型?
PartnerInfo(fx(??)//如何将输入类型(slick)转换为Scala类型?
}
//注意,我使用的是slick 2.0.0 RC1
类PartnerInfoTable(tag:tag)扩展表[(Int,String,String,String,String)](tag,“PARTNER”){
def id=列[Int](“id”)
def名称=列[字符串](“名称”)
def site=列[字符串](“站点”)
def largeLogo=列[String](“大的_标志”)
def smallLogo=列[String](“small_logo”)
def publicationSite=列[String](“发布站点”)
def*=(id、名称、站点、largeLogo、smallLogo、publicationSite)
}
val partnerInfos=TableQuery[PartnerInfoTable]
def qPartnerInfosForPuglisher(publicationSite:String)={
对于(
pi我认为这里最简单的方法是在表模式中映射PartnerInfo
:
class PartnerInfoTable(tag: Tag) extends Table[PartnerInfo](tag, "PARTNER"){
def id = column[Int]("id")
def name = column[String]("name")
def site = column[String]("site")
def largeLogo = column[String]("large_logo")
def smallLogo = column[String]("small_logo")
def publicationSite = column[String]("publication_site")
def * = (id, name, site, largeLogo, smallLogo, publicationSite) <> (PartnerInfo.tupled, PartnerInfo.unapply)
}
val partnerInfos = TableQuery[PartnerInfoTable]
def qPartnerInfosForPuglisher(publicationSite: String) = {
for (
pi <- partnerInfos if ( pi.publicationSite == publicationSite )
) yield pi
}
def toCaseClass(pi:(Int, String, String, String, String, String)) = PartnerInfo.tupled(pi)