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