I want to use some extra features of PostgreSQL in my code but I don't want to fill the place with SQL string interpolations.
Currently I have:
/** Use 'now()' through Slick. */
val psqlNow = SimpleFunction.nullary[java.sql.Date]("now")
//Not really my code, but we only care for 2 lines.
def aQuery(limiter: Column[Int]) = {
myTable
.filter(_.validFrom >= psqlNow)
.filter(_.validUntil <= psqlNow)
.filter(_.fakeId === limiter).map(e => (e.fakeId, e.name)
}
But I want to use 'CURRENT_DATE', which I is a literal (and using it in place of "now" throws an exception). Can someone provide an actual example, because I can't get this to compile:
/** Use 'CURRENT_DATE' through Slick. */
val psqlNow = SimpleLiteral("CURRENT_DATE")(...WHAT GOES HERE?...)
//Not really my code, but we only care for 2 lines.
def aQuery(limiter: Column[Int]) = {
myTable
.filter(_.validFrom >= psqlNow)
.filter(_.validUntil <= psqlNow)
.filter(_.fakeId === limiter).map(e => (e.fakeId, e.name)
}
And I also want to change the following to lifted Slick, can I do it with SimpleLiteral (to somehow put 'count(*) OVER() recordsFiltered' into the generated query?
SELECT *, count(*) OVER() recordsFiltered FROM example
WHERE id = $1
The examples are trivial, the actual code is a series of folds over filtering criteria.
import scala.slick.ast.TypedType
val current_date = Column.forNode[java.sql.Date](new SimpleLiteral("CURRENT_DATE")(implicitly[TypedType[java.sql.Date]]))
does the trick. Better support is missing at the moment.
I added a PR, so in Slick 2.2 it will be supported like this:
val current_date = SimpleLiteral[java.sql.Date]("CURRENT_DATE")
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments