MAPG-235 queries with selected relations enabled; reduced size of some of the queries

This commit is contained in:
Balázs Vigh 2021-05-19 13:06:46 +02:00
parent 7898c52328
commit a7cfc3c19f
7 changed files with 67 additions and 103 deletions

View File

@ -6,9 +6,13 @@ use MapGuesser\Util\Geo\Position;
use MapGuesser\Response\JsonContent; use MapGuesser\Response\JsonContent;
use MapGuesser\Interfaces\Response\IContent; use MapGuesser\Interfaces\Response\IContent;
use MapGuesser\Multi\MultiConnector; use MapGuesser\Multi\MultiConnector;
use MapGuesser\PersistentData\Model\Challenge;
use MapGuesser\PersistentData\PersistentDataManager; use MapGuesser\PersistentData\PersistentDataManager;
use MapGuesser\PersistentData\Model\Challenge;
use MapGuesser\PersistentData\Model\Guess; use MapGuesser\PersistentData\Model\Guess;
use MapGuesser\PersistentData\Model\Map;
use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\Model\PlaceInChallenge;
use MapGuesser\PersistentData\Model\User;
use MapGuesser\PersistentData\Model\UserPlayedPlace; use MapGuesser\PersistentData\Model\UserPlayedPlace;
use MapGuesser\Repository\ChallengeRepository; use MapGuesser\Repository\ChallengeRepository;
use MapGuesser\Repository\GuessRepository; use MapGuesser\Repository\GuessRepository;
@ -146,7 +150,8 @@ class GameFlowController
$currentPlace = $this->placeRepository->getByRoundInChallenge($challenge, $currentRound); $currentPlace = $this->placeRepository->getByRoundInChallenge($challenge, $currentRound);
if (!isset($currentPlace) || $withHistory) { if (!isset($currentPlace) || $withHistory) {
foreach ($this->guessRepository->getAllInChallenge($challenge) as $guess) { $withRelations = [User::class, PlaceInChallenge::class, Place::class];
foreach ($this->guessRepository->getAllInChallenge($challenge, $withRelations) as $guess) {
$round = $guess->getPlaceInChallenge()->getRound(); $round = $guess->getPlaceInChallenge()->getRound();
if ($guess->getUser()->getId() === $userId) { if ($guess->getUser()->getId() === $userId) {
@ -178,7 +183,7 @@ class GameFlowController
$prevRound = $currentRound - 1; $prevRound = $currentRound - 1;
if ($prevRound >= 0) { if ($prevRound >= 0) {
foreach ($this->guessRepository->getAllInChallengeByRound($prevRound, $challenge) as $guess) { foreach ($this->guessRepository->getAllInChallengeByRound($prevRound, $challenge, [User::class]) as $guess) {
if ($guess->getUser()->getId() != $userId) { if ($guess->getUser()->getId() != $userId) {
$response['allResults'][] = [ $response['allResults'][] = [
'userName' => $guess->getUser()->getDisplayName(), 'userName' => $guess->getUser()->getDisplayName(),
@ -199,7 +204,7 @@ class GameFlowController
$session = $this->request->session(); $session = $this->request->session();
$userId = $session->get('userId'); $userId = $session->get('userId');
$challengeToken_str = $this->request->query('challengeToken'); $challengeToken_str = $this->request->query('challengeToken');
$userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str); $userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str, [Challenge::class]);
if (!isset($userInChallenge)) { if (!isset($userInChallenge)) {
return new JsonContent(['error' => 'game_not_found']); return new JsonContent(['error' => 'game_not_found']);
@ -215,9 +220,6 @@ class GameFlowController
public function guess(): IContent public function guess(): IContent
{ {
// testing
$testPlace = $this->placeRepository->getByIdWithMap(5);
$mapId = (int) $this->request->query('mapId'); $mapId = (int) $this->request->query('mapId');
$session = $this->request->session(); $session = $this->request->session();
@ -318,7 +320,7 @@ class GameFlowController
$session = $this->request->session(); $session = $this->request->session();
$userId = $session->get('userId'); $userId = $session->get('userId');
$challengeToken_str = $this->request->query('challengeToken'); $challengeToken_str = $this->request->query('challengeToken');
$userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str); $userInChallenge = $this->userInChallengeRepository->getByUserIdAndToken($userId, $challengeToken_str, [Challenge::class]);
if (!isset($userInChallenge)) { if (!isset($userInChallenge)) {
return new JsonContent(['error' => 'game_not_found']); return new JsonContent(['error' => 'game_not_found']);
@ -326,7 +328,7 @@ class GameFlowController
$challenge = $userInChallenge->getChallenge(); $challenge = $userInChallenge->getChallenge();
$currentRound = $userInChallenge->getCurrentRound(); $currentRound = $userInChallenge->getCurrentRound();
$currentPlaceInChallenge = $this->placeInChallengeRepository->getByRoundInChallenge($currentRound, $challenge); $currentPlaceInChallenge = $this->placeInChallengeRepository->getByRoundInChallenge($currentRound, $challenge, [Place::class, Map::class]);
$currentPlace = $currentPlaceInChallenge->getPlace(); $currentPlace = $currentPlaceInChallenge->getPlace();
$map = $currentPlace->getMap(); $map = $currentPlace->getMap();

View File

@ -8,9 +8,9 @@ use MapGuesser\PersistentData\Model\Model;
class PersistentDataManager class PersistentDataManager
{ {
public function selectFromDb(Select $select, string $type, bool $withRelations = false) public function selectFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = [])
{ {
$select = $this->createSelect($select, $type, $withRelations); $select = $this->createSelect($select, $type, $useRelations, $withRelations);
$data = $select->execute()->fetch(IResultSet::FETCH_ASSOC); $data = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
@ -19,36 +19,38 @@ class PersistentDataManager
} }
$model = new $type(); $model = new $type();
$this->fillWithData($data, $model); $this->fillWithData($data, $model, $withRelations);
return $model; return $model;
} }
public function selectMultipleFromDb(Select $select, string $type, bool $withRelations = false): Generator public function selectMultipleFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Generator
{ {
$select = $this->createSelect($select, $type, $withRelations); $select = $this->createSelect($select, $type, $useRelations, $withRelations);
$result = $select->execute(); $result = $select->execute();
while ($data = $result->fetch(IResultSet::FETCH_ASSOC)) { while ($data = $result->fetch(IResultSet::FETCH_ASSOC)) {
$model = new $type(); $model = new $type();
$this->fillWithData($data, $model); $this->fillWithData($data, $model, $withRelations);
yield $model; yield $model;
} }
} }
public function selectFromDbById($id, string $type, bool $withRelations = false) public function selectFromDbById($id, string $type, bool $useRelations = false)
{ {
$select = new Select(\Container::$dbConnection); $select = new Select(\Container::$dbConnection);
$select->whereId($id); $select->whereId($id);
return $this->selectFromDb($select, $type, $withRelations); return $this->selectFromDb($select, $type, $useRelations);
} }
public function fillWithData(array &$data, Model $model, ?string $modelKey = null): void public function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
{ {
$relations = $model::getRelations(); $relations = $model::getRelations();
$relationData = []; if (count($withRelations)) {
$relations = array_intersect($relations, $withRelations);
}
while (key($data)) { while (key($data)) {
$key = key($data); $key = key($data);
@ -74,34 +76,19 @@ class PersistentDataManager
next($data); next($data);
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') { } else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
// $relation = current($relations);
$relationType = current($relations); $relationType = current($relations);
$relationModel = new $relationType(); $relationModel = new $relationType();
$this->fillWithData($data, $relationModel, $relation); $this->fillWithData($data, $relationModel, $withRelations, $relation);
$method = 'set' . str_replace('_', '', ucwords($relation, '_')); $method = 'set' . str_replace('_', '', ucwords($relation, '_'));
$model->$method($relationModel); $model->$method($relationModel);
next($relations); next($relations);
} else { } else {
break; return;
} }
} }
// foreach ($data as $key => $value) {
// if ($this->extractRelationData($key, $value, $relationData, $relations)) {
// continue;
// }
// $method = 'set' . str_replace('_', '', ucwords($key, '_'));
// if (method_exists($model, $method)) {
// $model->$method($value);
// }
// }
// $this->setRelations($model, $relationData);
$model->saveSnapshot(); $model->saveSnapshot();
} }
@ -166,15 +153,11 @@ class PersistentDataManager
$model->resetSnapshot(); $model->resetSnapshot();
} }
private function createSelect(Select $select, string $type, bool $withRelations = false): Select private function createSelect(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Select
{ {
$table = call_user_func([$type, 'getTable']); $table = call_user_func([$type, 'getTable']);
$fields = call_user_func([$type, 'getFields']); $fields = call_user_func([$type, 'getFields']);
// array_walk($fields, function (&$value, $key, $table) {
// $value = [$table, $value];
// }, $table);
$columns = []; $columns = [];
foreach ($fields as $field) { foreach ($fields as $field) {
@ -183,29 +166,24 @@ class PersistentDataManager
$select->from($table); $select->from($table);
//TODO: only with some relations? if ($useRelations) {
if ($withRelations) {
$relations = call_user_func([$type, 'getRelations']); $relations = call_user_func([$type, 'getRelations']);
if (count($withRelations)) {
$relations = array_intersect($relations, $withRelations);
}
// $columns = []; $columns = array_merge($columns, $this->getRelationColumns($relations, $withRelations));
// foreach ($fields as $field) { $this->leftJoinRelations($select, $table, $relations, $withRelations);
// $columns[] = [$table, $field];
// }
$columns = array_merge($columns, $this->getRelationColumns($relations));
$this->leftJoinRelations($select, $table, $relations);
$select->columns($columns); $select->columns($columns);
} else { } else {
// $select->columns($fields);
$select->columns($columns); $select->columns($columns);
} }
return $select; return $select;
} }
private function getRelationColumns(array $relations): array private function getRelationColumns(array $relations, array $withRelations): array
{ {
$columns = []; $columns = [];
@ -216,50 +194,26 @@ class PersistentDataManager
} }
$nextOrderRelations = call_user_func([$relationType, 'getRelations']); $nextOrderRelations = call_user_func([$relationType, 'getRelations']);
$columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations)); if (count($withRelations)) {
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
}
$columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations, $withRelations));
} }
return $columns; return $columns;
} }
private function leftJoinRelations(Select $select, string $table, array $relations): void private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
{ {
foreach ($relations as $relation => $relationType) { foreach ($relations as $relation => $relationType) {
$relationTable = call_user_func([$relationType, 'getTable']); $relationTable = call_user_func([$relationType, 'getTable']);
$select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']); $select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']);
$nextOrderRelations = call_user_func([$relationType, 'getRelations']); $nextOrderRelations = call_user_func([$relationType, 'getRelations']);
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations); if (count($withRelations)) {
} $nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
}
private function extractRelationData(string $key, $value, array &$relationData, array $relations): bool
{
$found = false;
foreach ($relations as $relation => $relationType) {
if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
$found = true;
$relationData[$relation][substr($key, strlen($relation . '__'))] = $value;
break;
}
}
return $found;
}
private function setRelations(Model $model, array &$relations): void
{
foreach ($model::getRelations() as $relation => $relationType) {
if (isset($relations[$relation])) {
$object = new $relationType();
$this->fillWithData($relations[$relation], $object);
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
$model->$method($object);
} }
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations, $withRelations);
} }
} }

View File

@ -7,6 +7,7 @@ use MapGuesser\PersistentData\Model\Guess;
use MapGuesser\PersistentData\Model\User; use MapGuesser\PersistentData\Model\User;
use MapGuesser\PersistentData\Model\UserInChallenge; use MapGuesser\PersistentData\Model\UserInChallenge;
use MapGuesser\PersistentData\Model\Place; use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\Model\PlaceInChallenge;
use MapGuesser\PersistentData\PersistentDataManager; use MapGuesser\PersistentData\PersistentDataManager;
class GuessRepository class GuessRepository
@ -50,21 +51,31 @@ class GuessRepository
yield from $this->pdm->selectMultipleFromDb($select, Guess::class); yield from $this->pdm->selectMultipleFromDb($select, Guess::class);
} }
public function getAllInChallenge(Challenge $challenge): Generator public function getAllInChallenge(Challenge $challenge, array $withRelations = []): Generator
{ {
if (count($withRelations)) {
$necessaryRelations = [PlaceInChallenge::class];
$withRelations = array_unique(array_merge($withRelations, $necessaryRelations));
}
$select = new Select(\Container::$dbConnection); $select = new Select(\Container::$dbConnection);
$select->where('challenge_id', '=', $challenge->getId()); $select->where('challenge_id', '=', $challenge->getId());
$select->orderBy('round'); $select->orderBy('round');
yield from $this->pdm->selectMultipleFromDb($select, Guess::class, true); yield from $this->pdm->selectMultipleFromDb($select, Guess::class, true, $withRelations);
} }
public function getAllInChallengeByRound(int $round, Challenge $challenge): Generator public function getAllInChallengeByRound(int $round, Challenge $challenge, array $withRelations = []): Generator
{ {
if (count($withRelations)) {
$necessaryRelations = [PlaceInChallenge::class];
$withRelations = array_unique(array_merge($withRelations, $necessaryRelations));
}
$select = new Select(\Container::$dbConnection); $select = new Select(\Container::$dbConnection);
$select->where('challenge_id', '=', $challenge->getId()); $select->where('challenge_id', '=', $challenge->getId());
$select->where('round', '=', $round); $select->where('round', '=', $round);
yield from $this->pdm->selectMultipleFromDb($select, Guess::class, true); yield from $this->pdm->selectMultipleFromDb($select, Guess::class, true, $withRelations);
} }
} }

