I am new to Squeryl and trying to figure out how to cast the results I get back from a query into case class. I have something like this
def getUsers = {
val data = getUserData
...
}
def getUserData = {
transaction {
from(users)(s =>
select(s.id, s.firstName, s.lastName, s.userName, s.email, s.lastLoginDate, s.dateJoined)
)
}
}
case class UserData(userId: Long, firstName: String, lastName: String, userName: String, email: String, lastLoginDate: Timestamp, dateJoined: Timestamp)
case class UserDataRecords(users: List[UserData])
Ideally, I would like to get the data back in the from of UserDataRecords. Right now it is returned as Query[Tuple7]. Such as
...
(5,Suzie,Queue,squeue,SQueue@example.com,2014-01-15 22:02:12.0,2014-01-15 22:02:12.0)
...
What I cant figure out is how to cast this data. Any help on this would be great!
The simplest way would be to use map
to transform the result list. Something like the example below should be a starting point:
def getUserData = {
transaction {
from(users)(s =>
select(s.id, s.firstName, s.lastName, s.userName, s.email, s.lastLoginDate, s.dateJoined)
).toList.map { row =>
UserData(row._1, row._2, row._3, row._4, row._5, row._6, row._7)
}
}
}
This will iterate through each row and create a UserData
object out of the tuple, leaving you with List[UserData]
.
That said, if your schema users
is of type Table[UserData]
, then you could simply do:
from(users)(s => select(s))