Passed
Pull Request — dev (#98)
by Wilmer
43:38 queued 28:30
created

ConnectionPDOMysql::getQueryBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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

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