Passed
Pull Request — master (#137)
by Alexander
15:32 queued 12:19
created

CommandPDO::insertEx()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use PDOException;
8
use Yiisoft\Db\Driver\PDO\CommandPDO as AbstractCommandPDO;
9
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
10
use Yiisoft\Db\Exception\ConvertException;
11
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
12
13
use function is_array;
14
15
final class CommandPDO extends AbstractCommandPDO
16
{
17
    /**
18
     * @inheritDoc
19
    */
20 5
    public function insertEx(string $table, array $columns): bool|array
21
    {
22 5
        $params = [];
23 5
        $sql = $this->queryBuilder()->insertEx($table, $columns, $params);
24
25 5
        $this->setSql($sql)->bindValues($params);
26 5
        $this->prepare(false);
27
28
        /** @psalm-var array|bool */
29 5
        $result = $this->queryOne();
30
31 5
        return is_array($result) ? $result : false;
32
    }
33
34 267
    public function queryBuilder(): QueryBuilderInterface
35
    {
36 267
        return $this->db->getQueryBuilder();
37
    }
38
39
    /**
40
     * @psalm-suppress UnusedClosureParam
41
     */
42 255
    protected function internalExecute(string|null $rawSql): void
43
    {
44 255
        $attempt = 0;
45
46 255
        while (true) {
47
            try {
48
                if (
49
                    ++$attempt === 1
50 255
                    && $this->isolationLevel !== null
51 255
                    && $this->db->getTransaction() === null
52
                ) {
53 1
                    $this->db->transaction(
54 1
                        fn (ConnectionPDOInterface $db) => $this->internalExecute($rawSql),
0 ignored issues
show
Unused Code introduced by
The parameter $db is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

54
                        fn (/** @scrutinizer ignore-unused */ ConnectionPDOInterface $db) => $this->internalExecute($rawSql),

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55 1
                        $this->isolationLevel
56
                    );
57
                } else {
58 255
                    $this->pdoStatement?->execute();
59
                }
60 254
                break;
61 9
            } catch (PDOException $e) {
62 9
                $rawSql = $rawSql ?: $this->getRawSql();
63 9
                $e = (new ConvertException($e, $rawSql))->run();
64
65 9
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
66 9
                    throw $e;
67
                }
68
            }
69
        }
70
    }
71
}
72