| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Doctrine\DBAL\Driver\PDO;
|
|
|
4 |
|
|
|
5 |
use Doctrine\DBAL\Driver\Exception as ExceptionInterface;
|
|
|
6 |
use Doctrine\DBAL\Driver\Exception\UnknownParameterType;
|
|
|
7 |
use Doctrine\DBAL\Driver\Result as ResultInterface;
|
|
|
8 |
use Doctrine\DBAL\Driver\Statement as StatementInterface;
|
|
|
9 |
use Doctrine\DBAL\ParameterType;
|
|
|
10 |
use Doctrine\Deprecations\Deprecation;
|
|
|
11 |
use PDO;
|
|
|
12 |
use PDOException;
|
|
|
13 |
use PDOStatement;
|
|
|
14 |
|
|
|
15 |
use function array_slice;
|
|
|
16 |
use function func_get_args;
|
|
|
17 |
use function func_num_args;
|
|
|
18 |
|
|
|
19 |
final class Statement implements StatementInterface
|
|
|
20 |
{
|
|
|
21 |
private const PARAM_TYPE_MAP = [
|
|
|
22 |
ParameterType::NULL => PDO::PARAM_NULL,
|
|
|
23 |
ParameterType::INTEGER => PDO::PARAM_INT,
|
|
|
24 |
ParameterType::STRING => PDO::PARAM_STR,
|
|
|
25 |
ParameterType::ASCII => PDO::PARAM_STR,
|
|
|
26 |
ParameterType::BINARY => PDO::PARAM_LOB,
|
|
|
27 |
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
|
|
|
28 |
ParameterType::BOOLEAN => PDO::PARAM_BOOL,
|
|
|
29 |
];
|
|
|
30 |
|
|
|
31 |
private PDOStatement $stmt;
|
|
|
32 |
|
|
|
33 |
/** @internal The statement can be only instantiated by its driver connection. */
|
|
|
34 |
public function __construct(PDOStatement $stmt)
|
|
|
35 |
{
|
|
|
36 |
$this->stmt = $stmt;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* {@inheritdoc}
|
|
|
41 |
*/
|
|
|
42 |
public function bindValue($param, $value, $type = ParameterType::STRING)
|
|
|
43 |
{
|
|
|
44 |
if (func_num_args() < 3) {
|
|
|
45 |
Deprecation::trigger(
|
|
|
46 |
'doctrine/dbal',
|
|
|
47 |
'https://github.com/doctrine/dbal/pull/5558',
|
|
|
48 |
'Not passing $type to Statement::bindValue() is deprecated.'
|
|
|
49 |
. ' Pass the type corresponding to the parameter being bound.',
|
|
|
50 |
);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$type = $this->convertParamType($type);
|
|
|
54 |
|
|
|
55 |
try {
|
|
|
56 |
return $this->stmt->bindValue($param, $value, $type);
|
|
|
57 |
} catch (PDOException $exception) {
|
|
|
58 |
throw Exception::new($exception);
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* {@inheritDoc}
|
|
|
64 |
*
|
|
|
65 |
* @deprecated Use {@see bindValue()} instead.
|
|
|
66 |
*
|
|
|
67 |
* @param mixed $param
|
|
|
68 |
* @param mixed $variable
|
|
|
69 |
* @param int $type
|
|
|
70 |
* @param int|null $length
|
|
|
71 |
* @param mixed $driverOptions The usage of the argument is deprecated.
|
|
|
72 |
*/
|
|
|
73 |
public function bindParam(
|
|
|
74 |
$param,
|
|
|
75 |
&$variable,
|
|
|
76 |
$type = ParameterType::STRING,
|
|
|
77 |
$length = null,
|
|
|
78 |
$driverOptions = null
|
|
|
79 |
): bool {
|
|
|
80 |
Deprecation::trigger(
|
|
|
81 |
'doctrine/dbal',
|
|
|
82 |
'https://github.com/doctrine/dbal/pull/5563',
|
|
|
83 |
'%s is deprecated. Use bindValue() instead.',
|
|
|
84 |
__METHOD__,
|
|
|
85 |
);
|
|
|
86 |
|
|
|
87 |
if (func_num_args() < 3) {
|
|
|
88 |
Deprecation::trigger(
|
|
|
89 |
'doctrine/dbal',
|
|
|
90 |
'https://github.com/doctrine/dbal/pull/5558',
|
|
|
91 |
'Not passing $type to Statement::bindParam() is deprecated.'
|
|
|
92 |
. ' Pass the type corresponding to the parameter being bound.',
|
|
|
93 |
);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
if (func_num_args() > 4) {
|
|
|
97 |
Deprecation::triggerIfCalledFromOutside(
|
|
|
98 |
'doctrine/dbal',
|
|
|
99 |
'https://github.com/doctrine/dbal/issues/4533',
|
|
|
100 |
'The $driverOptions argument of Statement::bindParam() is deprecated.',
|
|
|
101 |
);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
$type = $this->convertParamType($type);
|
|
|
105 |
|
|
|
106 |
try {
|
|
|
107 |
return $this->stmt->bindParam(
|
|
|
108 |
$param,
|
|
|
109 |
$variable,
|
|
|
110 |
$type,
|
|
|
111 |
$length ?? 0,
|
|
|
112 |
...array_slice(func_get_args(), 4),
|
|
|
113 |
);
|
|
|
114 |
} catch (PDOException $exception) {
|
|
|
115 |
throw Exception::new($exception);
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* {@inheritdoc}
|
|
|
121 |
*/
|
|
|
122 |
public function execute($params = null): ResultInterface
|
|
|
123 |
{
|
|
|
124 |
if ($params !== null) {
|
|
|
125 |
Deprecation::trigger(
|
|
|
126 |
'doctrine/dbal',
|
|
|
127 |
'https://github.com/doctrine/dbal/pull/5556',
|
|
|
128 |
'Passing $params to Statement::execute() is deprecated. Bind parameters using'
|
|
|
129 |
. ' Statement::bindParam() or Statement::bindValue() instead.',
|
|
|
130 |
);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
try {
|
|
|
134 |
$this->stmt->execute($params);
|
|
|
135 |
} catch (PDOException $exception) {
|
|
|
136 |
throw Exception::new($exception);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
return new Result($this->stmt);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Converts DBAL parameter type to PDO parameter type
|
|
|
144 |
*
|
|
|
145 |
* @param int $type Parameter type
|
|
|
146 |
*
|
|
|
147 |
* @throws ExceptionInterface
|
|
|
148 |
*/
|
|
|
149 |
private function convertParamType(int $type): int
|
|
|
150 |
{
|
|
|
151 |
if (! isset(self::PARAM_TYPE_MAP[$type])) {
|
|
|
152 |
throw UnknownParameterType::new($type);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
return self::PARAM_TYPE_MAP[$type];
|
|
|
156 |
}
|
|
|
157 |
}
|