|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver\SQLAnywhere; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Driver\DriverException; |
|
8
|
|
|
use Doctrine\DBAL\Driver\FetchUtils; |
|
9
|
|
|
use Doctrine\DBAL\Driver\Statement; |
|
10
|
|
|
use Doctrine\DBAL\Exception\GetVariableType; |
|
11
|
|
|
use Doctrine\DBAL\ParameterType; |
|
12
|
|
|
use Traversable; |
|
13
|
|
|
use function array_key_exists; |
|
14
|
|
|
use function assert; |
|
15
|
|
|
use function is_int; |
|
16
|
|
|
use function is_resource; |
|
17
|
|
|
use function sasql_fetch_assoc; |
|
|
|
|
|
|
18
|
|
|
use function sasql_fetch_row; |
|
|
|
|
|
|
19
|
|
|
use function sasql_prepare; |
|
|
|
|
|
|
20
|
|
|
use function sasql_stmt_affected_rows; |
|
|
|
|
|
|
21
|
|
|
use function sasql_stmt_bind_param_ex; |
|
|
|
|
|
|
22
|
|
|
use function sasql_stmt_execute; |
|
|
|
|
|
|
23
|
|
|
use function sasql_stmt_field_count; |
|
|
|
|
|
|
24
|
|
|
use function sasql_stmt_reset; |
|
|
|
|
|
|
25
|
|
|
use function sasql_stmt_result_metadata; |
|
|
|
|
|
|
26
|
|
|
use function sprintf; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* SAP SQL Anywhere implementation of the Statement interface. |
|
30
|
|
|
*/ |
|
31
|
|
|
final class SQLAnywhereStatement implements Statement |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var resource The connection resource. */ |
|
34
|
|
|
private $conn; |
|
35
|
|
|
|
|
36
|
|
|
/** @var resource|null The result set resource to fetch. */ |
|
37
|
|
|
private $result; |
|
38
|
|
|
|
|
39
|
|
|
/** @var resource The prepared SQL statement to execute. */ |
|
40
|
|
|
private $stmt; |
|
41
|
|
|
|
|
42
|
|
|
/** @var mixed[] The references to bound parameter values. */ |
|
43
|
|
|
private $boundValues = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Prepares given statement for given connection. |
|
47
|
|
|
* |
|
48
|
|
|
* @param resource $conn The connection resource to use. |
|
49
|
|
|
* @param string $sql The SQL statement to prepare. |
|
50
|
|
|
* |
|
51
|
|
|
* @throws SQLAnywhereException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct($conn, string $sql) |
|
54
|
|
|
{ |
|
55
|
|
|
if (! is_resource($conn)) { |
|
56
|
|
|
throw new SQLAnywhereException(sprintf( |
|
57
|
|
|
'Invalid SQL Anywhere connection resource, %s given.', |
|
58
|
|
|
(new GetVariableType())->__invoke($conn) |
|
59
|
|
|
)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->conn = $conn; |
|
63
|
|
|
$this->stmt = sasql_prepare($conn, $sql); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
if (! is_resource($this->stmt)) { |
|
66
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($conn); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
* |
|
73
|
|
|
* @throws SQLAnywhereException |
|
74
|
|
|
*/ |
|
75
|
|
|
public function bindParam($param, &$variable, int $type = ParameterType::STRING, ?int $length = null) : void |
|
76
|
|
|
{ |
|
77
|
|
|
assert(is_int($param)); |
|
78
|
|
|
|
|
79
|
|
|
switch ($type) { |
|
80
|
|
|
case ParameterType::INTEGER: |
|
81
|
|
|
case ParameterType::BOOLEAN: |
|
82
|
|
|
$type = 'i'; |
|
83
|
|
|
break; |
|
84
|
|
|
|
|
85
|
|
|
case ParameterType::LARGE_OBJECT: |
|
86
|
|
|
$type = 'b'; |
|
87
|
|
|
break; |
|
88
|
|
|
|
|
89
|
|
|
case ParameterType::NULL: |
|
90
|
|
|
case ParameterType::STRING: |
|
91
|
|
|
case ParameterType::BINARY: |
|
92
|
|
|
$type = 's'; |
|
93
|
|
|
break; |
|
94
|
|
|
|
|
95
|
|
|
default: |
|
96
|
|
|
throw new SQLAnywhereException(sprintf('Unknown type %d.', $type)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->boundValues[$param] =& $variable; |
|
100
|
|
|
|
|
101
|
|
|
if (! sasql_stmt_bind_param_ex($this->stmt, $param - 1, $variable, $type, $variable === null)) { |
|
|
|
|
|
|
102
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function bindValue($param, $value, int $type = ParameterType::STRING) : void |
|
110
|
|
|
{ |
|
111
|
|
|
$this->bindParam($param, $value, $type); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function closeCursor() : void |
|
115
|
|
|
{ |
|
116
|
|
|
sasql_stmt_reset($this->stmt); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function columnCount() : int |
|
120
|
|
|
{ |
|
121
|
|
|
return sasql_stmt_field_count($this->stmt); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
* |
|
127
|
|
|
* @throws SQLAnywhereException |
|
128
|
|
|
*/ |
|
129
|
|
|
public function execute(?array $params = null) : void |
|
130
|
|
|
{ |
|
131
|
|
|
if ($params !== null) { |
|
132
|
|
|
$hasZeroIndex = array_key_exists(0, $params); |
|
133
|
|
|
|
|
134
|
|
|
foreach ($params as $key => $val) { |
|
135
|
|
|
if ($hasZeroIndex && is_int($key)) { |
|
136
|
|
|
$this->bindValue($key + 1, $val); |
|
137
|
|
|
} else { |
|
138
|
|
|
$this->bindValue($key, $val); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if (! sasql_stmt_execute($this->stmt)) { |
|
|
|
|
|
|
144
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$this->result = sasql_stmt_result_metadata($this->stmt); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* {@inheritDoc} |
|
152
|
|
|
*/ |
|
153
|
|
|
public function fetchNumeric() |
|
154
|
|
|
{ |
|
155
|
|
|
if (! is_resource($this->result)) { |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return sasql_fetch_row($this->result); |
|
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
|
|
public function fetchAssociative() |
|
166
|
|
|
{ |
|
167
|
|
|
if (! is_resource($this->result)) { |
|
168
|
|
|
return false; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return sasql_fetch_assoc($this->result); |
|
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* {@inheritdoc} |
|
176
|
|
|
* |
|
177
|
|
|
* @throws DriverException |
|
178
|
|
|
*/ |
|
179
|
|
|
public function fetchOne() |
|
180
|
|
|
{ |
|
181
|
|
|
return FetchUtils::fetchOneFromNumeric($this); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return array<int,array<int,mixed>> |
|
186
|
|
|
* |
|
187
|
|
|
* @throws DriverException |
|
188
|
|
|
*/ |
|
189
|
|
|
public function fetchAllNumeric() : array |
|
190
|
|
|
{ |
|
191
|
|
|
return FetchUtils::fetchAllNumericByOne($this); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return array<int,array<string,mixed>> |
|
196
|
|
|
* |
|
197
|
|
|
* @throws DriverException |
|
198
|
|
|
*/ |
|
199
|
|
|
public function fetchAllAssociative() : array |
|
200
|
|
|
{ |
|
201
|
|
|
return FetchUtils::fetchAllAssociativeByOne($this); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return array<int,mixed> |
|
206
|
|
|
* |
|
207
|
|
|
* @throws DriverException |
|
208
|
|
|
*/ |
|
209
|
|
|
public function fetchColumn() : array |
|
210
|
|
|
{ |
|
211
|
|
|
return FetchUtils::fetchColumnByOne($this); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @return Traversable<int,array<int,mixed>> |
|
216
|
|
|
* |
|
217
|
|
|
* @throws DriverException |
|
218
|
|
|
*/ |
|
219
|
|
|
public function iterateNumeric() : Traversable |
|
220
|
|
|
{ |
|
221
|
|
|
return FetchUtils::iterateNumericByOne($this); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @return Traversable<int,array<string,mixed>> |
|
226
|
|
|
* |
|
227
|
|
|
* @throws DriverException |
|
228
|
|
|
*/ |
|
229
|
|
|
public function iterateAssociative() : Traversable |
|
230
|
|
|
{ |
|
231
|
|
|
return FetchUtils::iterateAssociativeByOne($this); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @return Traversable<int,mixed> |
|
236
|
|
|
* |
|
237
|
|
|
* @throws DriverException |
|
238
|
|
|
*/ |
|
239
|
|
|
public function iterateColumn() : Traversable |
|
240
|
|
|
{ |
|
241
|
|
|
return FetchUtils::iterateColumnByOne($this); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
public function rowCount() : int |
|
245
|
|
|
{ |
|
246
|
|
|
return sasql_stmt_affected_rows($this->stmt); |
|
|
|
|
|
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|