-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathPDOConnection.php
150 lines (132 loc) · 3.64 KB
/
PDOConnection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\PDO\Exception;
use Doctrine\DBAL\Driver\PDO\Statement;
use Doctrine\DBAL\ParameterType;
use Doctrine\Deprecations\Deprecation;
use PDO;
use PDOException;
use PDOStatement;
use ReturnTypeWillChange;
use function assert;
/**
* PDO implementation of the Connection interface.
* Used by all PDO-based drivers.
*
* @deprecated Use {@link Connection} instead
*/
class PDOConnection extends PDO implements ConnectionInterface, ServerInfoAwareConnection
{
use PDOQueryImplementation;
/**
* @internal The connection can be only instantiated by its driver.
*
* @param string $dsn
* @param string|null $user
* @param string|null $password
* @param mixed[]|null $options
*
* @throws PDOException In case of an error.
*/
public function __construct($dsn, $user = null, $password = null, ?array $options = null)
{
try {
parent::__construct($dsn, (string) $user, (string) $password, (array) $options);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [Statement::class, []]);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function exec($sql)
{
try {
$result = parent::exec($sql);
assert($result !== false);
return $result;
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}
/**
* {@inheritdoc}
*/
public function getServerVersion()
{
return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
}
/**
* @param string $sql
* @param array<int, int> $driverOptions
*
* @return PDOStatement
*/
#[ReturnTypeWillChange]
public function prepare($sql, $driverOptions = [])
{
try {
$statement = parent::prepare($sql, $driverOptions);
assert($statement instanceof PDOStatement);
return $statement;
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function quote($value, $type = ParameterType::STRING)
{
return parent::quote($value, $type);
}
/**
* {@inheritdoc}
*
* @param string|null $name
*
* @return string|int|false
*/
#[ReturnTypeWillChange]
public function lastInsertId($name = null)
{
try {
if ($name === null) {
return parent::lastInsertId();
}
return parent::lastInsertId($name);
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}
/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4114',
'ServerInfoAwareConnection::requiresQueryForServerVersion() is deprecated and removed in DBAL 3.'
);
return false;
}
/**
* @param mixed ...$args
*/
private function doQuery(...$args): PDOStatement
{
try {
$stmt = parent::query(...$args);
} catch (PDOException $exception) {
throw Exception::new($exception);
}
assert($stmt instanceof PDOStatement);
return $stmt;
}
}