Skip to content

Instantly share code, notes, and snippets.

@koenkarsten
Last active February 12, 2019 09:16
Show Gist options
  • Save koenkarsten/ebe04cb802c2e39546d5e46c9225b10f to your computer and use it in GitHub Desktop.
Save koenkarsten/ebe04cb802c2e39546d5e46c9225b10f to your computer and use it in GitHub Desktop.
import java.time.Instant
import io.getquill.{MappedEncoding, LowerCase, MySQLDialect, JdbcContext}
import io.getquill.context.sql.SqlContext
import java.util.Date
trait Encoders {
implicit val instantEncoder = MappedEncoding[Instant, Date](i => Date.from(i))
}
trait Decoders {
implicit val instantDecoder = MappedEncoding[Date, Instant](d => d.toInstant)
}
trait Quotes {
this: SqlContext[_, _] =>
implicit class TimestampQuotes(left: Instant) {
def >(right: Instant) = quote(infix"$left > $right".as[Boolean])
def <(right: Instant) = quote(infix"$left < $right".as[Boolean])
def >=(right: Instant) = quote(infix"$left >= $right".as[Boolean])
def <=(right: Instant) = quote(infix"$left <= $right".as[Boolean])
}
}
case class User(id: Int, name: String, timestamp: Instant)
lazy val ctx = new JdbcContext[MySQLDialect, LowerCase]("ctx") with Encoders with Decoders with Quotes
import ctx._
val start = Instant.parse("2016-12-01T00:00:00.000Z")
val end = Instant.now()
val q = quote {
query[User].filter(u => u.timestamp >= start && u.timestamp < end)
}
ctx.run(q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment