Command::showDatabases()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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