1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Driver\SQLAnywhere; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Driver\Statement; |
6
|
|
|
use Doctrine\DBAL\Driver\StatementIterator; |
7
|
|
|
use Doctrine\DBAL\FetchMode; |
8
|
|
|
use Doctrine\DBAL\ParameterType; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
use const SASQL_BOTH; |
|
|
|
|
11
|
|
|
use function array_key_exists; |
12
|
|
|
use function assert; |
13
|
|
|
use function is_int; |
14
|
|
|
use function is_resource; |
15
|
|
|
use function sasql_fetch_array; |
|
|
|
|
16
|
|
|
use function sasql_fetch_assoc; |
|
|
|
|
17
|
|
|
use function sasql_fetch_row; |
|
|
|
|
18
|
|
|
use function sasql_prepare; |
|
|
|
|
19
|
|
|
use function sasql_stmt_affected_rows; |
|
|
|
|
20
|
|
|
use function sasql_stmt_bind_param_ex; |
|
|
|
|
21
|
|
|
use function sasql_stmt_errno; |
|
|
|
|
22
|
|
|
use function sasql_stmt_error; |
|
|
|
|
23
|
|
|
use function sasql_stmt_execute; |
|
|
|
|
24
|
|
|
use function sasql_stmt_field_count; |
|
|
|
|
25
|
|
|
use function sasql_stmt_reset; |
|
|
|
|
26
|
|
|
use function sasql_stmt_result_metadata; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* SAP SQL Anywhere implementation of the Statement interface. |
30
|
|
|
*/ |
31
|
|
|
class SQLAnywhereStatement implements IteratorAggregate, Statement |
32
|
|
|
{ |
33
|
|
|
/** @var resource The connection resource. */ |
34
|
|
|
private $conn; |
35
|
|
|
|
36
|
|
|
/** @var int Default fetch mode to use. */ |
37
|
|
|
private $defaultFetchMode = FetchMode::MIXED; |
38
|
|
|
|
39
|
|
|
/** @var resource|null The result set resource to fetch. */ |
40
|
|
|
private $result; |
41
|
|
|
|
42
|
|
|
/** @var resource The prepared SQL statement to execute. */ |
43
|
|
|
private $stmt; |
44
|
|
|
|
45
|
|
|
/** @var mixed[] The references to bound parameter values. */ |
46
|
|
|
private $boundValues = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Prepares given statement for given connection. |
50
|
|
|
* |
51
|
|
|
* @param resource $conn The connection resource to use. |
52
|
|
|
* @param string $sql The SQL statement to prepare. |
53
|
|
|
* |
54
|
|
|
* @throws SQLAnywhereException |
55
|
|
|
*/ |
56
|
|
|
public function __construct($conn, $sql) |
57
|
|
|
{ |
58
|
|
|
if (! is_resource($conn)) { |
59
|
|
|
throw new SQLAnywhereException('Invalid SQL Anywhere connection resource: ' . $conn); |
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($column, &$variable, $type = ParameterType::STRING, $length = null) |
76
|
|
|
{ |
77
|
|
|
assert(is_int($column)); |
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('Unknown type: ' . $type); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->boundValues[$column] =& $variable; |
100
|
|
|
|
101
|
|
|
if (! sasql_stmt_bind_param_ex($this->stmt, $column - 1, $variable, $type, $variable === null)) { |
|
|
|
|
102
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function bindValue($param, $value, $type = ParameterType::STRING) |
112
|
|
|
{ |
113
|
|
|
return $this->bindParam($param, $value, $type); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
* |
119
|
|
|
* @throws SQLAnywhereException |
120
|
|
|
*/ |
121
|
|
|
public function closeCursor() |
122
|
|
|
{ |
123
|
|
|
if (! sasql_stmt_reset($this->stmt)) { |
|
|
|
|
124
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function columnCount() |
134
|
|
|
{ |
135
|
|
|
return sasql_stmt_field_count($this->stmt); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function errorCode() |
142
|
|
|
{ |
143
|
|
|
return sasql_stmt_errno($this->stmt); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function errorInfo() |
150
|
|
|
{ |
151
|
|
|
return sasql_stmt_error($this->stmt); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
* |
157
|
|
|
* @throws SQLAnywhereException |
158
|
|
|
*/ |
159
|
|
|
public function execute($params = null) |
160
|
|
|
{ |
161
|
|
|
if ($params !== null) { |
162
|
|
|
$hasZeroIndex = array_key_exists(0, $params); |
163
|
|
|
|
164
|
|
|
foreach ($params as $key => $val) { |
165
|
|
|
if ($hasZeroIndex && is_int($key)) { |
166
|
|
|
$this->bindValue($key + 1, $val); |
167
|
|
|
} else { |
168
|
|
|
$this->bindValue($key, $val); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (! sasql_stmt_execute($this->stmt)) { |
|
|
|
|
174
|
|
|
throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->result = sasql_stmt_result_metadata($this->stmt); |
|
|
|
|
178
|
|
|
|
179
|
|
|
return true; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
* |
185
|
|
|
* @throws SQLAnywhereException |
186
|
|
|
*/ |
187
|
|
|
public function fetch($fetchMode = null) |
188
|
|
|
{ |
189
|
|
|
if (! is_resource($this->result)) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$fetchMode = $fetchMode ?? $this->defaultFetchMode; |
194
|
|
|
|
195
|
|
|
switch ($fetchMode) { |
196
|
|
|
case FetchMode::COLUMN: |
197
|
|
|
return $this->fetchColumn(); |
198
|
|
|
|
199
|
|
|
case FetchMode::ASSOCIATIVE: |
200
|
|
|
return sasql_fetch_assoc($this->result); |
|
|
|
|
201
|
|
|
|
202
|
|
|
case FetchMode::MIXED: |
203
|
|
|
return sasql_fetch_array($this->result, SASQL_BOTH); |
|
|
|
|
204
|
|
|
|
205
|
|
|
case FetchMode::NUMERIC: |
206
|
|
|
return sasql_fetch_row($this->result); |
|
|
|
|
207
|
|
|
|
208
|
|
|
default: |
209
|
|
|
throw new SQLAnywhereException('Fetch mode is not supported: ' . $fetchMode); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
|
|
public function fetchAll($fetchMode = null) |
217
|
|
|
{ |
218
|
|
|
$rows = []; |
219
|
|
|
|
220
|
|
|
switch ($fetchMode) { |
221
|
|
|
case FetchMode::COLUMN: |
222
|
|
|
while (($row = $this->fetchColumn()) !== false) { |
223
|
|
|
$rows[] = $row; |
224
|
|
|
} |
225
|
|
|
break; |
226
|
|
|
|
227
|
|
|
default: |
228
|
|
|
while (($row = $this->fetch($fetchMode)) !== false) { |
229
|
|
|
$rows[] = $row; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $rows; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* {@inheritdoc} |
238
|
|
|
*/ |
239
|
|
|
public function fetchColumn() |
240
|
|
|
{ |
241
|
|
|
$row = $this->fetch(FetchMode::NUMERIC); |
242
|
|
|
|
243
|
|
|
if ($row === false) { |
244
|
|
|
return false; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $row[0] ?? null; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
253
|
|
|
public function getIterator() |
254
|
|
|
{ |
255
|
|
|
return new StatementIterator($this); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritdoc} |
260
|
|
|
*/ |
261
|
|
|
public function rowCount() : int |
262
|
|
|
{ |
263
|
|
|
return sasql_stmt_affected_rows($this->stmt); |
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
|
|
public function setFetchMode($fetchMode) |
270
|
|
|
{ |
271
|
|
|
$this->defaultFetchMode = $fetchMode; |
272
|
|
|
|
273
|
|
|
return true; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|