Passed
Pull Request — master (#148)
by Def
02:33
created

ConnectionPDO::close()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
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 15
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 404
            $this->pdo = null;
39 404
            $this->transaction = null;
40
        }
41
    }
42
43 187
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
44
    {
45 187
        $command = new CommandPDO($this, $this->queryCache);
46
47 187
        if ($sql !== null) {
48 185
            $command->setSql($sql);
49
        }
50
51 187
        if ($this->logger !== null) {
52 187
            $command->setLogger($this->logger);
53
        }
54
55 187
        if ($this->profiler !== null) {
56 187
            $command->setProfiler($this->profiler);
57
        }
58
59 187
        return $command->bindValues($params);
60
    }
61
62 11
    public function createTransaction(): TransactionInterface
63
    {
64 11
        return new TransactionPDO($this);
65
    }
66
67
    /**
68
     * @throws Exception|InvalidConfigException
69
     */
70 358
    public function getQueryBuilder(): QueryBuilderInterface
71
    {
72 358
        if ($this->queryBuilder === null) {
73 358
            $this->queryBuilder = new QueryBuilder(
74 358
                $this->getQuoter(),
75 358
                $this->getSchema(),
76
            );
77
        }
78
79 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...
80
    }
81
82 385
    public function getQuoter(): QuoterInterface
83
    {
84 385
        if ($this->quoter === null) {
85 385
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix(), $this->getActivePDO());
86
        }
87
88 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...
89
    }
90
91 385
    public function getSchema(): SchemaInterface
92
    {
93 385
        if ($this->schema === null) {
94 385
            $this->schema = new Schema($this, $this->schemaCache);
95
        }
96
97 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...
98
    }
99
100
    /**
101
     * Initializes the DB connection.
102
     *
103
     * This method is invoked right after the DB connection is established.
104
     *
105
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
106
     *
107
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
108
     *
109
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
110
     */
111 407
    protected function initConnection(): void
112
    {
113 407
        if ($this->getEmulatePrepare() !== null) {
114 2
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
115
        }
116
117 407
        $this->pdo = $this->driver->createConnection();
118
    }
119
}
120