Command   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A showDatabases() 0 7 1
A insertWithReturningPks() 0 26 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 8
    public function insertWithReturningPks(string $table, array $columns): bool|array
16
    {
17 8
        $params = [];
18 8
        $sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
19 8
        $this->setSql($sql)->bindValues($params);
20
21 8
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
22
23 8
        if (!$this->execute()) {
24
            return false;
25
        }
26
27 8
        $tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
28 8
        $result = [];
29
30 8
        foreach ($tablePrimaryKeys as $name) {
31 7
            if ($tableSchema?->getColumn($name)?->isAutoIncrement()) {
32 6
                $result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName());
33 6
                continue;
34
            }
35
36
            /** @psalm-var mixed */
37 1
            $result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
38
        }
39
40 8
        return $result;
41
    }
42
43 1
    public function showDatabases(): array
44
    {
45 1
        $sql = <<<SQL
46
        SHOW DATABASES WHERE `Database` NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
47 1
        SQL;
48
49 1
        return $this->setSql($sql)->queryColumn();
50
    }
51
}
52