Test Failed
Pull Request — dev (#95)
by Def
03:00
created

ConnectionPDOMssql::getSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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