Passed
Push — dev ( fdf8fd...812003 )
by Def
28:15 queued 25:26
created

CommandPDOMysql::insertEx()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0378

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 2
dl 0
loc 25
ccs 13
cts 15
cp 0.8667
crap 4.0378
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\PDO;
6
7
use PDOException;
8
use Yiisoft\Db\Command\CommandPDO;
9
use Yiisoft\Db\Exception\ConvertException;
10
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...
11
12
final class CommandPDOMysql extends CommandPDO
13
{
14
    /**
15
     * @inheritDoc
16
     */
17 1
    public function insertEx(string $table, array $columns): bool|array
18
    {
19 1
        $params = [];
20 1
        $sql = $this->db->getQueryBuilder()->insertEx($table, $columns, $params);
21 1
        $this->setSql($sql)->bindValues($params);
22
23 1
        if (!$this->execute()) {
24
            return false;
25
        }
26
27 1
        $tableSchema = $this->queryBuilder()->schema()->getTableSchema($table);
28 1
        $tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
29
30 1
        $result = [];
31 1
        foreach ($tablePrimaryKeys as $name) {
32 1
            if ($tableSchema?->getColumn($name)?->isAutoIncrement()) {
33 1
                $result[$name] = $this->queryBuilder()->schema()->getLastInsertID((string) $tableSchema?->getSequenceName());
34 1
                continue;
35
            }
36
37
            /** @var mixed */
38
            $result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
39
        }
40
41 1
        return $result;
42
    }
43
44 177
    public function queryBuilder(): QueryBuilderInterface
45
    {
46 177
        return $this->db->getQueryBuilder();
47
    }
48
49 156
    protected function getCacheKey(int $queryMode, string $rawSql): array
50
    {
51
        return [
52 156
            __CLASS__,
53
            $queryMode,
54 156
            $this->db->getDriver()->getDsn(),
55 156
            $this->db->getDriver()->getUsername(),
56
            $rawSql,
57
        ];
58
    }
59
60 166
    protected function internalExecute(?string $rawSql): void
61
    {
62 166
        $attempt = 0;
63
64 166
        while (true) {
65
            try {
66
                if (
67 166
                    ++$attempt === 1
68 166
                    && $this->isolationLevel !== null
69 166
                    && $this->db->getTransaction() === null
70
                ) {
71
                    $this->db->transaction(fn (?string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
72
                } else {
73 166
                    $this->pdoStatement?->execute();
74
                }
75 163
                break;
76 23
            } catch (PDOException $e) {
77 23
                $rawSql = $rawSql ?: $this->getRawSql();
78 23
                $e = (new ConvertException($e, $rawSql))->run();
79
80 23
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
81 23
                    throw $e;
82
                }
83
            }
84
        }
85
    }
86
}
87