From 7c5757bca515b63c9ea1f0901ca932303cd29ea1 Mon Sep 17 00:00:00 2001 From: Yi-Jyun Pan Date: Thu, 28 Nov 2024 01:26:20 +0800 Subject: [PATCH] refactor!: Remove old dbrunner completely --- config/packages/cache.yaml | 5 - frankenphp/docker-entrypoint.sh | 3 - ...ultDto.php => FallableSqlRunnerResult.php} | 5 +- src/Entity/ChallengeDto/QueryResultDto.php | 36 ----- src/Service/DbRunner.php | 128 ------------------ src/Service/DbRunnerComparer.php | 54 -------- src/Service/DbRunnerService.php | 37 ----- .../Processes/DbRunnerProcessService.php | 64 --------- src/Service/Processes/ProcessService.php | 52 ------- src/Service/Processes/dbrunner_process.php | 9 -- src/Service/SqlRunnerService.php | 2 +- src/Service/Types/DbRunnerProcessPayload.php | 17 --- src/Service/Types/ProcessError.php | 31 ----- src/Service/Types/SchemaDatabase.php | 125 ----------------- .../Components/Challenge/ColumnsOfAnswer.php | 4 +- src/Twig/Components/Challenge/Executor.php | 6 +- .../Challenge/Instruction/Modal.php | 21 ++- .../Challenge/Tabs/AnswerQueryResult.php | 12 +- .../Challenge/Tabs/DiffPresenter.php | 6 +- .../Challenge/Tabs/UserQueryResult.php | 26 ++-- 20 files changed, 46 insertions(+), 597 deletions(-) rename src/Entity/ChallengeDto/{FallableQueryResultDto.php => FallableSqlRunnerResult.php} (90%) delete mode 100644 src/Entity/ChallengeDto/QueryResultDto.php delete mode 100644 src/Service/DbRunner.php delete mode 100644 src/Service/DbRunnerComparer.php delete mode 100644 src/Service/DbRunnerService.php delete mode 100644 src/Service/Processes/DbRunnerProcessService.php delete mode 100644 src/Service/Processes/ProcessService.php delete mode 100644 src/Service/Processes/dbrunner_process.php delete mode 100644 src/Service/Types/DbRunnerProcessPayload.php delete mode 100644 src/Service/Types/ProcessError.php delete mode 100644 src/Service/Types/SchemaDatabase.php diff --git a/config/packages/cache.yaml b/config/packages/cache.yaml index 6ce81e7..5236446 100644 --- a/config/packages/cache.yaml +++ b/config/packages/cache.yaml @@ -4,11 +4,6 @@ framework: app: cache.adapter.redis_tag_aware default_redis_provider: "%app.redis_uri%" - - pools: - cache.dbrunner: - adapter: cache.adapter.redis_tag_aware - default_lifetime: "12 hour" # Unique name of your app: used to compute stable namespaces for cache keys. #prefix_seed: your_vendor_name/app_name diff --git a/frankenphp/docker-entrypoint.sh b/frankenphp/docker-entrypoint.sh index 7d56f4e..5ec5f2d 100644 --- a/frankenphp/docker-entrypoint.sh +++ b/frankenphp/docker-entrypoint.sh @@ -33,9 +33,6 @@ if [ "$1" = 'frankenphp' ] || [ "$1" = 'php' ] || [ "$1" = 'bin/console' ]; then fi fi - echo "Cleaning up dbrunner cache..." - php bin/console cache:pool:clear cache.dbrunner || true - echo "Updating Meilisearch indexes..." php bin/console meili:clear || true php bin/console meili:import --update-settings || true diff --git a/src/Entity/ChallengeDto/FallableQueryResultDto.php b/src/Entity/ChallengeDto/FallableSqlRunnerResult.php similarity index 90% rename from src/Entity/ChallengeDto/FallableQueryResultDto.php rename to src/Entity/ChallengeDto/FallableSqlRunnerResult.php index 3225f99..1b4dc4a 100644 --- a/src/Entity/ChallengeDto/FallableQueryResultDto.php +++ b/src/Entity/ChallengeDto/FallableSqlRunnerResult.php @@ -5,14 +5,15 @@ namespace App\Entity\ChallengeDto; use App\Entity\SqlRunnerDto\SqlRunnerResult; +use App\Service\SqlRunnerService; use Symfony\Component\Translation\TranslatableMessage; /** - * A DTO for the result of a query that may contain the error from DbRunner. + * A DTO for the result of a query that may contain the error from {@link SqlRunnerService}. * * If there is no error, the errorCode will be 0. */ -class FallableQueryResultDto +class FallableSqlRunnerResult { public ?SqlRunnerResult $result = null; public ?TranslatableMessage $errorMessage = null; diff --git a/src/Entity/ChallengeDto/QueryResultDto.php b/src/Entity/ChallengeDto/QueryResultDto.php deleted file mode 100644 index 69e3787..0000000 --- a/src/Entity/ChallengeDto/QueryResultDto.php +++ /dev/null @@ -1,36 +0,0 @@ -> the result of the user's query - */ - private array $result; - - /** - * @return array> the result of the user's query - */ - public function getResult(): array - { - return $this->result; - } - - /** - * @param array> $result the result of the user's query - */ - public function setResult(array $result): self - { - $this->result = $result; - - return $this; - } -} diff --git a/src/Service/DbRunner.php b/src/Service/DbRunner.php deleted file mode 100644 index aa9b9ea..0000000 --- a/src/Service/DbRunner.php +++ /dev/null @@ -1,128 +0,0 @@ -formatter = new SqlFormatter(); - } - - /** - * Hash the given SQL statement to a hex string. - * - * Useful for caching. Normalization is applied. - * - * @param string $sql the SQL to hash - * - * @return string the hashed SHA-256 hex string - */ - public function hashStatement(string $sql): string - { - return hash('sha3-256', $this->formatter->compress($sql)); - } - - /** - * Run the query with SQLite3. - * - * For example: - * - * - * $dbRunner = new DbRunner(); - * $schema = "CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT)"; - * $query = "SELECT * FROM students"; - * $result = $dbRunner->runQuery($schema, $query); - * // $result is an array with the result of the query. - * // Example: [["id" => 1, "name" => "John"]] - * - * - * @param string $schema the schema to create the database - * @param string $query the query to run - * - * @return QueryResultDto the result of the query - * - * @throws SchemaExecuteException if the schema could not be executed - * @throws QueryExecuteException if the query could not be executed - * @throws ResourceException if the resource is exhausted (exit code = 255) - * @throws \Throwable if the unexpected error is received - */ - public function runQuery(string $schema, string $query): QueryResultDto - { - // Use a process to prevent the SQLite3 extension from crashing the PHP process. - // For example, CTE queries and randomblob can crash the PHP process. - // See the test cases for more details. - // - // We don't yield over the result; instead, we store in the memory. - // Our PHP process has a hard limit of the memory usage, - // and we can crash it as early as possible when receiving a big result. - - $process = new Process(['php', __DIR__.'/Processes/dbrunner_process.php']); - $process->setTimeout($this->timeout); - $process->setInput(serialize(new DbRunnerProcessPayload($schema, $query))); - - try { - $process->mustRun(); - - $output = $process->getOutput(); - $outputDeserialized = unserialize($output, [ - 'allowed_classes' => [ - QueryResultDto::class, - ], - ]); - - if (!$outputDeserialized instanceof QueryResultDto) { - throw new \RuntimeException("unexpected output: $output"); - } - - return $outputDeserialized; - } catch (ProcessFailedException) { - $exitCode = $process->getExitCode(); - - if (255 === $exitCode) { - throw new ResourceException(); - } - - if (1 === $exitCode) { - $output = $process->getErrorOutput(); - $outputDeserialized = unserialize($output, [ - 'allowed_classes' => true, - ]); - - if (!($outputDeserialized instanceof ProcessError)) { - $o = json_encode($output); - throw new \RuntimeException("Unexpected data received (exit code 1): $o"); - } - - $outputDeserialized->rethrow(); - } - - throw new \RuntimeException("Unexpected exit code: $exitCode"); - } catch (ProcessTimedOutException) { - throw new TimedOutException($this->timeout); - } - } -} diff --git a/src/Service/DbRunnerComparer.php b/src/Service/DbRunnerComparer.php deleted file mode 100644 index 0f95e02..0000000 --- a/src/Service/DbRunnerComparer.php +++ /dev/null @@ -1,54 +0,0 @@ -getResult())) { - return new CompareResult\EmptyAnswer(); - } - if (0 === \count($userResult->getResult())) { - return new CompareResult\EmptyResult(); - } - - $answerColumns = $answerResult->getResult()[0]; - $userColumns = $userResult->getResult()[0]; - if ($answerColumns !== $userColumns) { - return new CompareResult\ColumnDifferent(); - } - - $answerRows = \array_slice($answerResult->getResult(), 1); - $userRows = \array_slice($userResult->getResult(), 1); - if (\count($answerRows) !== \count($userRows)) { - return new CompareResult\RowUnmatched( - expected: \count($answerRows), - actual: \count($userRows), - ); - } - - for ($i = 0; $i < \count($answerRows); ++$i) { - if ($answerRows[$i] !== $userRows[$i]) { - return new CompareResult\RowDifferent( - row: $i + 1, - ); - } - } - - return new CompareResult\Same(); - } -} diff --git a/src/Service/DbRunnerService.php b/src/Service/DbRunnerService.php deleted file mode 100644 index e611b7e..0000000 --- a/src/Service/DbRunnerService.php +++ /dev/null @@ -1,37 +0,0 @@ -dbRunner = new DbRunner(); - } - - /** - * Run a query on the SQLite3 database, cached. - * - * @throws InvalidArgumentException - * @throws SchemaExecuteException - * @throws QueryExecuteException - */ - public function runQuery(string $schema, string $query): QueryResultDto - { - $schemaHash = $this->dbRunner->hashStatement($schema); - $queryHash = $this->dbRunner->hashStatement($query); - $hash = "dbrunner.$schemaHash.$queryHash"; - - return $this->cacheDbrunner->get($hash, fn () => $this->dbRunner->runQuery($schema, $query)); - } -} diff --git a/src/Service/Processes/DbRunnerProcessService.php b/src/Service/Processes/DbRunnerProcessService.php deleted file mode 100644 index 529ec09..0000000 --- a/src/Service/Processes/DbRunnerProcessService.php +++ /dev/null @@ -1,64 +0,0 @@ -schema); - $sqliteResult = $db->query($input->query); - $queryResult = $this->transformResult($sqliteResult); - $sqliteResult->finalize(); - - return $queryResult; - } - - private function transformResult(\SQLite3Result $result): QueryResultDto - { - /** - * @var array> $columnsRow - */ - $columnsRow = []; - - for ($i = 0; $i < $result->numColumns(); ++$i) { - $columnsRow[] = $result->columnName($i); - } - - /** - * @var array> $rows - */ - $rows = []; - - while ($rawRow = $result->fetchArray(\SQLITE3_ASSOC)) { - $row = []; - foreach ($rawRow as $value) { - $row[] = match (true) { - null === $value => 'NULL', - \is_string($value) => $value, - \is_bool($value) => $value ? 'TRUE' : 'FALSE', - is_numeric($value) => (string) $value, - default => '', - }; - } - $rows[] = $row; - } - - /** - * @var array> $merged - */ - $merged = array_merge([$columnsRow], $rows); - - return (new QueryResultDto())->setResult($merged); - } -} diff --git a/src/Service/Processes/ProcessService.php b/src/Service/Processes/ProcessService.php deleted file mode 100644 index 8f9fc82..0000000 --- a/src/Service/Processes/ProcessService.php +++ /dev/null @@ -1,52 +0,0 @@ - [ - DbRunnerProcessPayload::class, - ], - ]); - if (!\is_object($input)) { - throw new \InvalidArgumentException('input must be an object'); - } - - $result = $this->main($input); - fwrite(\STDOUT, serialize($result)); - exit(0); - } catch (\Throwable $throwable) { - fwrite(\STDERR, serialize(new ProcessError($throwable))); - exit(1); - } - } - - /** - * The main function of the process. - * - * @param object $input the input of the process - * - * @return object the result of the process - */ - abstract public function main(object $input): object; -} diff --git a/src/Service/Processes/dbrunner_process.php b/src/Service/Processes/dbrunner_process.php deleted file mode 100644 index 90d16e3..0000000 --- a/src/Service/Processes/dbrunner_process.php +++ /dev/null @@ -1,9 +0,0 @@ -run(); diff --git a/src/Service/SqlRunnerService.php b/src/Service/SqlRunnerService.php index d9a39e8..49936b5 100644 --- a/src/Service/SqlRunnerService.php +++ b/src/Service/SqlRunnerService.php @@ -51,7 +51,7 @@ public function runQuery(SqlRunnerRequest $request): SqlRunnerResult $response = $this->httpClient->request('POST', $endpoint, [ 'json' => (array) $request, 'headers' => [ - 'User-Agent' => 'dbrunner/v1', + 'User-Agent' => 'dbplay/v1', ], ]); $content = $response->getContent(false); diff --git a/src/Service/Types/DbRunnerProcessPayload.php b/src/Service/Types/DbRunnerProcessPayload.php deleted file mode 100644 index 7042736..0000000 --- a/src/Service/Types/DbRunnerProcessPayload.php +++ /dev/null @@ -1,17 +0,0 @@ -throwable; - } - - /** - * Rethrows the error. - * - * @throws \Throwable - */ - public function rethrow(): void - { - throw $this->throwable; - } -} diff --git a/src/Service/Types/SchemaDatabase.php b/src/Service/Types/SchemaDatabase.php deleted file mode 100644 index eda2658..0000000 --- a/src/Service/Types/SchemaDatabase.php +++ /dev/null @@ -1,125 +0,0 @@ -db->close(); - } - - /** - * Execute the query and return the result. - * - * @param string $query the query to execute - * - * @return \SQLite3Result the result of the query - * - * @throws QueryExecuteException if the query could not be executed - */ - public function query(string $query): \SQLite3Result - { - try { - $result = $this->db->query($query); - } catch (\Throwable) { - throw new QueryExecuteException($this->db->lastErrorMsg()); - } - - if (\is_bool($result)) { - throw new QueryExecuteException("Invalid query given: '$query'"); - } - - return $result; - } - - private static function setUp(\SQLite3 $db): \SQLite3 - { - $db->busyTimeout(3000 /* milliseconds */); - $db->enableExceptions(true); - - $dateop = fn (string $format) => fn (string $date) => (int) date( - $format, - ($datestr = strtotime($date)) !== false - ? $datestr - : throw new \InvalidArgumentException("Failed to convert $date as $format."), - ); - - // MySQL-compatible functions - $db->createFunction('YEAR', $dateop('Y'), 1, \SQLITE3_DETERMINISTIC); - $db->createFunction('MONTH', $dateop('n'), 1, \SQLITE3_DETERMINISTIC); - $db->createFunction('DAY', $dateop('j'), 1, \SQLITE3_DETERMINISTIC); - $db->createFunction('LEFT', fn (string $str, int $len) => substr($str, 0, $len), 2, \SQLITE3_DETERMINISTIC); - $db->createFunction( - 'IF', - fn (bool $condition, mixed $true, mixed $false) => $condition ? $true : $false, - 3, - \SQLITE3_DETERMINISTIC - ); - - return $db; - } - - /** - * Initialize the database and return the filename to the schema sqlite3. - * - * It does nothing if the file already exists. - * - * @param string $filename the filename to the schema sqlite3 - * @param string $schema the schema to initialize - * - * @throws SchemaExecuteException if the schema could not be executed - */ - private static function initialize(string $filename, string $schema): void - { - if (file_exists($filename)) { - return; - } - - $db = self::setUp(new \SQLite3($filename)); - - try { - $db->exec('BEGIN EXCLUSIVE'); - $db->exec($schema); - $db->exec('COMMIT'); - $db->close(); - } catch (\Throwable) { - $lastErrorMessage = $db->lastErrorMsg(); - - // remove the file if the schema could not be executed - $db->close(); - unlink($filename); - - throw new SchemaExecuteException($lastErrorMessage); - } - } - - private static function getSchemaSqlFilename(string $schema): string - { - $tmpdir = sys_get_temp_dir(); - $schemaHash = hash('sha3-256', $schema); - - return "$tmpdir/dbrunner_$schemaHash.sql"; - } -} diff --git a/src/Twig/Components/Challenge/ColumnsOfAnswer.php b/src/Twig/Components/Challenge/ColumnsOfAnswer.php index 820f2ef..1cd24e5 100644 --- a/src/Twig/Components/Challenge/ColumnsOfAnswer.php +++ b/src/Twig/Components/Challenge/ColumnsOfAnswer.php @@ -16,7 +16,7 @@ final class ColumnsOfAnswer use DefaultActionTrait; public function __construct( - private readonly QuestionSqlRunnerService $questionDbRunnerService, + private readonly QuestionSqlRunnerService $questionSqlRunnerService, ) { } @@ -31,7 +31,7 @@ public function __construct( public function getColumnsOfAnswer(): array { try { - $answer = $this->questionDbRunnerService->getAnswerResult($this->question); + $answer = $this->questionSqlRunnerService->getAnswerResult($this->question); return $answer->getColumns(); } catch (\Throwable $e) { diff --git a/src/Twig/Components/Challenge/Executor.php b/src/Twig/Components/Challenge/Executor.php index 41d1807..a49b933 100644 --- a/src/Twig/Components/Challenge/Executor.php +++ b/src/Twig/Components/Challenge/Executor.php @@ -26,7 +26,7 @@ final class Executor use DefaultActionTrait; public function __construct( - private readonly QuestionSqlRunnerService $questionDbRunnerService, + private readonly QuestionSqlRunnerService $questionSqlRunnerService, private readonly SolutionEventRepository $solutionEventRepository, private readonly EntityManagerInterface $entityManager, ) { @@ -60,8 +60,8 @@ public function createNewQuery( ->setQuery($query); try { - $answer = $this->questionDbRunnerService->getAnswerResult($this->question); - $result = $this->questionDbRunnerService->getQueryResult($this->question, $query); + $answer = $this->questionSqlRunnerService->getAnswerResult($this->question); + $result = $this->questionSqlRunnerService->getQueryResult($this->question, $query); $compareResult = SqlRunnerComparer::compare($answer, $result); $solutionEvent = $solutionEvent->setStatus( diff --git a/src/Twig/Components/Challenge/Instruction/Modal.php b/src/Twig/Components/Challenge/Instruction/Modal.php index b749303..462215c 100644 --- a/src/Twig/Components/Challenge/Instruction/Modal.php +++ b/src/Twig/Components/Challenge/Instruction/Modal.php @@ -7,12 +7,13 @@ use App\Entity\HintOpenEvent; use App\Entity\Question; use App\Entity\SolutionEventStatus; +use App\Entity\SqlRunnerDto\SqlRunnerRequest; use App\Entity\User; use App\Repository\SolutionEventRepository; -use App\Service\DbRunnerComparer; -use App\Service\DbRunnerService; use App\Service\PointCalculationService; use App\Service\PromptService; +use App\Service\SqlRunnerComparer; +use App\Service\SqlRunnerService; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -56,7 +57,7 @@ public function getCost(): int #[LiveAction] public function instruct( SolutionEventRepository $solutionEventRepository, - DbRunnerService $dbRunnerService, + SqlRunnerService $sqlRunnerService, PromptService $promptService, TranslatorInterface $translator, EntityManagerInterface $entityManager, @@ -85,7 +86,11 @@ public function instruct( try { $answer = $query->getQuestion()->getAnswer(); - $answerResult = $dbRunnerService->runQuery($schema->getSchema(), $answer); + $answerResult = $sqlRunnerService->runQuery( + (new SqlRunnerRequest()) + ->setSchema($schema->getSchema()) + ->setQuery($answer), + ); } catch (\Throwable $e) { $this->flushHint('informative', t('instruction.hint.error', [ '%error%' => $e->getMessage(), @@ -101,7 +106,11 @@ public function instruct( try { try { - $userResult = $dbRunnerService->runQuery($schema->getSchema(), $query->getQuery()); + $userResult = $sqlRunnerService->runQuery( + (new SqlRunnerRequest()) + ->setSchema($schema->getSchema()) + ->setQuery($query->getQuery()), + ); } catch (\Throwable $e) { $hint = $promptService->hint($query->getQuery(), $e->getMessage(), $answer); $hintOpenEvent->setResponse($hint); @@ -111,7 +120,7 @@ public function instruct( return; } - $compareResult = DbRunnerComparer::compare($answerResult, $userResult); + $compareResult = SqlRunnerComparer::compare($answerResult, $userResult); if ($compareResult->correct()) { $this->flushHint('informative', t('instruction.hint.solved')); diff --git a/src/Twig/Components/Challenge/Tabs/AnswerQueryResult.php b/src/Twig/Components/Challenge/Tabs/AnswerQueryResult.php index 1d9a1b3..7b479ae 100644 --- a/src/Twig/Components/Challenge/Tabs/AnswerQueryResult.php +++ b/src/Twig/Components/Challenge/Tabs/AnswerQueryResult.php @@ -4,7 +4,7 @@ namespace App\Twig\Components\Challenge\Tabs; -use App\Entity\ChallengeDto\FallableQueryResultDto; +use App\Entity\ChallengeDto\FallableSqlRunnerResult; use App\Entity\Question; use App\Service\QuestionSqlRunnerService; use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; @@ -15,7 +15,7 @@ final class AnswerQueryResult { public function __construct( - private readonly QuestionSqlRunnerService $questionDbRunnerService, + private readonly QuestionSqlRunnerService $questionSqlRunnerService, ) { } @@ -24,18 +24,18 @@ public function __construct( */ public Question $question; - public function getAnswer(): FallableQueryResultDto + public function getAnswer(): FallableSqlRunnerResult { try { - $resultDto = $this->questionDbRunnerService->getAnswerResult($this->question); + $resultDto = $this->questionSqlRunnerService->getAnswerResult($this->question); - return (new FallableQueryResultDto())->setResult($resultDto); + return (new FallableSqlRunnerResult())->setResult($resultDto); } catch (\Throwable $e) { $errorMessage = t('challenge.errors.answer-query-failure', [ '%error%' => $e->getMessage(), ]); - return (new FallableQueryResultDto())->setErrorMessage($errorMessage); + return (new FallableSqlRunnerResult())->setErrorMessage($errorMessage); } } } diff --git a/src/Twig/Components/Challenge/Tabs/DiffPresenter.php b/src/Twig/Components/Challenge/Tabs/DiffPresenter.php index 802a313..4605519 100644 --- a/src/Twig/Components/Challenge/Tabs/DiffPresenter.php +++ b/src/Twig/Components/Challenge/Tabs/DiffPresenter.php @@ -26,7 +26,7 @@ final class DiffPresenter use DefaultActionTrait; public function __construct( - private readonly QuestionSqlRunnerService $questionDbRunnerService, + private readonly QuestionSqlRunnerService $questionSqlRunnerService, private readonly SolutionEventRepository $solutionEventRepository, private readonly TranslatorInterface $translator, private readonly SerializerInterface $serializer, @@ -52,7 +52,7 @@ public function postMount(): void public function getAnswerResult(): ?string { try { - $resultDto = $this->questionDbRunnerService->getAnswerResult($this->question); + $resultDto = $this->questionSqlRunnerService->getAnswerResult($this->question); $columnsAndRows = [$resultDto->getColumns(), ...$resultDto->getRows()]; @@ -76,7 +76,7 @@ public function getUserResult(): ?string } try { - $resultDto = $this->questionDbRunnerService->getQueryResult($this->question, $this->query); + $resultDto = $this->questionSqlRunnerService->getQueryResult($this->question, $this->query); $columnsAndRows = [$resultDto->getColumns(), ...$resultDto->getRows()]; diff --git a/src/Twig/Components/Challenge/Tabs/UserQueryResult.php b/src/Twig/Components/Challenge/Tabs/UserQueryResult.php index 2101cb4..3896ad1 100644 --- a/src/Twig/Components/Challenge/Tabs/UserQueryResult.php +++ b/src/Twig/Components/Challenge/Tabs/UserQueryResult.php @@ -4,7 +4,7 @@ namespace App\Twig\Components\Challenge\Tabs; -use App\Entity\ChallengeDto\FallableQueryResultDto; +use App\Entity\ChallengeDto\FallableSqlRunnerResult; use App\Entity\Question; use App\Entity\User; use App\Exception\SqlRunner\QueryExecuteException; @@ -28,7 +28,7 @@ final class UserQueryResult use DefaultActionTrait; public function __construct( - private readonly QuestionSqlRunnerService $questionDbRunnerService, + private readonly QuestionSqlRunnerService $questionSqlRunnerService, private readonly SolutionEventRepository $solutionEventRepository, private readonly LoggerInterface $logger, ) { @@ -55,14 +55,14 @@ public function postMount(): void $this->query = $this->solutionEventRepository->getLatestQuery($this->question, $this->user)?->getQuery(); } - public function getResult(): ?FallableQueryResultDto + public function getResult(): ?FallableSqlRunnerResult { if (null === $this->query) { return null; } try { - $answerResultDto = $this->questionDbRunnerService->getAnswerResult($this->question); + $answerResultDto = $this->questionSqlRunnerService->getAnswerResult($this->question); } catch (SchemaExecuteException $e) { $this->logger->error('Schema Error', [ 'exception' => $e, @@ -72,7 +72,7 @@ public function getResult(): ?FallableQueryResultDto '%error%' => $e->getMessage(), ]); - return (new FallableQueryResultDto())->setErrorMessage($errorMessage); + return (new FallableSqlRunnerResult())->setErrorMessage($errorMessage); } catch (QueryExecuteException $e) { $this->logger->error('Failed to get the answer result', [ 'exception' => $e, @@ -82,7 +82,7 @@ public function getResult(): ?FallableQueryResultDto '%error%' => $e->getMessage(), ]); - return (new FallableQueryResultDto())->setErrorMessage($errorMessage); + return (new FallableSqlRunnerResult())->setErrorMessage($errorMessage); } catch (\Throwable $e) { $this->logger->error('SQL Runner failed when running answer', [ 'exception' => $e, @@ -92,11 +92,11 @@ public function getResult(): ?FallableQueryResultDto '%error%' => $e->getMessage(), ]); - return (new FallableQueryResultDto())->setErrorMessage($errorMessage); + return (new FallableSqlRunnerResult())->setErrorMessage($errorMessage); } try { - $resultDto = $this->questionDbRunnerService->getQueryResult($this->question, $this->query); + $resultDto = $this->questionSqlRunnerService->getQueryResult($this->question, $this->query); } catch (SchemaExecuteException $e) { $this->logger->error('Schema Error', [ 'exception' => $e, @@ -106,13 +106,13 @@ public function getResult(): ?FallableQueryResultDto '%error%' => $e->getMessage(), ]); - return (new FallableQueryResultDto())->setErrorMessage($errorMessage); + return (new FallableSqlRunnerResult())->setErrorMessage($errorMessage); } catch (QueryExecuteException $e) { $errorMessage = t('challenge.errors.user-query-error', [ '%error%' => $e->getMessage(), ]); - return (new FallableQueryResultDto())->setErrorMessage($errorMessage); + return (new FallableSqlRunnerResult())->setErrorMessage($errorMessage); } catch (\Throwable $e) { $this->logger->error('SQL Runner failed when running user queries', [ 'exception' => $e, @@ -122,20 +122,20 @@ public function getResult(): ?FallableQueryResultDto '%error%' => $e->getMessage(), ]); - return (new FallableQueryResultDto())->setErrorMessage($errorMessage); + return (new FallableSqlRunnerResult())->setErrorMessage($errorMessage); } // compare the result $compareResult = SqlRunnerComparer::compare($answerResultDto, $resultDto); if ($compareResult->correct()) { - return (new FallableQueryResultDto())->setResult($resultDto); + return (new FallableSqlRunnerResult())->setResult($resultDto); } $errorMessage = t('challenge.errors.user-query-failure', [ '%error%' => $compareResult->reason(), ]); - return (new FallableQueryResultDto())->setResult($resultDto)->setErrorMessage($errorMessage); + return (new FallableSqlRunnerResult())->setResult($resultDto)->setErrorMessage($errorMessage); } #[LiveListener('app:challenge-executor:query-created')]