Passed
Push — dev ( a09bd1...8dff19 )
by Def
03:12
created

ConnectionPDOMssql::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql\PDO;
6
7
use PDO;
8
use Yiisoft\Db\Command\CommandInterface;
9
use Yiisoft\Db\Connection\ConnectionPDO;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Mssql\Quoter;
13
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 ConnectionPDOMssql extends ConnectionPDO
23
{
24 377
    public function createCommand(?string $sql = null, array $params = []): CommandInterface
25
    {
26 377
        $command = new CommandPDOMssql($this, $this->queryCache);
27
28 377
        if ($sql !== null) {
29 214
            $command->setSql($sql);
30
        }
31
32 377
        if ($this->logger !== null) {
33 377
            $command->setLogger($this->logger);
34
        }
35
36 377
        if ($this->profiler !== null) {
37 377
            $command->setProfiler($this->profiler);
38
        }
39
40 377
        return $command->bindValues($params);
41
    }
42
43 8
    public function createTransaction(): TransactionInterface
44
    {
45 8
        return new TransactionPDOMssql($this);
46
    }
47
48 11
    public function getDriverName(): string
49
    {
50 11
        return 'sqlsrv';
51
    }
52
53
    /**
54
     * @throws Exception|InvalidConfigException
55
     */
56 377
    public function getQueryBuilder(): QueryBuilderInterface
57
    {
58 377
        if ($this->queryBuilder === null) {
59 377
            $this->queryBuilder = new QueryBuilderPDOMssql(
60 377
                $this->createCommand(),
61 377
                $this->getQuoter(),
62 377
                $this->getSchema(),
63
            );
64
        }
65
66 377
        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...
67
    }
68
69 398
    public function getQuoter(): QuoterInterface
70
    {
71 398
        if ($this->quoter === null) {
72 398
            $this->quoter = new Quoter(['[', ']'], ['[', ']'], $this->getTablePrefix());
73
        }
74
75 398
        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...
76
    }
77
78 380
    public function getSchema(): SchemaInterface
79
    {
80 380
        if ($this->schema === null) {
81 380
            $this->schema = new SchemaPDOMssql($this, $this->schemaCache);
82
        }
83
84 380
        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...
85
    }
86
87
    /**
88
     * Initializes the DB connection.
89
     *
90
     * This method is invoked right after the DB connection is established.
91
     *
92
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
93
     *
94
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
95
     *
96
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
97
     */
98 209
    protected function initConnection(): void
99
    {
100 209
        $this->pdo = $this->driver->createConnection();
101 209
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
102
    }
103
}
104