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