View File

@ -3,6 +3,7 @@
use Generator; use Generator;
use MapGuesser\Database\Query\Select; use MapGuesser\Database\Query\Select;
use MapGuesser\PersistentData\Model\Challenge; use MapGuesser\PersistentData\Model\Challenge;
use MapGuesser\PersistentData\Model\Map;
use MapGuesser\PersistentData\Model\Place; use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\Model\PlaceInChallenge; use MapGuesser\PersistentData\Model\PlaceInChallenge;
use MapGuesser\PersistentData\PersistentDataManager; use MapGuesser\PersistentData\PersistentDataManager;
@ -41,13 +42,13 @@ class PlaceInChallengeRepository
return $this->pdm->selectFromDb($select, PlaceInChallenge::class); return $this->pdm->selectFromDb($select, PlaceInChallenge::class);
} }
public function getByRoundInChallenge(int $round, Challenge $challenge): ?PlaceInChallenge public function getByRoundInChallenge(int $round, Challenge $challenge, array $withRelations = []): ?PlaceInChallenge
{ {
$select = new Select(\Container::$dbConnection); $select = new Select(\Container::$dbConnection);
$select->where('challenge_id', '=', $challenge->getId()); $select->where('challenge_id', '=', $challenge->getId());
$select->orderBy('round'); $select->orderBy('round');
$select->limit(1, $round); $select->limit(1, $round);
return $this->pdm->selectFromDb($select, PlaceInChallenge::class, true); return $this->pdm->selectFromDb($select, PlaceInChallenge::class, true, $withRelations);
} }
} }

