Passed
Pull Request — dev (#98)
by Def
29:23 queued 17:44
created

setTransactionIsolationLevel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

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