Completed
Pull Request — dev (#78)
by Def
28:49 queued 28:49
created

ConnectionPDOMssql::close()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 12
nop 0
dl 0
loc 27
ccs 15
cts 15
cp 1
crap 5
rs 9.4555
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
use function array_keys;
25
use function is_string;
26
use function strncmp;
27
28
final class ConnectionPDOMssql extends Connection implements ConnectionPDOInterface
29
{
30
    private ?PDO $pdo = null;
31
    private ?QueryBuilderInterface $queryBuilder = null;
32
    private ?QuoterInterface $quoter = null;
33
    private ?SchemaInterface $schema = null;
34
    private string $serverVersion = '';
35
36 416
    public function __construct(
37
        private PDODriver $driver,
38
        private QueryCache $queryCache,
39
        private SchemaCache $schemaCache
40
    ) {
41 416
        parent::__construct($queryCache);
42
    }
43
44
    /**
45
     * Reset the connection after cloning.
46
     */
47 1
    public function __clone()
48
    {
49 1
        $this->master = null;
50 1
        $this->slave = null;
51 1
        $this->transaction = null;
52
53 1
        if (strncmp($this->driver->getDsn(), 'sqlite::memory:', 15) !== 0) {
54
            /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */
55 1
            $this->pdo = null;
56
        }
57
    }
58
59
    /**
60
     * Close the connection before serializing.
61
     *
62
     * @return array
63
     */
64 1
    public function __sleep(): array
65
    {
66 1
        $fields = (array) $this;
67
68
        unset(
69 1
            $fields["\000" . __CLASS__ . "\000" . 'pdo'],
70 1
            $fields["\000" . __CLASS__ . "\000" . 'master'],
71 1
            $fields["\000" . __CLASS__ . "\000" . 'slave'],
72 1
            $fields["\000" . __CLASS__ . "\000" . 'transaction'],
73 1
            $fields["\000" . __CLASS__ . "\000" . 'schema']
74
        );
75
76 1
        return array_keys($fields);
77
    }
78
79 372
    public function createCommand(?string $sql = null, array $params = []): CommandInterface
80
    {
81 372
        $command = new CommandPDOMssql($this, $this->queryCache);
82
83 372
        if ($sql !== null) {
84 209
            $command->setSql($sql);
85
        }
86
87 372
        if ($this->logger !== null) {
88 372
            $command->setLogger($this->logger);
89
        }
90
91 372
        if ($this->profiler !== null) {
92 372
            $command->setProfiler($this->profiler);
93
        }
94
95 372
        return $command->bindValues($params);
96
    }
97
98 8
    public function createTransaction(): TransactionInterface
99
    {
100 8
        return new TransactionPDOMssql($this);
101
    }
102
103 416
    public function close(): void
104
    {
105 416
        if (!empty($this->master)) {
106
            /** @var ConnectionPDOInterface */
107 2
            $db = $this->master;
108
109 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

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