How to understand result parser of play2's anorm?
-
22-04-2021 - |
Question
This is a sample code from play2's built-in's zentasks:
object Project {
// -- Parsers
/**
* Parse a Project from a ResultSet
*/
val simple = {
get[Pk[Long]]("project.id") ~
get[String]("project.folder") ~
get[String]("project.name") map {
case id~folder~name => Project(id, folder, name)
}
}
}
Please notice the field keys: project.id
, project.folder
, project.name
.
What does the project
part mean? Where is it come from?
Look at the query method:
def findById(id: Long): Option[Project] = {
DB.withConnection { implicit connection =>
SQL("select * from project where id = {id}").on(
'id -> id
).as(Project.simple.singleOpt)
}
}
The SQL is select * from project ...
, the result should be:
id | folder | name
Not:
project.id | project.folder | project.name
Why we should specify the keys as project.???
, but not field name
directly?
How play2 use the project.
part?
La solution
As per the code available in here, Anorm Parser uses the string provided to retrieve the given column.
As it gets some metadata, it requires teh full scope of the field (table + name) to work.
Autres conseils
IMHO it's just DB alias.
select id, folder, name from project
is the same as
select project.id, project.folder, project.name from project
or even
select p.id, p.folder, p.name from project p
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow