Passed
Push — dev ( 332d94...aa1494 )
by Def
03:03
created

ConnectionPDO   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 78
ccs 29
cts 29
cp 1
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchema() 0 7 2
A createTransaction() 0 3 1
A initConnection() 0 3 1
A getQueryBuilder() 0 10 2
A createCommand() 0 17 4
A getQuoter() 0 7 2
A createBatchQueryResult() 0 3 1
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 223
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
30
    {
31 223
        $command = new CommandPDO($this, $this->queryCache);
32
33 223
        if ($sql !== null) {
34 223
            $command->setSql($sql);
35
        }
36
37 223
        if ($this->logger !== null) {
38 223
            $command->setLogger($this->logger);
39
        }
40
41 223
        if ($this->profiler !== null) {
42 223
            $command->setProfiler($this->profiler);
43
        }
44
45 223
        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 385
    public function getQueryBuilder(): QueryBuilderInterface
57
    {
58 385
        if ($this->queryBuilder === null) {
59 385
            $this->queryBuilder = new QueryBuilder(
60 385
                $this->getQuoter(),
61 385
                $this->getSchema(),
62
            );
63
        }
64
65 385
        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 406
    public function getQuoter(): QuoterInterface
69
    {
70 406
        if ($this->quoter === null) {
71 406
            $this->quoter = new Quoter(['[', ']'], ['[', ']'], $this->getTablePrefix());
72
        }
73
74 406
        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...
75
    }
76
77 387
    public function getSchema(): SchemaInterface
78
    {
79 387
        if ($this->schema === null) {
80 387
            $this->schema = new Schema($this, $this->schemaCache);
81
        }
82
83 387
        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...
84
    }
85
86
    /**
87
     * Initializes the DB connection.
88
     *
89
     * This method is invoked right after the DB connection is established.
90
     *
91
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
92
     *
93
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
94
     *
95
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
96
     */
97 219
    protected function initConnection(): void
98
    {
99 219
        $this->pdo = $this->driver->createConnection();
100
    }
101
}
102