Passed
Pull Request — master (#201)
by Def
25:55 queued 22:15
created

CommandPDO::insertWithReturningPks()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 25
ccs 14
cts 15
cp 0.9333
crap 4.0047
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\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 3
    public function insertWithReturningPks(string $table, array $columns): bool|array
17
    {
18 3
        $params = [];
19 3
        $sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
20 3
        $this->setSql($sql)->bindValues($params);
21
22 3
        if (!$this->execute()) {
23
            return false;
24
        }
25
26 3
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
27 3
        $tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
28
29 3
        $result = [];
30 3
        foreach ($tablePrimaryKeys as $name) {
31 3
            if ($tableSchema?->getColumn($name)?->isAutoIncrement()) {
32 2
                $result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName());
33 2
                continue;
34
            }
35
36
            /** @psalm-var mixed */
37 1
            $result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
38
        }
39
40 3
        return $result;
41
    }
42
43 247
    public function queryBuilder(): QueryBuilderInterface
44
    {
45 247
        return $this->db->getQueryBuilder();
46
    }
47
48
    /**
49
     * @psalm-suppress UnusedClosureParam
50
     *
51
     * @throws Throwable
52
     */
53 226
    protected function internalExecute(string|null $rawSql): void
54
    {
55 226
        $attempt = 0;
56
57 226
        while (true) {
58
            try {
59
                if (
60 226
                    ++$attempt === 1
61 226
                    && $this->isolationLevel !== null
62 226
                    && $this->db->getTransaction() === null
63
                ) {
64 1
                    $this->db->transaction(
65 1
                        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

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