Passed
Push — master ( 4c4427...2e8336 )
by Sergei
33:33 queued 29:40
created

Command   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 19
c 1
b 0
f 0
dl 0
loc 36
ccs 18
cts 19
cp 0.9474
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A showDatabases() 0 7 1
A insertWithReturningPks() 0 25 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use Yiisoft\Db\Driver\Pdo\AbstractPdoCommand;
8
9
/**
10
 * Implements a database command that can be executed with a PDO (PHP Data Object) database connection for MySQL,
11
 * MariaDB.
12
 */
13
final class Command extends AbstractPdoCommand
14
{
15 6
    public function insertWithReturningPks(string $table, array $columns): bool|array
16
    {
17 6
        $params = [];
18 6
        $sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
19 6
        $this->setSql($sql)->bindValues($params);
20
21 6
        if (!$this->execute()) {
22
            return false;
23
        }
24
25 6
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
26 6
        $tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
27 6
        $result = [];
28
29 6
        foreach ($tablePrimaryKeys as $name) {
30 6
            if ($tableSchema?->getColumn($name)?->isAutoIncrement()) {
31 5
                $result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName());
32 5
                continue;
33
            }
34
35
            /** @psalm-var mixed */
36 1
            $result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
37
        }
38
39 6
        return $result;
40
    }
41
42 1
    public function showDatabases(): array
43
    {
44 1
        $sql = <<<SQL
45
        SHOW DATABASES WHERE `Database` NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
46 1
        SQL;
47
48 1
        return $this->setSql($sql)->queryColumn();
49
    }
50
}
51