Test Failed
Pull Request — master (#163)
by Wilmer
16:00 queued 13:25
created

CommandPDO   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 79.41%

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 67
ccs 27
cts 34
cp 0.7941
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B internalExecute() 0 25 9
A queryBuilder() 0 3 1
A insertEx() 0 25 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use PDOException;
8
use Throwable;
9
use Yiisoft\Db\Driver\PDO\CommandPDO as AbstractCommandPDO;
10
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
11
use Yiisoft\Db\Exception\ConvertException;
12
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
13
14
final class CommandPDO extends AbstractCommandPDO
15
{
16
    /**
17
     * @inheritDoc
18 1
     */
19
    public function insertEx(string $table, array $columns): bool|array
20 1
    {
21 1
        $params = [];
22 1
        $sql = $this->db->getQueryBuilder()->insertEx($table, $columns, $params);
23
        $this->setSql($sql)->bindValues($params);
24 1
25
        if (!$this->execute()) {
26
            return false;
27
        }
28 1
29 1
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
30
        $tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
31 1
32 1
        $result = [];
33 1
        foreach ($tablePrimaryKeys as $name) {
34 1
            if ($tableSchema?->getColumn($name)?->isAutoIncrement()) {
35 1
                $result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName());
36
                continue;
37
            }
38
39
            /** @psalm-var mixed */
40
            $result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
41
        }
42 1
43
        return $result;
44
    }
45 188
46
    public function queryBuilder(): QueryBuilderInterface
47 188
    {
48
        return $this->db->getQueryBuilder();
49
    }
50
51
    /**
52
     * @psalm-suppress UnusedClosureParam
53
     *
54
     * @throws Throwable
55 177
     */
56
    protected function internalExecute(string|null $rawSql): void
57 177
    {
58
        $attempt = 0;
59 177
60
        while (true) {
61
            try {
62
                if (
63 177
                    ++$attempt === 1
64 177
                    && $this->isolationLevel !== null
65
                    && $this->db->getTransaction() === null
66
                ) {
67
                    $this->db->transaction(
68
                        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

68
                        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...
69
                        $this->isolationLevel,
70
                    );
71 177
                } else {
72
                    $this->pdoStatement?->execute();
73 175
                }
74 22
                break;
75 22
            } catch (PDOException $e) {
76 22
                $rawSql = $rawSql ?: $this->getRawSql();
77
                $e = (new ConvertException($e, $rawSql))->run();
78 22
79 22
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
80
                    throw $e;
81
                }
82
            }
83
        }
84
    }
85
}
86