/
Statement.php
251 lines (221 loc) · 7.18 KB
/
Statement.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace Doctrine\DBAL;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;
use function func_num_args;
use function is_string;
/**
* A database abstraction-level statement that implements support for logging, DBAL mapping types, etc.
*/
class Statement
{
/**
* The SQL statement.
*
* @var string
*/
protected $sql;
/**
* The bound parameters.
*
* @var mixed[]
*/
protected $params = [];
/**
* The parameter types.
*
* @var int[]|string[]
*/
protected $types = [];
/**
* The underlying driver statement.
*
* @var Driver\Statement
*/
protected $stmt;
/**
* The underlying database platform.
*
* @var AbstractPlatform
*/
protected $platform;
/**
* The connection this statement is bound to and executed on.
*
* @var Connection
*/
protected $conn;
/**
* Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
*
* @internal The statement can be only instantiated by {@see Connection}.
*
* @param Connection $conn The connection for handling statement errors.
* @param Driver\Statement $statement The underlying driver-level statement.
* @param string $sql The SQL of the statement.
*
* @throws Exception
*/
public function __construct(Connection $conn, Driver\Statement $statement, string $sql)
{
$this->conn = $conn;
$this->stmt = $statement;
$this->sql = $sql;
$this->platform = $conn->getDatabasePlatform();
}
/**
* Binds a parameter value to the statement.
*
* The value can optionally be bound with a DBAL mapping type.
* If bound with a DBAL mapping type, the binding type is derived from the mapping
* type and the value undergoes the conversion routines of the mapping type before
* being bound.
*
* @param string|int $param The name or position of the parameter.
* @param mixed $value The value of the parameter.
* @param mixed $type Either a PDO binding type or a DBAL mapping type name or instance.
*
* @return bool TRUE on success, FALSE on failure.
*
* @throws Exception
*/
public function bindValue($param, $value, $type = ParameterType::STRING)
{
$this->params[$param] = $value;
$this->types[$param] = $type;
$bindingType = ParameterType::STRING;
if ($type !== null) {
if (is_string($type)) {
$type = Type::getType($type);
}
$bindingType = $type;
if ($type instanceof Type) {
$value = $type->convertToDatabaseValue($value, $this->platform);
$bindingType = $type->getBindingType();
}
}
try {
return $this->stmt->bindValue($param, $value, $bindingType);
} catch (Driver\Exception $e) {
throw $this->conn->convertException($e);
}
}
/**
* Binds a parameter to a value by reference.
*
* Binding a parameter by reference does not support DBAL mapping types.
*
* @param string|int $param The name or position of the parameter.
* @param mixed $variable The reference to the variable to bind.
* @param int $type The binding type.
* @param int|null $length Must be specified when using an OUT bind
* so that PHP allocates enough memory to hold the returned value.
*
* @return bool TRUE on success, FALSE on failure.
*
* @throws Exception
*/
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null)
{
$this->params[$param] = $variable;
$this->types[$param] = $type;
try {
if (func_num_args() > 3) {
return $this->stmt->bindParam($param, $variable, $type, $length);
}
return $this->stmt->bindParam($param, $variable, $type);
} catch (Driver\Exception $e) {
throw $this->conn->convertException($e);
}
}
/**
* Executes the statement with the currently bound parameters.
*
* @deprecated Statement::execute() is deprecated, use Statement::executeQuery() or executeStatement() instead
*
* @param mixed[]|null $params
*
* @throws Exception
*/
public function execute($params = null): Result
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4580',
'Statement::execute() is deprecated, use Statement::executeQuery() or Statement::executeStatement() instead'
);
if ($params !== null) {
$this->params = $params;
}
$logger = $this->conn->getConfiguration()->getSQLLogger();
if ($logger !== null) {
$logger->startQuery($this->sql, $this->params, $this->types);
}
try {
return new Result(
$this->stmt->execute($params),
$this->conn
);
} catch (Driver\Exception $ex) {
throw $this->conn->convertExceptionDuringQuery($ex, $this->sql, $this->params, $this->types);
} finally {
if ($logger !== null) {
$logger->stopQuery();
}
}
}
/**
* Executes the statement with the currently bound parameters and return result.
*
* @param mixed[] $params
*
* @throws Exception
*/
public function executeQuery(array $params = []): Result
{
if (func_num_args() > 0) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5556',
'Passing $params to Statement::executeQuery() is deprecated. Bind parameters using'
. ' Statement::bindParam() or Statement::bindValue() instead.'
);
}
if ($params === []) {
$params = null; // Workaround as long execute() exists and used internally.
}
return $this->execute($params);
}
/**
* Executes the statement with the currently bound parameters and return affected rows.
*
* @param mixed[] $params
*
* @throws Exception
*/
public function executeStatement(array $params = []): int
{
if (func_num_args() > 0) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5556',
'Passing $params to Statement::executeStatement() is deprecated. Bind parameters using'
. ' Statement::bindParam() or Statement::bindValue() instead.'
);
}
if ($params === []) {
$params = null; // Workaround as long execute() exists and used internally.
}
return $this->execute($params)->rowCount();
}
/**
* Gets the wrapped driver statement.
*
* @return Driver\Statement
*/
public function getWrappedStatement()
{
return $this->stmt;
}
}