Test Failed
Pull Request — dev (#93)
by Wilmer
03:09
created

CommandPDOMssql::schema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql\PDO;
6
7
use PDOException;
8
use Yiisoft\Db\Driver\PDO\CommandPDO;
9
use Yiisoft\Db\Exception\ConvertException;
10
use Yiisoft\Db\Query\QueryBuilderInterface;
11
use Yiisoft\Db\Schema\SchemaInterface;
12
13
use function is_array;
14
15
final class CommandPDOMssql extends CommandPDO
16
{
17
    /**
18
     * @inheritDoc
19 2
    */
20
    public function insertEx(string $table, array $columns): bool|array
21 2
    {
22 2
        $params = [];
23
        $sql = $this->queryBuilder()->insertEx($table, $columns, $params);
24 2
25 2
        $this->setSql($sql)->bindValues($params);
26
        $this->prepare(false);
27
28 2
        /** @psalm-var array|bool */
29
        $result = $this->queryOne();
30 2
31
        return is_array($result) ? $result : false;
32
    }
33 222
34
    public function queryBuilder(): QueryBuilderInterface
35 222
    {
36
        return $this->db->getQueryBuilder();
37
    }
38 211
39
    public function schema(): SchemaInterface
40 211
    {
41
        return $this->db->getSchema();
42 211
    }
43
44
    protected function internalExecute(?string $rawSql): void
45 211
    {
46 211
        $attempt = 0;
47 211
48
        while (true) {
49
            try {
50
                if (
51
                    ++$attempt === 1
52
                    && $this->isolationLevel !== null
53
                    && $this->db->getTransaction() === null
54 211
                ) {
55
                    $this->db->transaction(
56 210
                        fn (string $rawSql) => $this->internalExecute($rawSql),
57 5
                        $this->isolationLevel
58 5
                    );
59 5
                } else {
60
                    $this->pdoStatement?->execute();
61 5
                }
62 5
                break;
63
            } catch (PDOException $e) {
64
                $rawSql = $rawSql ?: $this->getRawSql();
65
                $e = (new ConvertException($e, $rawSql))->run();
66
67
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
68
                    throw $e;
69
                }
70
            }
71
        }
72
    }
73
}
74