Test Failed
Push — master ( cd5e80...cf80ad )
by Wilmer
17:24 queued 14:48
created

CommandPDO::insertEx()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 0
Metric Value
cc 4
eloc 14
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 25
ccs 12
cts 14
cp 0.8571
crap 4.0466
rs 9.7998
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