|
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 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
|
|
|
*/ |
|
25
|
942 |
|
public function __construct(string $dsn, string $username = '', string $password = '', array $options = []) |
|
26
|
|
|
{ |
|
27
|
|
|
try { |
|
28
|
942 |
|
$this->connection = new PDO($dsn, $username, $password, $options); |
|
29
|
904 |
|
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
30
|
69 |
|
} catch (\PDOException $exception) { |
|
31
|
69 |
|
throw new PDOException($exception); |
|
32
|
|
|
} |
|
33
|
904 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
3415 |
|
public function exec(string $statement) : int |
|
39
|
|
|
{ |
|
40
|
|
|
try { |
|
41
|
3415 |
|
return $this->connection->exec($statement); |
|
42
|
1708 |
|
} catch (\PDOException $exception) { |
|
43
|
1708 |
|
throw new PDOException($exception); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
325 |
|
public function getServerVersion() : string |
|
51
|
|
|
{ |
|
52
|
325 |
|
return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
3481 |
|
public function prepare(string $sql) : Statement |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
3481 |
|
return $this->createStatement( |
|
62
|
3481 |
|
$this->connection->prepare($sql) |
|
|
|
|
|
|
63
|
|
|
); |
|
64
|
38 |
|
} catch (\PDOException $exception) { |
|
65
|
38 |
|
throw new PDOException($exception); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
4074 |
|
public function query(string $sql) : ResultStatement |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
4074 |
|
$stmt = $this->connection->query($sql); |
|
76
|
3955 |
|
assert($stmt instanceof \PDOStatement); |
|
77
|
|
|
|
|
78
|
3955 |
|
return $this->createStatement($stmt); |
|
79
|
119 |
|
} catch (\PDOException $exception) { |
|
80
|
119 |
|
throw new PDOException($exception); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
140 |
|
public function quote(string $input) : string |
|
88
|
|
|
{ |
|
89
|
140 |
|
return $this->connection->quote($input); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
77 |
|
public function lastInsertId(?string $name = null) : string |
|
96
|
|
|
{ |
|
97
|
|
|
try { |
|
98
|
77 |
|
if ($name === null) { |
|
99
|
22 |
|
return $this->connection->lastInsertId(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
55 |
|
return $this->connection->lastInsertId($name); |
|
103
|
|
|
} catch (\PDOException $exception) { |
|
104
|
|
|
throw new PDOException($exception); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Creates a wrapped statement |
|
110
|
|
|
*/ |
|
111
|
335 |
|
protected function createStatement(\PDOStatement $stmt) : PDOStatement |
|
112
|
|
|
{ |
|
113
|
335 |
|
return new PDOStatement($stmt); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritDoc} |
|
118
|
|
|
*/ |
|
119
|
5712 |
|
public function beginTransaction() : void |
|
120
|
|
|
{ |
|
121
|
5712 |
|
$this->connection->beginTransaction(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritDoc} |
|
126
|
|
|
*/ |
|
127
|
279 |
|
public function commit() : void |
|
128
|
|
|
{ |
|
129
|
279 |
|
$this->connection->commit(); |
|
130
|
279 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritDoc} |
|
134
|
|
|
*/ |
|
135
|
110 |
|
public function rollBack() : void |
|
136
|
|
|
{ |
|
137
|
110 |
|
$this->connection->rollBack(); |
|
138
|
110 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function getWrappedConnection() : PDO |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->connection; |
|
143
|
185 |
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|