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