Passed
Branch dev (d14b82)
by Wilmer
12:57
created

CommandPDOPgsql::insertEx()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\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 CommandPDOPgsql extends Command
16
{
17 401
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
18
    {
19 401
        parent::__construct($queryCache);
20
    }
21
22
    /**
23
     * @inheritDoc
24
     */
25 1
    public function insertEx(string $table, array $columns): bool|array
26
    {
27 1
        $params = [];
28 1
        $sql = $this->queryBuilder()->insertEx($table, $columns, $params);
29
30 1
        $this->setSql($sql)->bindValues($params);
31 1
        $this->prepare(false);
32
33 1
        return $this->queryOne();
34
    }
35
36 191
    public function queryBuilder(): QueryBuilderInterface
37
    {
38 191
        return $this->db->getQueryBuilder();
39
    }
40
41 182
    public function prepare(?bool $forRead = null): void
42
    {
43 182
        if (isset($this->pdoStatement)) {
44 6
            $this->bindPendingParams();
45
46 6
            return;
47
        }
48
49 182
        $sql = $this->getSql();
50
51 182
        if ($this->db->getTransaction()) {
52
            /** master is in a transaction. use the same connection. */
53 6
            $forRead = false;
54
        }
55
56 182
        if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) {
57 177
            $pdo = $this->db->getSlavePdo();
58
        } else {
59 65
            $pdo = $this->db->getMasterPdo();
60
        }
61
62
        try {
63 182
            $this->pdoStatement = $pdo->prepare($sql);
64 182
            $this->bindPendingParams();
65
        } catch (PDOException $e) {
66
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
67
            $errorInfo = $e->errorInfo ?? null;
68
69
            throw new Exception($message, $errorInfo, $e);
70
        }
71
    }
72
73 2
    protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): array
74
    {
75
        return [
76
            __CLASS__,
77
            $method,
78
            $fetchMode,
79 2
            $this->db->getDriver()->getDsn(),
80 2
            $this->db->getDriver()->getUsername(),
81
            $rawSql,
82
        ];
83
    }
84
85 181
    protected function internalExecute(?string $rawSql): void
86
    {
87 181
        $attempt = 0;
88
89 181
        while (true) {
90
            try {
91
                if (
92 181
                    ++$attempt === 1
93 181
                    && $this->isolationLevel !== null
94 181
                    && $this->db->getTransaction() === null
95
                ) {
96
                    $this->db->transaction(fn ($rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
97
                } else {
98 181
                    $this->pdoStatement->execute();
0 ignored issues
show
Bug introduced by
The method execute() does not exist on null. ( Ignorable by Annotation )

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

98
                    $this->pdoStatement->/** @scrutinizer ignore-call */ 
99
                                         execute();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
                }
100 181
                break;
101 7
            } catch (\Exception $e) {
102 7
                $rawSql = $rawSql ?: $this->getRawSql();
103 7
                $e = (new ConvertException($e, $rawSql))->run();
104
105 7
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
106 7
                    throw $e;
107
                }
108
            }
109
        }
110
    }
111
}
112