Transaction::setTransactionIsolationLevel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite;
6
7
use Throwable;
8
use Yiisoft\Db\Driver\Pdo\AbstractPdoTransaction;
9
use Yiisoft\Db\Exception\Exception;
10
use Yiisoft\Db\Exception\InvalidConfigException;
11
use Yiisoft\Db\Exception\NotSupportedException;
12
13
/**
14
 * Implements the SQLite Server specific transaction.
15
 */
16
final class Transaction extends AbstractPdoTransaction
17
{
18
    /**
19
     * Sets the isolation level of the current transaction.
20
     *
21
     * @param string $level The transaction isolation level to use for this transaction.
22
     *
23
     * @see \Yiisoft\Db\Transaction\TransactionInterface::READ_UNCOMMITTED
24
     * @see \Yiisoft\Db\Transaction\TransactionInterface::SERIALIZABLE
25
     *
26
     * @throws Exception
27
     * @throws InvalidConfigException
28
     * @throws NotSupportedException
29
     * @throws Throwable When unsupported isolation levels are used. SQLite only supports `SERIALIZABLE`
30
     * and `READ UNCOMMITTED`.
31
     *
32
     * @link https://www.sqlite.org/pragma.html#pragma_read_uncommitted
33
     */
34 4
    protected function setTransactionIsolationLevel(string $level): void
35
    {
36 4
        match ($level) {
37 4
            self::SERIALIZABLE => $this->db->createCommand('PRAGMA read_uncommitted = False;')->execute(),
38 4
            self::READ_UNCOMMITTED => $this->db->createCommand('PRAGMA read_uncommitted = True;')->execute(),
39 4
            default => throw new NotSupportedException(
40 4
                self::class . ' only supports transaction isolation levels READ UNCOMMITTED and SERIALIZABLE.'
41 4
            ),
42 4
        };
43
    }
44
}
45