可变列数量 Anorm 结果集解析器
variable columns amount Anorm result set parser
我正在试验 scala,这是我的堆栈:
- Windows 10
- JDK 1.8
- Scala 2.11.8
- PlayFramework 2.5
- 异常 2.4.0
- MySql
5.5
我想用 Anorm 创建一个结果集解析器,它将能够解析我 select 来自给定 table 的任意数量的列。这是我的代码:
case class Campaign(id: Int, campaign_mode_id: Int, name: String)
class Application @Inject()(db: Database) extends Controller {
val campaign = {
get[Int]("campaign.id") ~
get[Int]("campaign.campaign_mode_id") ~
get[String]("campaign.name") map {
case id ~ campaign_mode_id ~ name => Campaign(id, campaign_mode_id, name)
}
}
def index = Action {
val data : List[Campaign] = db.withConnection { implicit connection =>
SQL("SELECT id, campaign_mode_id, name FROM campaign").as(campaign.*)
}
Ok(views.html.index(data))
}
}
我希望能够 运行 跟随 Sqls 并且仍然使用相同的 case class Campaign
:
SELECT id, name FROM campaign
SELECT name FROM campaign
如何实现此功能?
更新:
我更新了我的代码,将某些字段的类型更改为选项:
case class Campaign(id: Int, campaign_mode_id: Option[Int], name: Option[String])
class Application @Inject()(db: Database) extends Controller {
val campaign = {
get[Int]("campaign.id") ~
get[Option[Int]]("campaign.campaign_mode_id") ~
get[Option[String]]("campaign.name") map {
case id ~ campaign_mode_id ~ name => Campaign(id, campaign_mode_id, name)
}
}
def index = Action {
val data : List[Campaign] = db.withConnection { implicit connection =>
SQL("SELECT id, name FROM campaign").as(campaign.*)
}
Ok(views.html.index(data))
}
}
此代码产生以下错误:
[RuntimeException: campaign.campaign_mode_id not found, available columns : campaign.id, id, campaign.name, name]
如果列 campaign_mode_id
可以为 null,解析为 Option[Int]
并且始终出现在结果集中,请使用:
get[Option[Int]]("campaign.campaign_mode_id")
如果列campaign_mode_id
不可为空,可能不会出现在结果集中,并解析为Option[Int]
,使用:
get[Int]("campaign.campaign_mode_id").?
在你的情况下,你似乎想要:
val campaign = {
get[Int]("campaign.id") ~
get[Int]("campaign.campaign_mode_id").? ~
get[String]("campaign.name").? map {
case id ~ campaign_mode_id ~ name => Campaign(id, campaign_mode_id, name)
}
}
这假设 campaign.campaign_mode_id
和 compaign.name
都是不可空的,但在结果集中是可选的。
我正在试验 scala,这是我的堆栈:
- Windows 10
- JDK 1.8
- Scala 2.11.8
- PlayFramework 2.5
- 异常 2.4.0
- MySql 5.5
我想用 Anorm 创建一个结果集解析器,它将能够解析我 select 来自给定 table 的任意数量的列。这是我的代码:
case class Campaign(id: Int, campaign_mode_id: Int, name: String)
class Application @Inject()(db: Database) extends Controller {
val campaign = {
get[Int]("campaign.id") ~
get[Int]("campaign.campaign_mode_id") ~
get[String]("campaign.name") map {
case id ~ campaign_mode_id ~ name => Campaign(id, campaign_mode_id, name)
}
}
def index = Action {
val data : List[Campaign] = db.withConnection { implicit connection =>
SQL("SELECT id, campaign_mode_id, name FROM campaign").as(campaign.*)
}
Ok(views.html.index(data))
}
}
我希望能够 运行 跟随 Sqls 并且仍然使用相同的 case class Campaign
:
SELECT id, name FROM campaign
SELECT name FROM campaign
如何实现此功能?
更新:
我更新了我的代码,将某些字段的类型更改为选项:
case class Campaign(id: Int, campaign_mode_id: Option[Int], name: Option[String])
class Application @Inject()(db: Database) extends Controller {
val campaign = {
get[Int]("campaign.id") ~
get[Option[Int]]("campaign.campaign_mode_id") ~
get[Option[String]]("campaign.name") map {
case id ~ campaign_mode_id ~ name => Campaign(id, campaign_mode_id, name)
}
}
def index = Action {
val data : List[Campaign] = db.withConnection { implicit connection =>
SQL("SELECT id, name FROM campaign").as(campaign.*)
}
Ok(views.html.index(data))
}
}
此代码产生以下错误:
[RuntimeException: campaign.campaign_mode_id not found, available columns : campaign.id, id, campaign.name, name]
如果列 campaign_mode_id
可以为 null,解析为 Option[Int]
并且始终出现在结果集中,请使用:
get[Option[Int]]("campaign.campaign_mode_id")
如果列campaign_mode_id
不可为空,可能不会出现在结果集中,并解析为Option[Int]
,使用:
get[Int]("campaign.campaign_mode_id").?
在你的情况下,你似乎想要:
val campaign = {
get[Int]("campaign.id") ~
get[Int]("campaign.campaign_mode_id").? ~
get[String]("campaign.name").? map {
case id ~ campaign_mode_id ~ name => Campaign(id, campaign_mode_id, name)
}
}
这假设 campaign.campaign_mode_id
和 compaign.name
都是不可空的,但在结果集中是可选的。