Passed
Push — master ( 00e506...7ca946 )
by Def
04:04 queued 01:28
created

ConnectionPDO::close()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 3
rs 9.9666
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\Quoter;
15
use Yiisoft\Db\Schema\QuoterInterface;
16
use Yiisoft\Db\Schema\SchemaInterface;
17
use Yiisoft\Db\Transaction\TransactionInterface;
18
19
/**
20
 * Database connection class prefilled for MySQL Server.
21
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
22
 */
23
final class ConnectionPDO extends AbstractConnectionPDO
24
{
25 438
    public function close(): void
26
    {
27 438
        if ($this->pdo !== null) {
28 404
            $this->logger?->log(
29
                LogLevel::DEBUG,
30 404
                'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__,
31
            );
32
33
            // Solution for close connections from https://stackoverflow.com/questions/18277233/pdo-closing-connection
34
            try {
35 404
                $this->pdo->query('KILL CONNECTION_ID()');
36 404
            } catch (\Exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
37
            }
38
39 404
            $this->pdo = null;
40 404
            $this->transaction = null;
41
        }
42
    }
43
44 187
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
45
    {
46 187
        $command = new CommandPDO($this, $this->queryCache);
47
48 187
        if ($sql !== null) {
49 185
            $command->setSql($sql);
50
        }
51
52 187
        if ($this->logger !== null) {
53 187
            $command->setLogger($this->logger);
54
        }
55
56 187
        if ($this->profiler !== null) {
57 187
            $command->setProfiler($this->profiler);
58
        }
59
60 187
        return $command->bindValues($params);
61
    }
62
63 11
    public function createTransaction(): TransactionInterface
64
    {
65 11
        return new TransactionPDO($this);
66
    }
67
68
    /**
69
     * @throws Exception|InvalidConfigException
70
     */
71 358
    public function getQueryBuilder(): QueryBuilderInterface
72
    {
73 358
        if ($this->queryBuilder === null) {
74 358
            $this->queryBuilder = new QueryBuilder(
75 358
                $this->getQuoter(),
76 358
                $this->getSchema(),
77
            );
78
        }
79
80 358
        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...
81
    }
82
83 385
    public function getQuoter(): QuoterInterface
84
    {
85 385
        if ($this->quoter === null) {
86 385
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix(), $this->getActivePDO());
87
        }
88
89 385
        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...
90
    }
91
92 385
    public function getSchema(): SchemaInterface
93
    {
94 385
        if ($this->schema === null) {
95 385
            $this->schema = new Schema($this, $this->schemaCache);
96
        }
97
98 385
        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...
99
    }
100
101
    /**
102
     * Initializes the DB connection.
103
     *
104
     * This method is invoked right after the DB connection is established.
105
     *
106
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
107
     *
108
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
109
     *
110
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
111
     */
112 407
    protected function initConnection(): void
113
    {
114 407
        if ($this->getEmulatePrepare() !== null) {
115 2
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
116
        }
117
118 407
        $this->pdo = $this->driver->createConnection();
119
    }
120
}
121