|
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 string $dsn |
|
22
|
|
|
* @param string|null $user |
|
23
|
|
|
* @param string|null $password |
|
24
|
|
|
* @param mixed[]|null $options |
|
25
|
|
|
* |
|
26
|
4638 |
|
* @throws PDOException In case of an error. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($dsn, $user = null, $password = null, ?array $options = null) |
|
29
|
4638 |
|
{ |
|
30
|
4638 |
|
try { |
|
31
|
4035 |
|
$this->connection = new PDO($dsn, (string) $user, (string) $password, (array) $options); |
|
32
|
4035 |
|
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
33
|
|
|
} catch (\PDOException $exception) { |
|
34
|
4638 |
|
throw new PDOException($exception); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
4626 |
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function exec(string $statement) : int |
|
42
|
4626 |
|
{ |
|
43
|
4624 |
|
try { |
|
44
|
4624 |
|
return $this->connection->exec($statement); |
|
45
|
|
|
} catch (\PDOException $exception) { |
|
46
|
|
|
throw new PDOException($exception); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
4156 |
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
4156 |
|
public function getServerVersion() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
4529 |
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function prepare(string $sql) : Statement |
|
62
|
4529 |
|
{ |
|
63
|
4529 |
|
try { |
|
64
|
|
|
return $this->createStatement( |
|
65
|
4527 |
|
$this->connection->prepare($sql) |
|
|
|
|
|
|
66
|
4527 |
|
); |
|
67
|
|
|
} catch (\PDOException $exception) { |
|
68
|
|
|
throw new PDOException($exception); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
4604 |
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function query(string $sql) : ResultStatement |
|
76
|
4604 |
|
{ |
|
77
|
4466 |
|
try { |
|
78
|
|
|
$stmt = $this->connection->query($sql); |
|
79
|
4466 |
|
assert($stmt instanceof \PDOStatement); |
|
80
|
4600 |
|
|
|
81
|
4600 |
|
return $this->createStatement($stmt); |
|
82
|
|
|
} catch (\PDOException $exception) { |
|
83
|
|
|
throw new PDOException($exception); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
4230 |
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
4230 |
|
public function quote(string $input) : string |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->connection->quote($input); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
466 |
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function lastInsertId($name = null) |
|
99
|
466 |
|
{ |
|
100
|
154 |
|
try { |
|
101
|
|
|
if ($name === null) { |
|
102
|
|
|
return $this->connection->lastInsertId(); |
|
103
|
312 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->connection->lastInsertId($name); |
|
106
|
|
|
} catch (\PDOException $exception) { |
|
107
|
|
|
throw new PDOException($exception); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
4327 |
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
4327 |
|
public function requiresQueryForServerVersion() |
|
115
|
|
|
{ |
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
4234 |
|
* Creates a wrapped statement |
|
121
|
|
|
*/ |
|
122
|
4234 |
|
protected function createStatement(\PDOStatement $stmt) : PDOStatement |
|
123
|
|
|
{ |
|
124
|
|
|
return new PDOStatement($stmt); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
4337 |
|
* {@inheritDoc} |
|
129
|
|
|
*/ |
|
130
|
4337 |
|
public function beginTransaction() : void |
|
131
|
4337 |
|
{ |
|
132
|
|
|
$this->connection->beginTransaction(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
4252 |
|
* {@inheritDoc} |
|
137
|
|
|
*/ |
|
138
|
4252 |
|
public function commit() : void |
|
139
|
4252 |
|
{ |
|
140
|
|
|
$this->connection->commit(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
4282 |
|
* {@inheritDoc} |
|
145
|
|
|
*/ |
|
146
|
4282 |
|
public function rollBack() : void |
|
147
|
4282 |
|
{ |
|
148
|
|
|
$this->connection->rollBack(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function getWrappedConnection() : PDO |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->connection; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|