|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mysql; |
|
6
|
|
|
|
|
7
|
|
|
use PDO; |
|
8
|
|
|
use Yiisoft\Db\Cache\QueryCache; |
|
9
|
|
|
use Yiisoft\Db\Cache\SchemaCache; |
|
10
|
|
|
use Yiisoft\Db\Command\Command; |
|
11
|
|
|
use Yiisoft\Db\Connection\Connection as AbstractConnection; |
|
12
|
|
|
|
|
13
|
|
|
use function constant; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php). |
|
17
|
|
|
*/ |
|
18
|
|
|
final class Connection extends AbstractConnection |
|
19
|
|
|
{ |
|
20
|
|
|
private QueryCache $queryCache; |
|
21
|
|
|
private SchemaCache $schemaCache; |
|
22
|
|
|
|
|
23
|
419 |
|
public function __construct(string $dsn, QueryCache $queryCache, SchemaCache $schemaCache) |
|
24
|
|
|
{ |
|
25
|
419 |
|
$this->queryCache = $queryCache; |
|
26
|
419 |
|
$this->schemaCache = $schemaCache; |
|
27
|
|
|
|
|
28
|
419 |
|
parent::__construct($dsn, $queryCache); |
|
29
|
419 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Creates a command for execution. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string|null $sql the SQL statement to be executed |
|
35
|
|
|
* @param array $params the parameters to be bound to the SQL statement |
|
36
|
|
|
* |
|
37
|
|
|
* @return Command the DB command |
|
38
|
|
|
*/ |
|
39
|
174 |
|
public function createCommand(?string $sql = null, array $params = []): Command |
|
40
|
|
|
{ |
|
41
|
174 |
|
if ($sql !== null) { |
|
42
|
174 |
|
$sql = $this->quoteSql($sql); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
174 |
|
$command = new Command($this, $this->queryCache, $sql); |
|
46
|
|
|
|
|
47
|
174 |
|
if ($this->logger !== null) { |
|
48
|
174 |
|
$command->setLogger($this->logger); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
174 |
|
if ($this->profiler !== null) { |
|
52
|
174 |
|
$command->setProfiler($this->profiler); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
174 |
|
return $command->bindValues($params); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns the schema information for the database opened by this connection. |
|
60
|
|
|
* |
|
61
|
|
|
* @return Schema the schema information for the database opened by this connection. |
|
62
|
|
|
*/ |
|
63
|
334 |
|
public function getSchema(): Schema |
|
64
|
|
|
{ |
|
65
|
334 |
|
return new Schema($this, $this->schemaCache); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Creates the PDO instance. |
|
70
|
|
|
* |
|
71
|
|
|
* This method is called by {@see open} to establish a DB connection. The default implementation will create a PHP |
|
72
|
|
|
* PDO instance. You may override this method if the default PDO needs to be adapted for certain DBMS. |
|
73
|
|
|
* |
|
74
|
|
|
* @return PDO the pdo instance |
|
75
|
|
|
*/ |
|
76
|
196 |
|
protected function createPdoInstance(): PDO |
|
77
|
|
|
{ |
|
78
|
196 |
|
return new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Initializes the DB connection. |
|
83
|
|
|
* |
|
84
|
|
|
* This method is invoked right after the DB connection is established. |
|
85
|
|
|
* |
|
86
|
|
|
* The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`. |
|
87
|
|
|
* |
|
88
|
|
|
* if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty. |
|
89
|
|
|
* |
|
90
|
|
|
* It then triggers an {@see EVENT_AFTER_OPEN} event. |
|
91
|
|
|
*/ |
|
92
|
196 |
|
protected function initConnection(): void |
|
93
|
|
|
{ |
|
94
|
196 |
|
$pdo = $this->getPDO(); |
|
95
|
|
|
|
|
96
|
196 |
|
if ($pdo !== null) { |
|
97
|
196 |
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
98
|
|
|
|
|
99
|
196 |
|
if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) { |
|
100
|
1 |
|
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
196 |
|
$charset = $this->getCharset(); |
|
104
|
|
|
|
|
105
|
196 |
|
if ($charset !== null) { |
|
106
|
196 |
|
$pdo->exec('SET NAMES ' . $pdo->quote($charset)); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
196 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns the name of the DB driver. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string name of the DB driver |
|
115
|
|
|
*/ |
|
116
|
10 |
|
public function getDriverName(): string |
|
117
|
|
|
{ |
|
118
|
10 |
|
return 'mysql'; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|