Skip to content

Commit

Permalink
Add Option[Instant] Quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejjozwik committed Oct 1, 2020
1 parent 80bc0d9 commit 53918e5
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ trait DateQuotes {
def <=(right: LocalDateTime) = quote(infix"$left <= $right".as[Boolean])
}

implicit class LocalDateTimeQuotesOption(left: Option[LocalDateTime]) {
def >(right: Option[LocalDateTime]) = quote(infix"$left > $right".as[Boolean])

def <(right: Option[LocalDateTime]) = quote(infix"$left < $right".as[Boolean])

def >=(right: Option[LocalDateTime]) = quote(infix"$left >= $right".as[Boolean])

def <=(right: Option[LocalDateTime]) = quote(infix"$left <= $right".as[Boolean])
}

implicit class LocalDateQuotes(left: LocalDate) {
def >(right: LocalDate) = quote(infix"$left > $right".as[Boolean])

Expand All @@ -32,6 +42,16 @@ trait DateQuotes {
def <=(right: LocalDate) = quote(infix"$left <= $right".as[Boolean])
}

implicit class LocalDateQuotesOption(left: Option[LocalDate]) {
def >(right: Option[LocalDate]) = quote(infix"$left > $right".as[Boolean])

def <(right: Option[LocalDate]) = quote(infix"$left < $right".as[Boolean])

def >=(right: Option[LocalDate]) = quote(infix"$left >= $right".as[Boolean])

def <=(right: Option[LocalDate]) = quote(infix"$left <= $right".as[Boolean])
}

implicit class InstantQuotes(left: Instant) {
def >(right: Instant) = quote(infix"$left > $right".as[Boolean])

Expand All @@ -41,5 +61,15 @@ trait DateQuotes {

def <=(right: Instant) = quote(infix"$left <= $right".as[Boolean])
}

implicit class InstantQuotesOption(left: Option[Instant]) {
def >(right: Option[Instant]) = quote(infix"$left > $right".as[Boolean])

def <(right: Option[Instant]) = quote(infix"$left < $right".as[Boolean])

def >=(right: Option[Instant]) = quote(infix"$left >= $right".as[Boolean])

def <=(right: Option[Instant]) = quote(infix"$left <= $right".as[Boolean])
}
//scalastyle:on
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pl.jozwik.quillgeneric.model

import java.time.LocalDateTime
import java.time.Instant

import pl.jozwik.quillgeneric.quillmacro.WithId

Expand All @@ -16,6 +16,6 @@ final case class Address(
city: String,
street: Option[String] = None,
buildingNumber: Option[String] = None,
updated: Option[LocalDateTime] = None,
updated: Option[Instant] = None,
localNumber: Option[String] = None
) extends WithId[AddressId]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pl.jozwik.quillgeneric.sync.jdbc

import java.time.Instant

import pl.jozwik.quillgeneric.sync.jdbc.repository.AddressRepository
import pl.jozwik.quillgeneric.model.{ Address, AddressId }
import org.scalatest.TryValues._

trait AddressSuite extends AbstractJdbcSpec {
private val repository = new AddressRepository(ctx)

"AddressSuite " should {
"Call crud operations " in {
val id = AddressId(1)
val entity = Address(id, "Country", "City")
val address = repository.createOrUpdateAndRead(entity).success.value
address shouldBe entity
val a = repository.searchUpdateAfter(Instant.now).success.value
a shouldBe empty
repository.updateAndRead(address.copy(updated = Option(Instant.now)))
val b = repository.searchUpdateAfter(Instant.EPOCH).success.value
b should not be empty
repository.deleteAll shouldBe Symbol("success")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package pl.jozwik.quillgeneric.sync.jdbc

class QuillCrudJdbcSpec
extends Cell4dSuite
with PersonRepositoryNotGeneratedIdSuite
with PersonCustomRepositorySuite
with PersonRepositorySuite
with ConfigurationRepositorySuite
with SaleRepositorySuite
class QuillCrudJdbcSpec extends AddressSuite
// with PersonRepositoryNotGeneratedIdSuite
// with PersonCustomRepositorySuite
// with PersonRepositorySuite
// with ConfigurationRepositorySuite
// with SaleRepositorySuite
// with Cell4dSuite
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pl.jozwik.quillgeneric.sync.jdbc.repository

import java.time.Instant

import io.getquill.NamingStrategy
import io.getquill.context.sql.idiom.SqlIdiom
import pl.jozwik.quillgeneric.model.{ Address, AddressId }
import pl.jozwik.quillgeneric.quillmacro.sync.JdbcRepositoryWithGeneratedId

import scala.util.Try

trait AbstractAddressRepository[D <: SqlIdiom, N <: NamingStrategy] extends JdbcRepositoryWithGeneratedId[AddressId, Address, D, N] {

def searchUpdateAfter(updated: Instant): Try[Seq[Address]] =
Try {
import context._
val q = dynamicSchema
.filter(a => quote(a.updated > lift(Option(updated))))
run(q)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import io.getquill.NamingStrategy
import io.getquill.context.sql.idiom.SqlIdiom
import pl.jozwik.quillgeneric.model.{ Address, AddressId }
import pl.jozwik.quillgeneric.quillmacro.sync.JdbcRepository.JdbcContextDateQuotes
import pl.jozwik.quillgeneric.quillmacro.sync.JdbcRepositoryWithGeneratedId

import scala.util.Try

final class AddressRepository[D <: SqlIdiom, N <: NamingStrategy](
protected val context: JdbcContextDateQuotes[D, N],
tableName: String
) extends JdbcRepositoryWithGeneratedId[AddressId, Address, D, N] {
tableName: String = "Address"
) extends AbstractAddressRepository[D, N] {

protected implicit val dynamicSchema: context.DynamicEntityQuery[Address] = context.dynamicQuerySchema[Address](tableName)

Expand Down Expand Up @@ -72,15 +71,17 @@ final class AddressRepository[D <: SqlIdiom, N <: NamingStrategy](
}
}

override def update(entity: Address): Try[Long] = Try {
context.update[AddressId, Address](entity)
}
override def update(entity: Address): Try[Long] =
Try {
context.update[AddressId, Address](entity)
}

override def updateAndRead(entity: Address): Try[Address] = Try {
context.transaction {
context.updateAndRead[AddressId, Address](entity)
override def updateAndRead(entity: Address): Try[Address] =
Try {
context.transaction {
context.updateAndRead[AddressId, Address](entity)
}
}
}

override def delete(id: AddressId): Try[Long] =
Try {
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ThisBuild / version := "0.9.1-SNAPSHOT"
ThisBuild / version := "0.9.1"

0 comments on commit 53918e5

Please sign in to comment.