Skip to content

Commit

Permalink
Add readUnsafe to cassandra
Browse files Browse the repository at this point in the history
  • Loading branch information
ajozwik committed Aug 21, 2019
1 parent dc1c7b5 commit 43b76e2
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,18 @@ class AsyncCrudMacro(val c: MacroContext) extends AbstractAsyncCrudMacro {
"""
}

def readUnsafe[K: c.WeakTypeTag, T: c.WeakTypeTag](id: c.Expr[K])(dSchema: c.Expr[_], ex: c.Expr[ExecutionContext]): Tree = {
val filter = callFilterOnId[K](id)(dSchema)
q"""
import ${c.prefix}._
val q = $filter
for {
r <- run(q)
} yield {
r.headOption
.getOrElse(throw new NoSuchElementException(s"$$id"))
}
"""
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ trait AsyncCrudWithContext[U] {

def read[K, T <: WithId[K]](id: K)(implicit dSchema: dQuery[T], ex: ExecutionContext): Future[Option[T]] = macro AsyncCrudMacro.read[K, T]

def readUnsafe[K, T <: WithId[K]](id: K)(implicit dSchema: dQuery[T], ex: ExecutionContext): Future[T] = macro AsyncCrudMacro.readUnsafe[K, T]

def delete[K, T <: WithId[K]](id: K)(implicit dSchema: dQuery[T], ex: ExecutionContext): Future[U] = macro AsyncCrudMacro.delete[K]

def deleteAll[T](implicit dSchema: dQuery[T], ex: ExecutionContext): Future[U] = macro AsyncCrudMacro.deleteAll[T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ trait AsyncBaseRepository[K, T <: WithId[K]] {

def read(id: K)(implicit ex: ExecutionContext): Future[Option[T]]

def readUnsafe(id: K)(implicit ex: ExecutionContext): Future[T]

def update(t: T)(implicit ex: ExecutionContext): Future[U]

def updateAndRead(t: T)(implicit ex: ExecutionContext): Future[T]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package pl.jozwik.quillgeneric.cassandra.async

import pl.jozwik.quillgeneric.cassandra.async.repository.AddressAsyncRepository
import pl.jozwik.quillgeneric.cassandra.async.repository.AddressRepositoryGen
import pl.jozwik.quillgeneric.cassandra.model.AddressId

trait AddressSuite extends AbstractAsyncSpec {

private lazy val repository = new AddressAsyncRepository(ctx)
private lazy val repository = new AddressRepositoryGen(ctx)

"AddressSuite simple transformation" should {

Expand All @@ -17,6 +17,7 @@ trait AddressSuite extends AbstractAsyncSpec {
val all = repository.all.futureValue
logger.debug(s"$all")
repository.read(id).futureValue shouldBe Option(address)
repository.readUnsafe(id).futureValue shouldBe address
repository.all.futureValue shouldBe Seq(address)
repository.deleteAll.futureValue
repository.all.futureValue shouldBe Seq.empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,49 @@ import pl.jozwik.quillgeneric.quillmacro.cassandra.async.CassandraAsyncRepositor

import scala.concurrent.{ ExecutionContext, Future }

final class AddressAsyncRepository[Naming <: NamingStrategy](
final class AddressRepositoryGen[Naming <: NamingStrategy](
protected val context: CassandraAsyncContextDateQuotes[Naming],
protected val tableName: String = "Address"
) extends CassandraAsyncRepository[AddressId, Address, Naming] {

protected def dynamicSchema: context.DynamicEntityQuery[Address] = dSchema

private implicit val dSchema: context.DynamicEntityQuery[Address] = {
import context._
context.dynamicQuerySchema[Address](tableName, alias(_.city, "city"))

context.dynamicQuerySchema[Address](tableName)
}

override def all(implicit ec: ExecutionContext): Future[Seq[Address]] =
context.all[Address]
context.all[Address](dSchema, ec)

override def create(entity: Address)(implicit ec: ExecutionContext): Future[AddressId] =
context.create[AddressId, Address](entity)
context.create[AddressId, Address](entity)(dSchema, ec)

override def createAndRead(entity: Address)(implicit ec: ExecutionContext): Future[Address] =
context.createAndRead[AddressId, Address](entity)

override def read(id: AddressId)(implicit ec: ExecutionContext): Future[Option[Address]] =
context.read[AddressId, Address](id)

override def createOrUpdate(entity: Address)(implicit ec: ExecutionContext): Future[AddressId] =
context.create[AddressId, Address](entity)

override def createOrUpdateAndRead(entity: Address)(implicit ec: ExecutionContext): Future[Address] =
context.createAndRead[AddressId, Address](entity)

override def read(id: AddressId)(implicit ec: ExecutionContext): Future[Option[Address]] =
context.read[AddressId, Address](id)(dSchema, ec)

override def readUnsafe(id: AddressId)(implicit ec: ExecutionContext): Future[Address] =
context.readUnsafe[AddressId, Address](id)(dSchema, ec)

override def update(entity: Address)(implicit ec: ExecutionContext): Future[Unit] =
context.update[AddressId, Address](entity)
context.update[AddressId, Address](entity)(dSchema, ec)

override def updateAndRead(entity: Address)(implicit ec: ExecutionContext): Future[Address] =
context.updateAndRead[AddressId, Address](entity)

override def delete(id: AddressId)(implicit ec: ExecutionContext): Future[Unit] =
context.delete[AddressId, Address](id)
context.delete[AddressId, Address](id)(dSchema, ec)

override def deleteAll(implicit ec: ExecutionContext): Future[Unit] =
context.deleteAll
context.deleteAll(dSchema, ec)

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class ConfigurationAsyncRepository[D <: SqlIdiom, N <: NamingStrategy, C <: Conn
override def read(id: ConfigurationId)(implicit ex: ExecutionContext): Future[Option[Configuration]] =
context.read[ConfigurationId, Configuration](id)

override def readUnsafe(id: ConfigurationId)(implicit ex: ExecutionContext): Future[Configuration] =
context.readUnsafe[ConfigurationId, Configuration](id)

override def update(t: Configuration)(implicit ex: ExecutionContext): Future[U] =
context.update[ConfigurationId, Configuration](t)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class PersonAsyncRepository[D <: SqlIdiom, N <: NamingStrategy, C <: Connection]
override def read(id: PersonId)(implicit ex: ExecutionContext): Future[Option[Person]] =
context.read[PersonId, Person](id)

override def readUnsafe(id: PersonId)(implicit ex: ExecutionContext): Future[Person] =
context.readUnsafe[PersonId, Person](id)

override def update(t: Person)(implicit ex: ExecutionContext): Future[U] =
context.update[PersonId, Person](t)

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.8.1"
ThisBuild / version := "0.8.2"

0 comments on commit 43b76e2

Please sign in to comment.