-
Notifications
You must be signed in to change notification settings - Fork 5
/
Statement.php
254 lines (205 loc) · 6.3 KB
/
Statement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
namespace Laracache\Pdo\Cache;
use Laracache\Pdo\Cache;
use Laracache\Pdo\Cache\Exceptions\CacheException;
use PDO;
use PDOStatement;
use stdClass;
class Statement extends PDOStatement
{
private $statement;
private $connection;
private $dbh;
private $options = [];
private $parameters = [];
private $results = [];
private $fetchMode = PDO::FETCH_OBJ;
/**
* Constructor.
*/
public function __construct(Cache $connection, $dbh, $statement, array $options = [])
{
$this->connection = $connection;
$this->parameters = $this->parameters($statement);
$this->statement = @\odbc_prepare(
$dbh,
\preg_replace('/(?<=\s|^):[^\s:]++/um', '?', $statement)
);
if (\odbc_error()) {
$this->throwException();
}
if (\strtolower(\get_resource_type($this->statement)) !== 'odbc result') {
throw new CacheException(
'Resource expected of type odbc result; '
. (string) \get_resource_type($this->statement) . ' received instead.'
);
}
$fetchMode = $connection->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE);
if ($fetchMode) {
$this->setFetchMode($fetchMode);
}
}
/**
* Executes a prepared statement.
*
* @param array
*
* @return bool
*/
public function execute($inputParameters = null)
{
$result = @\odbc_execute($this->statement, $this->parameters);
$this->parameters = [];
if (\odbc_error()) {
$this->throwException();
}
return $result;
}
/**
* Fetches the next row from a result set.
*
* @param null|int $fetchMode
* @param int $cursorOrientation
* @param int $cursorOffset
*
* @return mixed
*/
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
{
if ($fetchMode === null) {
$fetchMode = $this->fetchMode;
}
$toLowercase = ($this->getAttribute(PDO::ATTR_CASE) === PDO::CASE_LOWER);
$nullToString = ($this->getAttribute(PDO::ATTR_ORACLE_NULLS) === PDO::NULL_TO_STRING);
$nullEmptyString = ($this->getAttribute(PDO::ATTR_ORACLE_NULLS) === PDO::NULL_EMPTY_STRING);
switch ($fetchMode) {
case PDO::FETCH_BOTH:
case PDO::FETCH_ASSOC:
$resultSet = @\odbc_fetch_array($this->statement);
if (\odbc_error()) {
$this->throwException();
}
if ($resultSet === false) {
return false;
}
if ($toLowercase) {
$resultSet = \array_change_key_case($resultSet);
}
return $resultSet;
case PDO::FETCH_OBJ:
$resultSet = @\odbc_fetch_array($this->statement);
if (\odbc_error()) {
$this->throwException();
}
if ($resultSet === false) {
return false;
}
if ($toLowercase) {
$resultSet = \array_change_key_case($resultSet);
}
$object = new stdClass();
foreach ($resultSet as $field => $value) {
if (\is_null($value) && $nullToString) {
$resultSet[$field] = '';
}
if (empty($resultSet[$field]) && $nullEmptyString) {
$resultSet[$field] = null;
}
$object->{$field} = $value;
}
return $object;
}
return false;
}
/**
* Returns an array containing all of the result set rows.
*
* @param int $fetchMode
* @param mixed $fetchArgument
* @param array $ctorArgs
*
* @return array
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = [])
{
if (\is_null($fetchMode)) {
$fetchMode = $this->fetchMode;
}
$this->setFetchMode($fetchMode, $fetchArgument, $ctorArgs);
$this->results = [];
while ($row = $this->fetch()) {
if ((\is_array($row) || \is_object($row)) && \is_resource(\reset($row))) {
$stmt = new self($this->connection, \reset($row), $this->options);
$stmt->execute();
$stmt->setFetchMode($fetchMode, $fetchArgument, $ctorArgs);
while ($rs = $stmt->fetch()) {
$this->results[] = $rs;
}
} else {
$this->results[] = $row;
}
}
return $this->results;
}
/**
* Bind value to statement.
*/
public function bindValue($parameter, $value, $dataType = null)
{
$this->parameters[$parameter] = $value;
}
/**
* ODBC row count.
*
* @return int
*/
public function rowCount()
{
return \odbc_num_rows($this->statement);
}
/**
* Set PDO fetch mode.
*
* @return bool
*/
public function setFetchMode($fetchMode, $modeArg = null, $ctorArgs = [])
{
switch ($fetchMode) {
case PDO::FETCH_ASSOC:
case PDO::FETCH_BOTH:
case PDO::FETCH_OBJ:
$this->fetchMode = $fetchMode;
break;
default:
throw new CacheException('Requested fetch mode is not yet supported.');
}
return true;
}
/**
* Extract parameters from statement.
*
* @return array
*/
private function parameters($statement)
{
$parameters = [];
$values = \explode(' ', $statement);
$count = 0;
while (\array_key_exists($count, $values)) {
if (\preg_match('/^:/', $values[$count])) {
$parameters[$values[$count]] = null;
}
++$count;
}
return $parameters;
}
/**
* Throws an odbc exception object.
*
* @throw CacheException
*/
private function throwException()
{
throw new CacheException(\odbc_errormsg(), (int) \odbc_error());
}
}