|
@@ -9,7 +9,7 @@ import cc.otavia.core.stack.helper.{FutureState, FuturesState, StartState}
|
|
|
import cc.otavia.core.stack.{AskStack, StackState, StackYield}
|
|
|
import cc.otavia.http.server.{HttpRequest, HttpResponse}
|
|
|
import cc.otavia.sql.Connection
|
|
|
-import cc.otavia.sql.Statement.{ModifyRows, PrepareQuery}
|
|
|
+import cc.otavia.sql.statement.{ModifyRows, PrepareQuery}
|
|
|
|
|
|
import java.util.SplittableRandom
|
|
|
|
|
@@ -22,20 +22,17 @@ class DBController extends StateActor[REQ] {
|
|
|
override protected def afterMount(): Unit = connection = autowire[Connection]()
|
|
|
|
|
|
override protected def resumeAsk(stack: AskStack[REQ & Ask[? <: Reply]]): StackYield =
|
|
|
- stack match
|
|
|
- case stack: AskStack[SingleQueryRequest] if stack.ask.isInstanceOf[SingleQueryRequest] =>
|
|
|
- handleSingleQuery(stack)
|
|
|
- case stack: AskStack[MultipleQueryRequest] if stack.ask.isInstanceOf[MultipleQueryRequest] =>
|
|
|
- handleMultipleQuery(stack)
|
|
|
- case stack: AskStack[UpdateRequest] if stack.ask.isInstanceOf[UpdateRequest] =>
|
|
|
- handleUpdateQuery(stack)
|
|
|
+ stack.ask match
|
|
|
+ case _: SingleQueryRequest => handleSingleQuery(stack.asInstanceOf[AskStack[SingleQueryRequest]])
|
|
|
+ case _: MultipleQueryRequest => handleMultipleQuery(stack.asInstanceOf[AskStack[MultipleQueryRequest]])
|
|
|
+ case _: UpdateRequest => handleUpdateQuery(stack.asInstanceOf[AskStack[UpdateRequest]])
|
|
|
|
|
|
// Test 2: Single database query
|
|
|
private def handleSingleQuery(stack: AskStack[SingleQueryRequest]): StackYield = {
|
|
|
stack.state match
|
|
|
case _: StartState =>
|
|
|
val state = FutureState[World]()
|
|
|
- connection.ask(PrepareQuery.fetchOne[World](SELECT_WORLD, Tuple1(randomWorld())), state.future)
|
|
|
+ connection.ask(PrepareQuery.fetchOne[World](SELECT_WORLD, randomWorld()), state.future)
|
|
|
stack.suspend(state)
|
|
|
case state: FutureState[World] =>
|
|
|
stack.`return`(state.future.getNow)
|
|
@@ -61,7 +58,7 @@ class DBController extends StateActor[REQ] {
|
|
|
stack.attach(worlds)
|
|
|
val newState = FutureState[ModifyRows]()
|
|
|
val newWorlds = worlds.sortBy(_.id).map(_.copy(randomNumber = randomWorld()))
|
|
|
- connection.ask(PrepareQuery.update(UPDATE_WORLD, newWorlds), newState.future)
|
|
|
+ connection.ask(PrepareQuery.updateBatch(UPDATE_WORLD, newWorlds), newState.future)
|
|
|
stack.suspend(newState)
|
|
|
case state: FutureState[ModifyRows] =>
|
|
|
if (state.future.isFailed) state.future.causeUnsafe.printStackTrace()
|
|
@@ -72,7 +69,7 @@ class DBController extends StateActor[REQ] {
|
|
|
private def selectWorlds(queries: Int): StackState = {
|
|
|
val state = FuturesState[World](queries)
|
|
|
for (future <- state.futures)
|
|
|
- connection.ask(PrepareQuery.fetchOne[World](SELECT_WORLD, Tuple1(randomWorld())), future)
|
|
|
+ connection.ask(PrepareQuery.fetchOne[World](SELECT_WORLD, randomWorld()), future)
|
|
|
state
|
|
|
}
|
|
|
|