1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mysql\PDO; |
6
|
|
|
|
7
|
|
|
use PDO; |
8
|
|
|
use Yiisoft\Db\Driver\PDO\CommandPDOInterface; |
9
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDO; |
10
|
|
|
use Yiisoft\Db\Exception\Exception; |
11
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
12
|
|
|
use Yiisoft\Db\Mysql\Quoter; |
13
|
|
|
use Yiisoft\Db\Mysql\Schema; |
14
|
|
|
use Yiisoft\Db\Query\QueryBuilderInterface; |
15
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
16
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
17
|
|
|
use Yiisoft\Db\Transaction\TransactionInterface; |
18
|
|
|
|
19
|
|
|
use function constant; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Database connection class prefilled for MySQL Server. |
23
|
|
|
* The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php). |
24
|
|
|
*/ |
25
|
|
|
final class ConnectionPDOMysql extends ConnectionPDO |
26
|
|
|
{ |
27
|
183 |
|
public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface |
28
|
|
|
{ |
29
|
183 |
|
$command = new CommandPDOMysql($this, $this->queryCache); |
30
|
|
|
|
31
|
183 |
|
if ($sql !== null) { |
32
|
182 |
|
$command->setSql($sql); |
33
|
|
|
} |
34
|
|
|
|
35
|
183 |
|
if ($this->logger !== null) { |
36
|
183 |
|
$command->setLogger($this->logger); |
37
|
|
|
} |
38
|
|
|
|
39
|
183 |
|
if ($this->profiler !== null) { |
40
|
183 |
|
$command->setProfiler($this->profiler); |
41
|
|
|
} |
42
|
|
|
|
43
|
183 |
|
return $command->bindValues($params); |
44
|
|
|
} |
45
|
|
|
|
46
|
11 |
|
public function createTransaction(): TransactionInterface |
47
|
|
|
{ |
48
|
11 |
|
return new TransactionPDOMysql($this); |
49
|
|
|
} |
50
|
|
|
|
51
|
10 |
|
/** |
52
|
|
|
* @throws Exception|InvalidConfigException |
53
|
10 |
|
*/ |
54
|
|
|
public function getQueryBuilder(): QueryBuilderInterface |
55
|
|
|
{ |
56
|
|
|
if ($this->queryBuilder === null) { |
57
|
|
|
$this->queryBuilder = new QueryBuilderPDOMysql( |
58
|
|
|
$this->getQuoter(), |
59
|
354 |
|
$this->getSchema(), |
60
|
|
|
); |
61
|
354 |
|
} |
62
|
354 |
|
|
63
|
354 |
|
return $this->queryBuilder; |
|
|
|
|
64
|
354 |
|
} |
65
|
|
|
|
66
|
|
|
public function getQuoter(): QuoterInterface |
67
|
|
|
{ |
68
|
354 |
|
if ($this->quoter === null) { |
69
|
|
|
$this->quoter = new Quoter('`', '`', $this->getTablePrefix()); |
70
|
|
|
} |
71
|
371 |
|
|
72
|
|
|
return $this->quoter; |
|
|
|
|
73
|
371 |
|
} |
74
|
371 |
|
|
75
|
|
|
public function getSchema(): SchemaInterface |
76
|
|
|
{ |
77
|
371 |
|
if ($this->schema === null) { |
78
|
|
|
$this->schema = new Schema($this, $this->schemaCache); |
79
|
|
|
} |
80
|
380 |
|
|
81
|
|
|
return $this->schema; |
|
|
|
|
82
|
380 |
|
} |
83
|
380 |
|
|
84
|
|
|
/** |
85
|
|
|
* Initializes the DB connection. |
86
|
380 |
|
* |
87
|
|
|
* This method is invoked right after the DB connection is established. |
88
|
|
|
* |
89
|
|
|
* The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`. |
90
|
|
|
* |
91
|
|
|
* if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty. |
92
|
|
|
* |
93
|
|
|
* It then triggers an {@see EVENT_AFTER_OPEN} event. |
94
|
|
|
*/ |
95
|
|
|
protected function initConnection(): void |
96
|
|
|
{ |
97
|
|
|
if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) { |
98
|
|
|
$this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]); |
99
|
|
|
} |
100
|
195 |
|
|
101
|
|
|
$this->pdo = $this->driver->createConnection(); |
102
|
195 |
|
} |
103
|
|
|
} |
104
|
|
|
|