I have a query that returns Person
objects that I’m using to fetch one page of results from the database:
def page(pageNumber:Int, pageSize:Int) : Seq[Person] = database.withSession {
val query = for(person <- People) yield person.mapped
val startIndex = (pageNumber - 1) * pageSize
query.list.slice(startIndex, startIndex + pageSize)
}
This works, but I want to know if I can do the paging in the database, in the same way as with the javax.persistence.Query
API's setFirstResult
and setMaxResults
methods, instead of using slice
on the resulting list.
The tests suggest using normal Scala collection methods like
drop
andtake
. Those will efficiently handle the JDBC ResultSet. It also looks like the appropriateLIMIT
andOFFSET
are added to the query.Pipelines of operations on Scala collections may look like they're just filtering actual data, but as you can see here information can also travel back up the pipeline to make the computation as a whole more efficient.
Demonstration:
Note that you need to import one of the specific driver implicits to make this work. See the end of the getting started guide for a list of driver names.