Passed
Push — dev ( 76dd6e...27bec4 )
by Def
31:49 queued 28:08
created

ConnectionPDOMssql::initConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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