View File

@ -198,14 +198,5 @@ class PlaceRepository
yield from $this->pdm->selectMultipleFromDb($select, Place::class); yield from $this->pdm->selectMultipleFromDb($select, Place::class);
} }
public function getByIdWithMap(int $placeId): ?Place
{
$select = new Select(\Container::$dbConnection);
// $select->innerJoin('maps', ['maps', 'id'], '=', ['places', 'map_id']);
$select->where(['places', 'id'], '=', $placeId);
return $this->pdm->selectFromDb($select, Place::class, true);
}
} }

View File

@ -41,15 +41,20 @@ class UserInChallengeRepository
return $this->pdm->selectFromDb($select, UserInChallenge::class); return $this->pdm->selectFromDb($select, UserInChallenge::class);
} }
public function getByUserIdAndToken(int $userId, string $token_str): ?UserInChallenge public function getByUserIdAndToken(int $userId, string $token_str, array $withRelations = []): ?UserInChallenge
{ {
if (count($withRelations)) {
$necessaryRelations = [Challenge::class];
$withRelations = array_unique(array_merge($withRelations, $necessaryRelations));
}
$token = hexdec($token_str); $token = hexdec($token_str);
$select = new Select(\Container::$dbConnection); $select = new Select(\Container::$dbConnection);
$select->where('user_id', '=', $userId); $select->where('user_id', '=', $userId);
$select->where('token', '=', $token); $select->where('token', '=', $token);
return $this->pdm->selectFromDb($select, UserInChallenge::class, true); return $this->pdm->selectFromDb($select, UserInChallenge::class, true, $withRelations);
} }
public function isUserParticipatingInChallenge(int $userId, Challenge $challenge): bool public function isUserParticipatingInChallenge(int $userId, Challenge $challenge): bool

View File

@ -38,7 +38,7 @@ class UserPlayedPlaceRepository
$select = new Select(\Container::$dbConnection); $select = new Select(\Container::$dbConnection);
$select->where('user_id', '=', $user->getId()); $select->where('user_id', '=', $user->getId());
yield from $this->pdm->selectMultipleFromDb($select, UserPlayedPlace::class, true); yield from $this->pdm->selectMultipleFromDb($select, UserPlayedPlace::class);
} }
public function getByUserIdAndPlaceId(int $userId, int $placeId) : ?UserPlayedPlace public function getByUserIdAndPlaceId(int $userId, int $placeId) : ?UserPlayedPlace