Test Failed
Pull Request — dev (#107)
by Def
06:53 queued 04:36
created

CommandPDOMysql   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 32
c 3
b 0
f 0
dl 0
loc 59
ccs 25
cts 27
cp 0.9259
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A insertEx() 0 25 4
A queryBuilder() 0 3 1
B internalExecute() 0 22 9
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 internalExecute(?string $rawSql): void
50
    {
51
        $attempt = 0;
52 156
53
        while (true) {
54 156
            try {
55 156
                if (
56
                    ++$attempt === 1
57
                    && $this->isolationLevel !== null
58
                    && $this->db->getTransaction() === null
59
                ) {
60 166
                    $this->db->transaction(fn (?string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
61
                } else {
62 166
                    $this->pdoStatement?->execute();
63
                }
64 166
                break;
65
            } catch (PDOException $e) {
66
                $rawSql = $rawSql ?: $this->getRawSql();
67 166
                $e = (new ConvertException($e, $rawSql))->run();
68 166
69 166
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
70
                    throw $e;
71
                }
72
            }
73 166
        }
74
    }
75
}
76