Last active
February 12, 2019 09:16
-
-
Save koenkarsten/ebe04cb802c2e39546d5e46c9225b10f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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