Passed
Push — dev ( 5209d4...332d94 )
by Def
43:00 queued 29:45
created

ConnectionPDO::initConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use PDO;
8
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
9
use Yiisoft\Db\Driver\PDO\ConnectionPDO as AbstractConnectionPDO;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
13
use Yiisoft\Db\Schema\QuoterInterface;
14
use Yiisoft\Db\Schema\SchemaInterface;
15
use Yiisoft\Db\Transaction\TransactionInterface;
16
17
/**
18
 * Database connection class prefilled for Microsoft SQL Server.
19
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
20
 */
21
final class ConnectionPDO extends AbstractConnectionPDO
22
{
23 223
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
24
    {
25 223
        $command = new CommandPDO($this, $this->queryCache);
26
27 223
        if ($sql !== null) {
28 223
            $command->setSql($sql);
29
        }
30
31 223
        if ($this->logger !== null) {
32 223
            $command->setLogger($this->logger);
33
        }
34
35 223
        if ($this->profiler !== null) {
36 223
            $command->setProfiler($this->profiler);
37
        }
38
39 223
        return $command->bindValues($params);
40
    }
41
42 11
    public function createTransaction(): TransactionInterface
43
    {
44 11
        return new TransactionPDO($this);
45
    }
46
47
    /**
48
     * @throws Exception|InvalidConfigException
49
     */
50 385
    public function getQueryBuilder(): QueryBuilderInterface
51
    {
52 385
        if ($this->queryBuilder === null) {
53 385
            $this->queryBuilder = new QueryBuilder(
54 385
                $this->getQuoter(),
55 385
                $this->getSchema(),
56
            );
57
        }
58
59 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...
60
    }
61
62 406
    public function getQuoter(): QuoterInterface
63
    {
64 406
        if ($this->quoter === null) {
65 406
            $this->quoter = new Quoter(['[', ']'], ['[', ']'], $this->getTablePrefix());
66
        }
67
68 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...
69
    }
70
71 387
    public function getSchema(): SchemaInterface
72
    {
73 387
        if ($this->schema === null) {
74 387
            $this->schema = new Schema($this, $this->schemaCache);
75
        }
76
77 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...
78
    }
79
80
    /**
81
     * Initializes the DB connection.
82
     *
83
     * This method is invoked right after the DB connection is established.
84
     *
85
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
86
     *
87
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
88
     *
89
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
90
     */
91 219
    protected function initConnection(): void
92
    {
93 219
        $this->pdo = $this->driver->createConnection();
94
    }
95
}
96