Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move methods from Command to AbstractPdoCommand class #770

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Bug #756: Fix `Quoter::quoteTableName()` for sub-query with alias (@Tigrov)
- Bug #761: Quote aliases of CTE in `WITH` queries (@Tigrov)
- Chg #765: Deprecate `SchemaInterface::TYPE_JSONB` (@Tigrov)
- Enh #770: Move methods from concrete `Command` class to `AbstractPdoCommand` class (@Tigrov)

## 1.1.1 August 16, 2023

Expand Down
37 changes: 36 additions & 1 deletion src/Driver/Pdo/AbstractPdoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
Loading