Passed
Push — master ( d9f47f...0d7a1e )
by Wilmer
03:40
created

Command   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 15
eloc 38
dl 0
loc 73
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueryBuilder() 0 3 1
A showDatabases() 0 7 1
B internalExecute() 0 25 9
A insertWithReturningPks() 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\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 Command 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 1
    public function showDatabases(): array
48
    {
49 1
        $sql = <<<SQL
50
        SHOW DATABASES WHERE `Database` NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
51 1
        SQL;
52
53 1
        return $this->setSql($sql)->queryColumn();
54
    }
55
56 266
    protected function getQueryBuilder(): QueryBuilderInterface
57
    {
58 266
        return $this->db->getQueryBuilder();
59
    }
60
61
    /**
62
     * @psalm-suppress UnusedClosureParam
63
     *
64
     * @throws Throwable
65
     */
66 244
    protected function internalExecute(string|null $rawSql): void
67
    {
68 244
        $attempt = 0;
69
70 244
        while (true) {
71
            try {
72
                if (
73 244
                    ++$attempt === 1
74 244
                    && $this->isolationLevel !== null
75 244
                    && $this->db->getTransaction() === null
76
                ) {
77 1
                    $this->db->transaction(
78 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

78
                        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...
79 1
                        $this->isolationLevel,
80 1
                    );
81
                } else {
82 244
                    $this->pdoStatement?->execute();
83
                }
84 244
                break;
85 42
            } catch (PDOException $e) {
86 42
                $rawSql = $rawSql ?: $this->getRawSql();
87 42
                $e = (new ConvertException($e, $rawSql))->run();
88
89 42
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
90 42
                    throw $e;
91
                }
92
            }
93
        }
94
    }
95
}
96