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