| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Doctrine\DBAL\Driver\OCI8;
|
|
|
4 |
|
|
|
5 |
use Doctrine\DBAL\Driver\OCI8\Exception\Error;
|
|
|
6 |
use Doctrine\DBAL\Driver\OCI8\Exception\UnknownParameterIndex;
|
|
|
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 |
|
|
|
12 |
use function func_num_args;
|
|
|
13 |
use function is_int;
|
|
|
14 |
use function oci_bind_by_name;
|
|
|
15 |
use function oci_execute;
|
|
|
16 |
use function oci_new_descriptor;
|
|
|
17 |
|
|
|
18 |
use const OCI_B_BIN;
|
|
|
19 |
use const OCI_B_BLOB;
|
|
|
20 |
use const OCI_COMMIT_ON_SUCCESS;
|
|
|
21 |
use const OCI_D_LOB;
|
|
|
22 |
use const OCI_NO_AUTO_COMMIT;
|
|
|
23 |
use const OCI_TEMP_BLOB;
|
|
|
24 |
use const SQLT_CHR;
|
|
|
25 |
|
|
|
26 |
final class Statement implements StatementInterface
|
|
|
27 |
{
|
|
|
28 |
/** @var resource */
|
|
|
29 |
private $connection;
|
|
|
30 |
|
|
|
31 |
/** @var resource */
|
|
|
32 |
private $statement;
|
|
|
33 |
|
|
|
34 |
/** @var array<int,string> */
|
|
|
35 |
private array $parameterMap;
|
|
|
36 |
|
|
|
37 |
private ExecutionMode $executionMode;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* @internal The statement can be only instantiated by its driver connection.
|
|
|
41 |
*
|
|
|
42 |
* @param resource $connection
|
|
|
43 |
* @param resource $statement
|
|
|
44 |
* @param array<int,string> $parameterMap
|
|
|
45 |
*/
|
|
|
46 |
public function __construct($connection, $statement, array $parameterMap, ExecutionMode $executionMode)
|
|
|
47 |
{
|
|
|
48 |
$this->connection = $connection;
|
|
|
49 |
$this->statement = $statement;
|
|
|
50 |
$this->parameterMap = $parameterMap;
|
|
|
51 |
$this->executionMode = $executionMode;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* {@inheritdoc}
|
|
|
56 |
*/
|
|
|
57 |
public function bindValue($param, $value, $type = ParameterType::STRING): bool
|
|
|
58 |
{
|
|
|
59 |
if (func_num_args() < 3) {
|
|
|
60 |
Deprecation::trigger(
|
|
|
61 |
'doctrine/dbal',
|
|
|
62 |
'https://github.com/doctrine/dbal/pull/5558',
|
|
|
63 |
'Not passing $type to Statement::bindValue() is deprecated.'
|
|
|
64 |
. ' Pass the type corresponding to the parameter being bound.',
|
|
|
65 |
);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
return $this->bindParam($param, $value, $type);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* {@inheritdoc}
|
|
|
73 |
*
|
|
|
74 |
* @deprecated Use {@see bindValue()} instead.
|
|
|
75 |
*/
|
|
|
76 |
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
|
|
|
77 |
{
|
|
|
78 |
Deprecation::trigger(
|
|
|
79 |
'doctrine/dbal',
|
|
|
80 |
'https://github.com/doctrine/dbal/pull/5563',
|
|
|
81 |
'%s is deprecated. Use bindValue() instead.',
|
|
|
82 |
__METHOD__,
|
|
|
83 |
);
|
|
|
84 |
|
|
|
85 |
if (func_num_args() < 3) {
|
|
|
86 |
Deprecation::trigger(
|
|
|
87 |
'doctrine/dbal',
|
|
|
88 |
'https://github.com/doctrine/dbal/pull/5558',
|
|
|
89 |
'Not passing $type to Statement::bindParam() is deprecated.'
|
|
|
90 |
. ' Pass the type corresponding to the parameter being bound.',
|
|
|
91 |
);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
if (is_int($param)) {
|
|
|
95 |
if (! isset($this->parameterMap[$param])) {
|
|
|
96 |
throw UnknownParameterIndex::new($param);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
$param = $this->parameterMap[$param];
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
if ($type === ParameterType::LARGE_OBJECT) {
|
|
|
103 |
if ($variable !== null) {
|
|
|
104 |
$lob = oci_new_descriptor($this->connection, OCI_D_LOB);
|
|
|
105 |
$lob->writeTemporary($variable, OCI_TEMP_BLOB);
|
|
|
106 |
|
|
|
107 |
$variable =& $lob;
|
|
|
108 |
} else {
|
|
|
109 |
$type = ParameterType::STRING;
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
return oci_bind_by_name(
|
|
|
114 |
$this->statement,
|
|
|
115 |
$param,
|
|
|
116 |
$variable,
|
|
|
117 |
$length ?? -1,
|
|
|
118 |
$this->convertParameterType($type),
|
|
|
119 |
);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Converts DBAL parameter type to oci8 parameter type
|
|
|
124 |
*/
|
|
|
125 |
private function convertParameterType(int $type): int
|
|
|
126 |
{
|
|
|
127 |
switch ($type) {
|
|
|
128 |
case ParameterType::BINARY:
|
|
|
129 |
return OCI_B_BIN;
|
|
|
130 |
|
|
|
131 |
case ParameterType::LARGE_OBJECT:
|
|
|
132 |
return OCI_B_BLOB;
|
|
|
133 |
|
|
|
134 |
default:
|
|
|
135 |
return SQLT_CHR;
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* {@inheritdoc}
|
|
|
141 |
*/
|
|
|
142 |
public function execute($params = null): ResultInterface
|
|
|
143 |
{
|
|
|
144 |
if ($params !== null) {
|
|
|
145 |
Deprecation::trigger(
|
|
|
146 |
'doctrine/dbal',
|
|
|
147 |
'https://github.com/doctrine/dbal/pull/5556',
|
|
|
148 |
'Passing $params to Statement::execute() is deprecated. Bind parameters using'
|
|
|
149 |
. ' Statement::bindParam() or Statement::bindValue() instead.',
|
|
|
150 |
);
|
|
|
151 |
|
|
|
152 |
foreach ($params as $key => $val) {
|
|
|
153 |
if (is_int($key)) {
|
|
|
154 |
$this->bindValue($key + 1, $val, ParameterType::STRING);
|
|
|
155 |
} else {
|
|
|
156 |
$this->bindValue($key, $val, ParameterType::STRING);
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
if ($this->executionMode->isAutoCommitEnabled()) {
|
|
|
162 |
$mode = OCI_COMMIT_ON_SUCCESS;
|
|
|
163 |
} else {
|
|
|
164 |
$mode = OCI_NO_AUTO_COMMIT;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
$ret = @oci_execute($this->statement, $mode);
|
|
|
168 |
if (! $ret) {
|
|
|
169 |
throw Error::new($this->statement);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
return new Result($this->statement);
|
|
|
173 |
}
|
|
|
174 |
}
|