Passed
Push — master ( 915131...7aaed1 )
by Wilmer
14:08 queued 09:54
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
/**
15
 * Implements a database command that can be executed with a PDO (PHP Data Object) database connection for MySQL,
16
 * MariaDB.
17
 */
18
final class CommandPDO extends AbstractCommandPDO
19
{
20 6
    public function insertWithReturningPks(string $table, array $columns): bool|array
21
    {
22 6
        $params = [];
23 6
        $sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
24 6
        $this->setSql($sql)->bindValues($params);
25
26 6
        if (!$this->execute()) {
27
            return false;
28
        }
29
30 6
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
31 6
        $tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
32 6
        $result = [];
33
34 6
        foreach ($tablePrimaryKeys as $name) {
35 6
            if ($tableSchema?->getColumn($name)?->isAutoIncrement()) {
36 5
                $result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName());
37 5
                continue;
38
            }
39
40
            /** @psalm-var mixed */
41 1
            $result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
42
        }
43
44 6
        return $result;
45
    }
46
47 265
    protected function getQueryBuilder(): QueryBuilderInterface
48
    {
49 265
        return $this->db->getQueryBuilder();
50
    }
51
52
    /**
53
     * @psalm-suppress UnusedClosureParam
54
     *
55
     * @throws Throwable
56
     */
57 243
    protected function internalExecute(string|null $rawSql): void
58
    {
59 243
        $attempt = 0;
60
61 243
        while (true) {
62
            try {
63
                if (
64 243
                    ++$attempt === 1
65 243
                    && $this->isolationLevel !== null
66 243
                    && $this->db->getTransaction() === null
67
                ) {
68 1
                    $this->db->transaction(
69 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

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