Passed
Push — dev ( cc6591...c9f34d )
by Wilmer
04:23 queued 23s
created

ConnectionPDOMssql::getMasterPdo()   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
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
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 PDOException;
9
use Psr\Log\LogLevel;
10
use Yiisoft\Db\Cache\QueryCache;
11
use Yiisoft\Db\Cache\SchemaCache;
12
use Yiisoft\Db\Command\CommandInterface;
13
use Yiisoft\Db\Connection\Connection;
14
use Yiisoft\Db\Connection\ConnectionPDOInterface;
15
use Yiisoft\Db\Driver\PDODriver;
16
use Yiisoft\Db\Exception\Exception;
17
use Yiisoft\Db\Exception\InvalidConfigException;
18
use Yiisoft\Db\Mssql\Quoter;
19
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...
20
use Yiisoft\Db\Schema\QuoterInterface;
21
use Yiisoft\Db\Schema\SchemaInterface;
22
use Yiisoft\Db\Transaction\TransactionInterface;
23
24
final class ConnectionPDOMssql extends Connection implements ConnectionPDOInterface
25
{
26
    private ?PDO $pdo = null;
27
    private ?QueryBuilderInterface $queryBuilder = null;
28
    private ?QuoterInterface $quoter = null;
29
    private ?SchemaInterface $schema = null;
30
    private string $serverVersion = '';
31
32 413
    public function __construct(
33
        private PDODriver $driver,
34
        private QueryCache $queryCache,
35
        private SchemaCache $schemaCache
36
    ) {
37 413
        parent::__construct($queryCache);
38
    }
39
40
    /**
41
     * Reset the connection after cloning.
42
     */
43 1
    public function __clone()
44
    {
45 1
        $this->master = null;
46 1
        $this->slave = null;
47 1
        $this->transaction = null;
48
49 1
        if (strncmp($this->driver->getDsn(), 'sqlite::memory:', 15) !== 0) {
50
            /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */
51 1
            $this->pdo = null;
52
        }
53
    }
54
55
    /**
56
     * Close the connection before serializing.
57
     *
58
     * @return array
59
     */
60 1
    public function __sleep(): array
61
    {
62 1
        $fields = (array) $this;
63
64
        unset(
65 1
            $fields["\000" . __CLASS__ . "\000" . 'pdo'],
66 1
            $fields["\000" . __CLASS__ . "\000" . 'master'],
67 1
            $fields["\000" . __CLASS__ . "\000" . 'slave'],
68 1
            $fields["\000" . __CLASS__ . "\000" . 'transaction'],
69 1
            $fields["\000" . __CLASS__ . "\000" . 'schema']
70
        );
71
72 1
        return array_keys($fields);
73
    }
74
75 367
    public function createCommand(?string $sql = null, array $params = []): CommandInterface
76
    {
77 367
        $command = new CommandPDOMssql($this, $this->queryCache);
78
79 367
        if ($sql !== null) {
80 204
            $command->setSql($sql);
81
        }
82
83 367
        if ($this->logger !== null) {
84 367
            $command->setLogger($this->logger);
85
        }
86
87 367
        if ($this->profiler !== null) {
88 367
            $command->setProfiler($this->profiler);
89
        }
90
91 367
        return $command->bindValues($params);
92
    }
93
94 8
    public function createTransaction(): TransactionInterface
95
    {
96 8
        return new TransactionPDOMssql($this);
97
    }
98
99 413
    public function close(): void
100
    {
101 413
        if (!empty($this->master)) {
102
            /** @var ConnectionPDOInterface */
103 2
            $db = $this->master;
104
105 2
            if ($this->pdo === $db->getPDO()) {
0 ignored issues
show
Bug introduced by
The method getPDO() does not exist on Yiisoft\Db\Connection\ConnectionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Yiisoft\Db\Connection\Connection. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
            if ($this->pdo === $db->/** @scrutinizer ignore-call */ getPDO()) {
Loading history...
106 2
                $this->pdo = null;
107
            }
108
109 2
            $db->close();
110 2
            $this->master = null;
111
        }
112
113 413
        if ($this->pdo !== null) {
114 199
            $this->logger?->log(
115
                LogLevel::DEBUG,
116 197
                'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__,
117
            );
118
119 199
            $this->pdo = null;
120 199
            $this->transaction = null;
121
        }
122
123 413
        if (!empty($this->slave)) {
124 1
            $this->slave->close();
125 1
            $this->slave = null;
126
        }
127
    }
128
129 168
    public function getDriver(): PDODriver
130
    {
131 168
        return $this->driver;
132
    }
133
134 11
    public function getDriverName(): string
135
    {
136 11
        return 'sqlsrv';
137
    }
138
139 197
    public function getMasterPdo(): PDO|null
140
    {
141 197
        $this->open();
142 197
        return $this->pdo;
143
    }
144
145 44
    public function getPDO(): ?PDO
146
    {
147 44
        return $this->pdo;
148
    }
149
150
    /**
151
     * @throws Exception|InvalidConfigException
152
     */
153 367
    public function getQueryBuilder(): QueryBuilderInterface
154
    {
155 367
        if ($this->queryBuilder === null) {
156 367
            $this->queryBuilder = new QueryBuilderPDOMssql(
157 367
                $this->createCommand(),
158 367
                $this->getQuoter(),
159 367
                $this->getSchema(),
160
            );
161
        }
162
163 367
        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...
164
    }
165
166 379
    public function getQuoter(): QuoterInterface
167
    {
168 379
        if ($this->quoter === null) {
169 379
            $this->quoter = new Quoter(['[', ']'], ['[', ']'], $this->getTablePrefix());
170
        }
171
172 379
        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...
173
    }
174
175
    /**
176
     * @throws Exception
177
     */
178 3
    public function getServerVersion(): string
179
    {
180 3
        if ($this->serverVersion === '') {
181
            /** @var mixed */
182 3
            $version = $this->getSlavePDO()?->getAttribute(PDO::ATTR_SERVER_VERSION);
183 3
            $this->serverVersion = is_string($version) ? $version : 'Version could not be determined.';
184
        }
185
186 3
        return $this->serverVersion;
187
    }
188
189 373
    public function getSchema(): SchemaInterface
190
    {
191 373
        if ($this->schema === null) {
192 373
            $this->schema = new SchemaPDOMssql($this, $this->schemaCache);
193
        }
194
195 373
        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...
196
    }
197
198 192
    public function getSlavePdo(bool $fallbackToMaster = true): ?PDO
199
    {
200 192
        $db = $this->getSlave(false);
201
202 192
        if ($db === null) {
203 191
            return $fallbackToMaster ? $this->getMasterPdo() : null;
204
        }
205
206 1
        return $db->getPDO();
207
    }
208
209 12
    public function isActive(): bool
210
    {
211 12
        return $this->pdo !== null;
212
    }
213
214 202
    public function open(): void
215
    {
216 202
        if (!empty($this->pdo)) {
217 187
            return;
218
        }
219
220 202
        if (!empty($this->masters)) {
221 2
            $db = $this->getMaster();
222
223 2
            if ($db !== null) {
224 2
                $this->pdo = $db->getPDO();
225 2
                return;
226
            }
227
228 2
            throw new InvalidConfigException('None of the master DB servers is available.');
229
        }
230
231 202
        $token = 'Opening DB connection: ' . $this->driver->getDsn();
232
233
        try {
234 202
            $this->logger?->log(LogLevel::INFO, $token);
235 202
            $this->profiler?->begin($token, [__METHOD__]);
236 202
            $this->initConnection();
237 202
            $this->profiler?->end($token, [__METHOD__]);
238 3
        } catch (PDOException $e) {
239 3
            $this->profiler?->end($token, [__METHOD__]);
240 3
            $this->logger?->log(LogLevel::ERROR, $token);
241
242 3
            throw new Exception($e->getMessage(), (array) $e->errorInfo, $e);
243
        }
244
    }
245
246
    /**
247
     * Initializes the DB connection.
248
     *
249
     * This method is invoked right after the DB connection is established.
250
     *
251
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
252
     *
253
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
254
     *
255
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
256
     */
257 202
    protected function initConnection(): void
258
    {
259 202
        $this->pdo = $this->driver->createConnection();
260 202
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
261
    }
262
}
263