I have the following SLICK query to get paged results for a data table with the name field matching some value criteria and sorted by the name column
val q = ThirdParties.where(_.name like criteria).sortBy(_.name.asc.nullsLast).drop(offset).take(pageSize)
val thirdParties = (for(s <-q) yield(s)).list map { case t: ThirdParty => t }
This works ok for me, but now I need to be able to pass a runtime parameter to the sortBy method that identifies the column on which to perform the sort by.
My method that calls the query will have an int that represents the index of the column in the data table.
How might I go about getting from an int column index to the necessary type required by the sortBy method?
You'll be losing some type safety by doing this, but maybe an approach like this would hurt the least:
This is with the coffee example from the Slick documentation. Let's assume you want a subset of your columns to addressed by 'index'. In our example, let's sat that we have for some reason, 2 price
Int
columns and also the sales column which we address as column0
,1
or2
. . If you can put up with a minor infraction of DRY, such as this:Here
Coffees.nth
is a vector of columns, of bothInt
andDouble
.Of course, picking the column to sort of at runtime implies that you have to deal with bogus column indexes - if you have only
k
columns and you ask for thek+1
th column you either have to throw an exception, or silently pick a default column. That's a consequence of wanting to translate a dynamic input into what is usually static (and type safe).If you're OK with an exception for a bogus column index, then (going back to your example)
Then to call the query