Passed
Push — master ( 8a002b...c640bb )
by Wilmer
05:52 queued 01:51
created

Command::internalExecute()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 9
nop 1
dl 0
loc 25
ccs 17
cts 17
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
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 Command extends AbstractCommandPDO
20
{
21 1
    public function showDatabases(): array
22
    {
23 1
        $sql = <<<SQL
24
        SELECT [name] FROM [sys].[databases] WHERE [name] NOT IN ('master', 'tempdb', 'model', 'msdb')
25 1
        SQL;
26
27 1
        return $this->setSql($sql)->queryColumn();
28
    }
29
30
    /**
31
     * @psalm-suppress UnusedClosureParam
32
     *
33
     * @throws Exception
34
     * @throws Throwable
35
     */
36 614
    protected function internalExecute(string|null $rawSql): void
37
    {
38 614
        $attempt = 0;
39
40 614
        while (true) {
41
            try {
42
                if (
43 614
                    ++$attempt === 1
44 614
                    && $this->isolationLevel !== null
45 614
                    && $this->db->getTransaction() === null
46
                ) {
47 1
                    $this->db->transaction(
48 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

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