Issues (10)

src/Connection.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use Exception;
8
use Psr\Log\LogLevel;
9
use Yiisoft\Db\Driver\Pdo\AbstractPdoConnection;
10
use Yiisoft\Db\Driver\Pdo\PdoCommandInterface;
11
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
12
use Yiisoft\Db\Schema\QuoterInterface;
13
use Yiisoft\Db\Schema\SchemaInterface;
14
use Yiisoft\Db\Transaction\TransactionInterface;
15
16
/**
17
 * Implements a connection to a database via PDO (PHP Data Objects) for MySQL, MariaDB.
18
 *
19
 * @link https://www.php.net/manual/en/ref.pdo-mysql.php
20
 */
21
final class Connection extends AbstractPdoConnection
22
{
23 195
    public function close(): void
24
    {
25 195
        if ($this->pdo !== null) {
26 183
            $this->logger?->log(
27 183
                LogLevel::DEBUG,
28 183
                'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__,
29 183
            );
30
31
            // Solution for close connections {@link https://stackoverflow.com/questions/18277233/pdo-closing-connection}
32
            try {
33 183
                $this->pdo->query('KILL CONNECTION_ID()');
34 183
            } catch (Exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
35
            }
36
37 183
            $this->pdo = null;
38 183
            $this->transaction = null;
39
        }
40
    }
41
42 296
    public function createCommand(string $sql = null, array $params = []): PdoCommandInterface
43
    {
44 296
        $command = new Command($this);
45
46 296
        if ($sql !== null) {
47 270
            $command->setSql($sql);
48
        }
49
50 296
        if ($this->logger !== null) {
51 2
            $command->setLogger($this->logger);
52
        }
53
54 296
        if ($this->profiler !== null) {
55 3
            $command->setProfiler($this->profiler);
56
        }
57
58 296
        return $command->bindValues($params);
59
    }
60
61 14
    public function createTransaction(): TransactionInterface
62
    {
63 14
        return new Transaction($this);
64
    }
65
66 549
    public function getQueryBuilder(): QueryBuilderInterface
67
    {
68 549
        if ($this->queryBuilder === null) {
69 549
            $this->queryBuilder = new QueryBuilder(
70 549
                $this->getQuoter(),
71 549
                $this->getSchema(),
72 549
            );
73
        }
74
75 549
        return $this->queryBuilder;
76
    }
77
78 610
    public function getQuoter(): QuoterInterface
79
    {
80 610
        if ($this->quoter === null) {
81 610
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
82
        }
83
84 610
        return $this->quoter;
85
    }
86
87 587
    public function getSchema(): SchemaInterface
88
    {
89 587
        if ($this->schema === null) {
90 587
            $this->schema = new Schema($this, $this->schemaCache);
91
        }
92
93 587
        return $this->schema;
94
    }
95
}
96