Test Failed
Push — master ( a28969...4c4427 )
by Sergei
02:41
created

Command::internalExecute()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 27
ccs 19
cts 19
cp 1
rs 8.0555
cc 9
nc 9
nop 1
crap 9
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
    public function insertWithReturningPks(string $table, array $columns): bool|array
16
    {
17
        $params = [];
18
        $sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
19 6
        $this->setSql($sql)->bindValues($params);
20
21 6
        if (!$this->execute()) {
22 6
            return false;
23 6
        }
24
25 6
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
26
        $tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
27
        $result = [];
28
29 6
        foreach ($tablePrimaryKeys as $name) {
30 6
            if ($tableSchema?->getColumn($name)?->isAutoIncrement()) {
31 6
                $result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName());
32
                continue;
33 6
            }
34 6
35 5
            /** @psalm-var mixed */
36 5
            $result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
37
        }
38
39
        return $result;
40 1
    }
41
42
    public function showDatabases(): array
43 6
    {
44
        $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