Skip to content
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 @@ -105,6 +105,7 @@
- New #988: Add `CaseExpression` and `CaseExpressionBuilder` to build `CASE-WHEN-THEN-ELSE` SQL expressions (@Tigrov)
- Enh #991: Improve types in `ConnectionInterface::transaction()` (@kikara)
- Chg #998: Add `yiisoft/db-implementation` virtual package as dependency (@vjik)
- Chg #999: Remove `requireTransaction()` method and `$isolationLevel` property from `AbstractCommand` (@vjik)

## 1.3.0 March 21, 2024

Expand Down
19 changes: 0 additions & 19 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ public function __construct(
*/
protected const QUERY_MODE_SCALAR = 32;

/**
* @var string|null Transaction isolation level.
*/
protected string|null $isolationLevel = null;

/**
* @var ParamInterface[] Parameters to use.
*/
Expand Down Expand Up @@ -663,19 +658,6 @@ protected function requireTableSchemaRefresh(string $name): static
return $this;
}

/**
* Marks the command to execute in transaction.
*
* @param string|null $isolationLevel The isolation level to use for this transaction.
*
* {@see \Yiisoft\Db\Transaction\TransactionInterface::begin()} for details.
*/
protected function requireTransaction(?string $isolationLevel = null): static
{
$this->isolationLevel = $isolationLevel;
return $this;
}

/**
* Resets the command object, so it can be reused to build another SQL statement.
*/
Expand All @@ -684,7 +666,6 @@ protected function reset(): void
$this->sql = '';
$this->params = [];
$this->refreshTableName = null;
$this->isolationLevel = null;
$this->retryHandler = null;
}

Expand Down
35 changes: 11 additions & 24 deletions src/Driver/Pdo/AbstractPdoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,31 +206,18 @@
*/
protected function internalExecute(): void
{
$attempt = 0;

while (true) {
for ($attempt = 0; ; ++$attempt) {
try {
if (
++$attempt === 1
&& $this->isolationLevel !== null
&& $this->db->getTransaction() === null
) {
$this->db->transaction(
fn () => $this->internalExecute(),
$this->isolationLevel
);
} else {
set_error_handler(
static fn(int $errorNumber, string $errorString): bool =>
str_starts_with($errorString, 'Packets out of order. Expected '),
E_WARNING,
);

try {
$this->pdoStatement?->execute();
} finally {
restore_error_handler();
}
set_error_handler(
static fn(int $errorNumber, string $errorString): bool =>
str_starts_with($errorString, 'Packets out of order. Expected '),

Check warning on line 213 in src/Driver/Pdo/AbstractPdoCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/Pdo/AbstractPdoCommand.php#L213

Added line #L213 was not covered by tests
E_WARNING,
);

try {
$this->pdoStatement?->execute();
} finally {
restore_error_handler();
}
break;
} catch (PDOException $e) {
Expand Down
111 changes: 0 additions & 111 deletions tests/Common/CommonCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Yiisoft\Db\Tests\Common;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use ReflectionException;
use Throwable;
use Yiisoft\Db\Constant\DataType;
use Yiisoft\Db\Command\Param;
Expand All @@ -31,7 +30,6 @@
use Yiisoft\Db\Tests\Provider\CommandProvider;
use Yiisoft\Db\Tests\Support\Assert;
use Yiisoft\Db\Tests\Support\DbHelper;
use Yiisoft\Db\Transaction\TransactionInterface;

use function array_filter;
use function is_string;
Expand Down Expand Up @@ -1128,68 +1126,6 @@ public function testExecuteWithoutSql(): void
$db->close();
}

/**
* @throws Exception
* @throws InvalidConfigException
* @throws ReflectionException
* @throws Throwable
*/
public function testExecuteWithTransaction(): void
{
$db = $this->getConnection(true);

$this->assertNull($db->getTransaction());

$command = $db->createCommand(
<<<SQL
INSERT INTO {{profile}} ([[description]]) VALUES('command transaction 1')
SQL,
);

Assert::invokeMethod($command, 'requireTransaction');

$command->execute();

$this->assertNull($db->getTransaction());

$this->assertEquals(
1,
$db->createCommand(
<<<SQL
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'command transaction 1'
SQL,
)->queryScalar(),
);

$command = $db->createCommand(
<<<SQL
INSERT INTO {{profile}} ([[description]]) VALUES('command transaction 2')
SQL,
);

Assert::invokeMethod($command, 'requireTransaction', [TransactionInterface::READ_UNCOMMITTED]);

$command->execute();

$this->assertNull($db->getTransaction());

$this->assertEquals(
1,
$db->createCommand(
<<<SQL
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'command transaction 2'
SQL,
)->queryScalar(),
);

$db->close();
}

/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testInsert(): void
{
$db = $this->getConnection(true);
Expand Down Expand Up @@ -1878,12 +1814,6 @@ public function testRenameTable(): void
$db->close();
}

/**
* @throws Exception
* @throws InvalidConfigException
* @throws ReflectionException
* @throws Throwable
*/
public function testSetRetryHandler(): void
{
$db = $this->getConnection(true);
Expand Down Expand Up @@ -1942,47 +1872,6 @@ static function ($exception, $attempt) use (&$attempts, &$hitHandler) {
$db->close();
}

/**
* @throws Exception
* @throws InvalidConfigException
* @throws ReflectionException
* @throws Throwable
*/
public function testTransaction(): void
{
$db = $this->getConnection(true);

$this->assertNull($db->getTransaction());

$command = $db->createCommand();
$command = $command->setSql(
<<<SQL
INSERT INTO [[profile]] ([[description]]) VALUES('command transaction')
SQL
);

Assert::invokeMethod($command, 'requireTransaction');

$command->execute();

$this->assertNull($db->getTransaction());
$this->assertEquals(
1,
$command->setSql(
<<<SQL
SELECT COUNT(*) FROM [[profile]] WHERE [[description]] = 'command transaction'
SQL
)->queryScalar(),
);

$db->close();
}

/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testTruncateTable(): void
{
$db = $this->getConnection(true);
Expand Down