Passed
Pull Request — master (#106)
by Wilmer
23:45 queued 10:07
created

ConnectionPDO   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 93.55%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 83
ccs 29
cts 31
cp 0.9355
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createTransaction() 0 3 1
A getQueryBuilder() 0 10 2
A createCommand() 0 17 4
A createBatchQueryResult() 0 3 1
A getSchema() 0 7 2
A initConnection() 0 3 1
A getName() 0 3 1
A getQuoter() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
8
use Yiisoft\Db\Driver\PDO\ConnectionPDO as AbstractConnectionPDO;
9
use Yiisoft\Db\Exception\Exception;
10
use Yiisoft\Db\Exception\InvalidConfigException;
11
use Yiisoft\Db\Query\BatchQueryResultInterface;
12
use Yiisoft\Db\Query\QueryInterface;
13
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
14
use Yiisoft\Db\Schema\QuoterInterface;
15
use Yiisoft\Db\Schema\SchemaInterface;
16
use Yiisoft\Db\Transaction\TransactionInterface;
17
18
/**
19
 * Database connection class prefilled for Microsoft SQL Server.
20
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
21
 */
22
final class ConnectionPDO extends AbstractConnectionPDO
23
{
24 3
    public function createBatchQueryResult(QueryInterface $query, bool $each = false): BatchQueryResultInterface
25
    {
26 3
        return new BatchQueryResult($query, $each);
27
    }
28
29 226
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
30
    {
31 226
        $command = new CommandPDO($this, $this->queryCache);
32
33 226
        if ($sql !== null) {
34 225
            $command->setSql($sql);
35
        }
36
37 226
        if ($this->logger !== null) {
38 226
            $command->setLogger($this->logger);
39
        }
40
41 226
        if ($this->profiler !== null) {
42 226
            $command->setProfiler($this->profiler);
43
        }
44
45 226
        return $command->bindValues($params);
46
    }
47
48 11
    public function createTransaction(): TransactionInterface
49
    {
50 11
        return new TransactionPDO($this);
51
    }
52
53
    /**
54
     * @throws Exception|InvalidConfigException
55
     */
56 387
    public function getQueryBuilder(): QueryBuilderInterface
57
    {
58 387
        if ($this->queryBuilder === null) {
59 387
            $this->queryBuilder = new QueryBuilder(
60 387
                $this->getQuoter(),
61 387
                $this->getSchema(),
62
            );
63
        }
64
65 387
        return $this->queryBuilder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->queryBuilder could return the type null which is incompatible with the type-hinted return Yiisoft\Db\QueryBuilder\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
66
    }
67
68
    public function getName(): string
69
    {
70
        return $this->getDriver()->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Yiisoft\Db\Driver\PDO\PDODriverInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        return $this->getDriver()->/** @scrutinizer ignore-call */ getName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    }
72
73 408
    public function getQuoter(): QuoterInterface
74
    {
75 408
        if ($this->quoter === null) {
76 408
            $this->quoter = new Quoter(['[', ']'], ['[', ']'], $this->getTablePrefix());
77
        }
78
79 408
        return $this->quoter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->quoter could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\QuoterInterface. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
82 390
    public function getSchema(): SchemaInterface
83
    {
84 390
        if ($this->schema === null) {
85 390
            $this->schema = new Schema($this, $this->schemaCache);
86
        }
87
88 390
        return $this->schema;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schema could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\SchemaInterface. Consider adding an additional type-check to rule them out.
Loading history...
89
    }
90
91
    /**
92
     * Initializes the DB connection.
93
     *
94
     * This method is invoked right after the DB connection is established.
95
     *
96
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
97
     *
98
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
99
     *
100
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
101
     */
102 221
    protected function initConnection(): void
103
    {
104 221
        $this->pdo = $this->driver->createConnection();
105
    }
106
}
107