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

CommandPDO   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 24
c 3
b 1
f 0
dl 0
loc 50
ccs 19
cts 19
cp 1
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
B internalExecute() 0 25 9
A showDatabases() 0 7 1
A getQueryBuilder() 0 3 1
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