Passed
Push — master ( f6686f...7eeed4 )
by Def
06:27 queued 03:17
created

ConnectionPDO::initConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use PDO;
8
use Psr\Log\LogLevel;
9
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
10
use Yiisoft\Db\Driver\PDO\ConnectionPDO as AbstractConnectionPDO;
11
use Yiisoft\Db\Exception\Exception;
12
use Yiisoft\Db\Exception\InvalidConfigException;
13
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
14
use Yiisoft\Db\Schema\QuoterInterface;
15
use Yiisoft\Db\Schema\SchemaInterface;
16
use Yiisoft\Db\Transaction\TransactionInterface;
17
18
/**
19
 * Database connection class prefilled for MySQL Server.
20
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
21
 */
22
final class ConnectionPDO extends AbstractConnectionPDO
23
{
24 183
    public function close(): void
25
    {
26 183
        if ($this->pdo !== null) {
27 116
            $this->logger?->log(
28
                LogLevel::DEBUG,
29 116
                'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__,
30
            );
31
32
            // Solution for close connections from https://stackoverflow.com/questions/18277233/pdo-closing-connection
33
            try {
34 116
                $this->pdo->query('KILL CONNECTION_ID()');
35 116
            } catch (\Exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
36
            }
37
38 116
            $this->pdo = null;
39 116
            $this->transaction = null;
40
        }
41
    }
42
43 235
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
44
    {
45 235
        $command = new CommandPDO($this, $this->queryCache);
46
47 235
        if ($sql !== null) {
48 215
            $command->setSql($sql);
49
        }
50
51 235
        if ($this->logger !== null) {
52 94
            $command->setLogger($this->logger);
53
        }
54
55 235
        if ($this->profiler !== null) {
56 94
            $command->setProfiler($this->profiler);
57
        }
58
59 235
        return $command->bindValues($params);
60
    }
61
62 12
    public function createTransaction(): TransactionInterface
63
    {
64 12
        return new TransactionPDO($this);
65
    }
66
67
    /**
68
     * @throws Exception|InvalidConfigException
69
     */
70 459
    public function getQueryBuilder(): QueryBuilderInterface
71
    {
72 459
        if ($this->queryBuilder === null) {
73 459
            $this->queryBuilder = new QueryBuilder(
74 459
                $this->getQuoter(),
75 459
                $this->getSchema(),
76
            );
77
        }
78
79 459
        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\QueryBuilder\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
82 506
    public function getQuoter(): QuoterInterface
83
    {
84 506
        if ($this->quoter === null) {
85 506
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
86
        }
87
88 506
        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...
89
    }
90
91 495
    public function getSchema(): SchemaInterface
92
    {
93 495
        if ($this->schema === null) {
94 495
            $this->schema = new Schema($this, $this->schemaCache);
95
        }
96
97 495
        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...
98
    }
99
}
100