1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver; |
6
|
|
|
|
7
|
|
|
use PDO; |
8
|
|
|
use function assert; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PDO implementation of the Connection interface. |
12
|
|
|
* |
13
|
|
|
* Used by all PDO-based drivers. |
14
|
|
|
*/ |
15
|
|
|
class PDOConnection implements Connection, ServerInfoAwareConnection |
16
|
|
|
{ |
17
|
|
|
/** @var PDO */ |
18
|
|
|
private $connection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array<int, mixed> $options |
22
|
|
|
* |
23
|
|
|
* @throws PDOException In case of an error. |
24
|
5025 |
|
*/ |
25
|
|
|
public function __construct(string $dsn, string $username = '', string $password = '', array $options = []) |
26
|
|
|
{ |
27
|
5025 |
|
try { |
28
|
5022 |
|
$this->connection = new PDO($dsn, $username, $password, $options); |
29
|
5022 |
|
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
30
|
4355 |
|
} catch (\PDOException $exception) { |
31
|
4355 |
|
throw new PDOException($exception); |
32
|
|
|
} |
33
|
5022 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
5141 |
|
public function exec(string $statement) : int |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
5141 |
|
return $this->connection->exec($statement); |
42
|
5093 |
|
} catch (\PDOException $exception) { |
43
|
5093 |
|
throw new PDOException($exception); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
4502 |
|
public function getServerVersion() : string |
51
|
|
|
{ |
52
|
4502 |
|
return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
4995 |
|
public function prepare(string $sql) : Statement |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
4995 |
|
return $this->createStatement( |
62
|
4861 |
|
$this->connection->prepare($sql) |
|
|
|
|
63
|
4861 |
|
); |
64
|
|
|
} catch (\PDOException $exception) { |
65
|
|
|
throw new PDOException($exception); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
5155 |
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
5155 |
|
public function query(string $sql) : ResultStatement |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
5155 |
|
$stmt = $this->connection->query($sql); |
76
|
5003 |
|
assert($stmt instanceof \PDOStatement); |
77
|
|
|
|
78
|
5003 |
|
return $this->createStatement($stmt); |
79
|
4942 |
|
} catch (\PDOException $exception) { |
80
|
4942 |
|
throw new PDOException($exception); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
4533 |
|
public function quote(string $input) : string |
88
|
|
|
{ |
89
|
4533 |
|
return $this->connection->quote($input); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
486 |
|
public function lastInsertId(?string $name = null) : string |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
486 |
|
if ($name === null) { |
99
|
145 |
|
return $this->connection->lastInsertId(); |
100
|
|
|
} |
101
|
|
|
|
102
|
341 |
|
return $this->connection->lastInsertId($name); |
103
|
|
|
} catch (\PDOException $exception) { |
104
|
|
|
throw new PDOException($exception); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
4681 |
|
public function requiresQueryForServerVersion() : bool |
112
|
|
|
{ |
113
|
4681 |
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Creates a wrapped statement |
118
|
|
|
*/ |
119
|
|
|
protected function createStatement(\PDOStatement $stmt) : PDOStatement |
120
|
|
|
{ |
121
|
|
|
return new PDOStatement($stmt); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
|
|
public function beginTransaction() : void |
128
|
|
|
{ |
129
|
|
|
$this->connection->beginTransaction(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
|
|
public function commit() : void |
136
|
|
|
{ |
137
|
|
|
$this->connection->commit(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritDoc} |
142
|
|
|
*/ |
143
|
|
|
public function rollBack() : void |
144
|
|
|
{ |
145
|
|
|
$this->connection->rollBack(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getWrappedConnection() : PDO |
149
|
|
|
{ |
150
|
|
|
return $this->connection; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|