Passed
Push — master ( cd145f...e9c25e )
by Wilmer
18:12 queued 14:05
created

CommandPDO::showDatabases()   A

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