|
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\AbstractConnectionPDO; |
|
10
|
|
|
use Yiisoft\Db\Driver\PDO\CommandPDOInterface; |
|
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 AbstractConnectionPDO |
|
22
|
|
|
{ |
|
23
|
183 |
|
public function close(): void |
|
24
|
|
|
{ |
|
25
|
183 |
|
if ($this->pdo !== null) { |
|
26
|
171 |
|
$this->logger?->log( |
|
27
|
171 |
|
LogLevel::DEBUG, |
|
28
|
171 |
|
'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__, |
|
29
|
171 |
|
); |
|
30
|
|
|
|
|
31
|
|
|
// Solution for close connections {@link https://stackoverflow.com/questions/18277233/pdo-closing-connection} |
|
32
|
|
|
try { |
|
33
|
171 |
|
$this->pdo->query('KILL CONNECTION_ID()'); |
|
34
|
171 |
|
} catch (Exception) { |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
171 |
|
$this->pdo = null; |
|
38
|
171 |
|
$this->transaction = null; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
270 |
|
public function createCommand(string $sql = null, array $params = []): CommandPDOInterface |
|
43
|
|
|
{ |
|
44
|
270 |
|
$command = new Command($this); |
|
45
|
|
|
|
|
46
|
270 |
|
if ($sql !== null) { |
|
47
|
244 |
|
$command->setSql($sql); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
270 |
|
if ($this->logger !== null) { |
|
51
|
2 |
|
$command->setLogger($this->logger); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
270 |
|
if ($this->profiler !== null) { |
|
55
|
3 |
|
$command->setProfiler($this->profiler); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
270 |
|
return $command->bindValues($params); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
14 |
|
public function createTransaction(): TransactionInterface |
|
62
|
|
|
{ |
|
63
|
14 |
|
return new Transaction($this); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
523 |
|
public function getQueryBuilder(): QueryBuilderInterface |
|
67
|
|
|
{ |
|
68
|
523 |
|
if ($this->queryBuilder === null) { |
|
69
|
523 |
|
$this->queryBuilder = new QueryBuilder( |
|
70
|
523 |
|
$this->getQuoter(), |
|
71
|
523 |
|
$this->getSchema(), |
|
72
|
523 |
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
523 |
|
return $this->queryBuilder; |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
577 |
|
public function getQuoter(): QuoterInterface |
|
79
|
|
|
{ |
|
80
|
577 |
|
if ($this->quoter === null) { |
|
81
|
577 |
|
$this->quoter = new Quoter('`', '`', $this->getTablePrefix()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
577 |
|
return $this->quoter; |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
562 |
|
public function getSchema(): SchemaInterface |
|
88
|
|
|
{ |
|
89
|
562 |
|
if ($this->schema === null) { |
|
90
|
562 |
|
$this->schema = new Schema($this, $this->schemaCache); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
562 |
|
return $this->schema; |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|