From 9391428c5e1436f61bb2b68f7ebb3f29f26a257c Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sun, 5 Nov 2023 13:30:36 +0700 Subject: [PATCH] Move methods from `Command` to `AbstractPdoCommand` class --- src/Driver/Pdo/AbstractPdoCommand.php | 37 ++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Driver/Pdo/AbstractPdoCommand.php b/src/Driver/Pdo/AbstractPdoCommand.php index bce283a53..c8348f832 100644 --- a/src/Driver/Pdo/AbstractPdoCommand.php +++ b/src/Driver/Pdo/AbstractPdoCommand.php @@ -14,12 +14,14 @@ use Yiisoft\Db\Command\AbstractCommand; use Yiisoft\Db\Command\Param; use Yiisoft\Db\Command\ParamInterface; +use Yiisoft\Db\Exception\ConvertException; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidParamException; use Yiisoft\Db\Profiler\Context\CommandContext; use Yiisoft\Db\Profiler\ProfilerAwareInterface; use Yiisoft\Db\Profiler\ProfilerAwareTrait; use Yiisoft\Db\Query\Data\DataReader; +use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; /** * Represents a database command that can be executed using a PDO (PHP Data Object) database connection. @@ -161,6 +163,11 @@ protected function bindPendingParams(): void } } + protected function getQueryBuilder(): QueryBuilderInterface + { + return $this->db->getQueryBuilder(); + } + protected function getQueryMode(int $queryMode): string { return match ($queryMode) { @@ -184,7 +191,35 @@ protected function getQueryMode(int $queryMode): string * @throws Exception * @throws Throwable */ - abstract protected function internalExecute(string|null $rawSql): void; + protected function internalExecute(string|null $rawSql): void + { + $attempt = 0; + + while (true) { + try { + if ( + ++$attempt === 1 + && $this->isolationLevel !== null + && $this->db->getTransaction() === null + ) { + $this->db->transaction( + fn () => $this->internalExecute($rawSql), + $this->isolationLevel + ); + } else { + $this->pdoStatement?->execute(); + } + break; + } catch (PDOException $e) { + $rawSql = $rawSql ?: $this->getRawSql(); + $e = (new ConvertException($e, $rawSql))->run(); + + if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) { + throw $e; + } + } + } + } /** * @throws InvalidParamException