Skip to content

Commit

Permalink
Merge pull request #996 from mentegy/fix-async-regres
Browse files Browse the repository at this point in the history
Return failed future instead of exception in cassandra async prepare
  • Loading branch information
fwbrasil authored Dec 15, 2017
2 parents d67c733 + 2d47a9b commit 46ed299
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,23 @@ class CassandraAsyncContext[N <: NamingStrategy](
super.performIO(io)
}

def executeQuery[T](cql: String, prepare: Prepare = identityPrepare, extractor: Extractor[T] = identityExtractor)(implicit ec: ExecutionContext): Future[List[T]] = {
val (params, bs) = prepare(super.prepare(cql))
logger.logQuery(cql, params)
session.executeAsync(bs)
.map(_.all.asScala.toList.map(extractor))
}
def executeQuery[T](cql: String, prepare: Prepare = identityPrepare, extractor: Extractor[T] = identityExtractor)(implicit ec: ExecutionContext): Future[List[T]] =
Future(prepare(super.prepare(cql))).flatMap {
case (params, bs) =>
logger.logQuery(cql, params)
session.executeAsync(bs)
.map(_.all.asScala.toList.map(extractor))
}

def executeQuerySingle[T](cql: String, prepare: Prepare = identityPrepare, extractor: Extractor[T] = identityExtractor)(implicit ec: ExecutionContext): Future[T] =
executeQuery(cql, prepare, extractor).map(handleSingleResult)

def executeAction[T](cql: String, prepare: Prepare = identityPrepare)(implicit ec: ExecutionContext): Future[Unit] = {
val (params, bs) = prepare(super.prepare(cql))
logger.logQuery(cql, params)
session.executeAsync(bs).map(_ => ())
Future(prepare(super.prepare(cql))).flatMap {
case (params, bs) =>
logger.logQuery(cql, params)
session.executeAsync(bs).map(_ => ())
}
}

def executeBatchAction(groups: List[BatchGroup])(implicit ec: ExecutionContext): Future[Unit] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.getquill.context.cassandra
import io.getquill._

import scala.concurrent.ExecutionContext.Implicits.{ global => ec }
import scala.util.Success
import scala.util.{ Success, Try }

class CassandraContextSpec extends Spec {

Expand Down Expand Up @@ -39,4 +39,17 @@ class CassandraContextSpec extends Spec {
"probe" in {
testSyncDB.probe("SELECT * FROM TestEntity") mustBe Success(())
}

"return failed future on `prepare` error in async context" - {
"query" - {
val f = testAsyncDB.executeQuery("bad cql")
Try(await(f)).isFailure mustEqual true
()
}
"action" - {
val f = testAsyncDB.executeAction("bad cql")
Try(await(f)).isFailure mustEqual true
()
}
}
}

0 comments on commit 46ed299

Please sign in to comment.