Test Failed
Pull Request — dev (#73)
by Def
04:16
created

CommandPDOMssql::insertEx()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 9
ccs 4
cts 4
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\Cache\QueryCache;
9
use Yiisoft\Db\Command\Command;
10
use Yiisoft\Db\Connection\ConnectionPDOInterface;
11
use Yiisoft\Db\Exception\ConvertException;
12
use Yiisoft\Db\Exception\Exception;
13
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
final class CommandPDOMssql extends Command
16
{
17 366
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
18
    {
19 366
        parent::__construct($queryCache);
20
    }
21
22 203
    /**
23
     * @inheritDoc
24 203
     */
25
    public function insertEx(string $table, array $columns): bool|array
26
    {
27 194
        $params = [];
28
        $sql = $this->queryBuilder()->insertEx($table, $columns, $params);
29 194
30 3
        $this->setSql($sql)->bindValues($params);
31 3
        $this->prepare(false);
32
33
        return $this->queryOne();
34 194
    }
35
36 194
    public function queryBuilder(): QueryBuilderInterface
37
    {
38 5
        return $this->db->getQueryBuilder();
39
    }
40
41 194
    public function prepare(?bool $forRead = null): void
42 189
    {
43
        if (isset($this->pdoStatement)) {
44 58
            $this->bindPendingParams();
45
            return;
46
        }
47
48 194
        $sql = $this->getSql() ?? '';
49 194
50
        if ($this->db->getTransaction()) {
51
            /** master is in a transaction. use the same connection. */
52
            $forRead = false;
53
        }
54
55
        if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) {
56
            $pdo = $this->db->getSlavePdo();
57
        } else {
58
            $pdo = $this->db->getMasterPdo();
59 2
        }
60
61
        try {
62
            $this->pdoStatement = $pdo?->prepare($sql);
63
            $this->bindPendingParams();
64
        } catch (PDOException $e) {
65 2
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
66 2
            /** @var array|null */
67
            $errorInfo = $e->errorInfo ?? null;
68
69
            throw new Exception($message, $errorInfo, $e);
70
        }
71 193
    }
72
73 193
    protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): array
74
    {
75 193
        return [
76
            __CLASS__,
77
            $method,
78 193
            $fetchMode,
79 193
            $this->db->getDriver()->getDsn(),
80 193
            $this->db->getDriver()->getUsername(),
81
            $rawSql,
82
        ];
83
    }
84
85
    protected function internalExecute(?string $rawSql): void
86
    {
87 193
        $attempt = 0;
88
89 192
        while (true) {
90 5
            try {
91 5
                if (
92 5
                    ++$attempt === 1
93
                    && $this->isolationLevel !== null
94 5
                    && $this->db->getTransaction() === null
95 5
                ) {
96
                    $this->db->transaction(
97
                        fn (string $rawSql) => $this->internalExecute($rawSql),
98
                        $this->isolationLevel
99
                    );
100
                } else {
101
                    $this->pdoStatement?->execute();
102
                }
103
                break;
104
            } catch (PDOException $e) {
105
                $rawSql = $rawSql ?: $this->getRawSql();
106
                $e = (new ConvertException($e, $rawSql))->run();
107
108
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
109
                    throw $e;
110
                }
111
            }
112
        }
113
    }
114
}
115