Passed
Branch dev (ea35c5)
by Wilmer
17:29 queued 12:43
created

ConnectionPDOSqlite::getDriverName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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