Test Failed
Pull Request — master (#255)
by Wilmer
04:17
created

CommandPDO::showDatabases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
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\Exception\Exception;
13
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
14
15
/**
16
 * Implements a database command that can be executed against a PDO (PHP Data Object) database connection for MSSQL
17
 * Server.
18
 */
19
final class CommandPDO extends AbstractCommandPDO
20
{
21
    public function showDatabases(): array
22
    {
23
        $sql = <<<SQL
24
        SELECT [name] FROM [sys].[databases] WHERE [name] NOT IN ('master', 'tempdb', 'model', 'msdb')
25
        SQL;
26
27 613
        return $this->setSql($sql)->queryColumn();
28
    }
29 613
30
    /**
31 613
     * @psalm-suppress UnusedClosureParam
32
     *
33
     * @throws Exception
34 613
     * @throws Throwable
35 613
     */
36 613
    protected function internalExecute(string|null $rawSql): void
37
    {
38 1
        $attempt = 0;
39 1
40 1
        while (true) {
41 1
            try {
42
                if (
43 613
                    ++$attempt === 1
44
                    && $this->isolationLevel !== null
45 612
                    && $this->db->getTransaction() === null
46 30
                ) {
47 30
                    $this->db->transaction(
48 30
                        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

48
                        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...
49
                        $this->isolationLevel
50 30
                    );
51 30
                } else {
52
                    $this->pdoStatement?->execute();
53
                }
54
                break;
55
            } catch (PDOException $e) {
56
                $rawSql = $rawSql ?: $this->getRawSql();
57 637
                $e = (new ConvertException($e, $rawSql))->run();
58
59 637
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
60
                    throw $e;
61
                }
62
            }
63
        }
64
    }
65
66
    protected function getQueryBuilder(): QueryBuilderInterface
67
    {
68
        return $this->db->getQueryBuilder();
69
    }
70
}
71