1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Repository; |
4
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\Exception\IteratorException; |
6
|
|
|
|
7
|
|
|
class SQLRelayIterator extends GenericIterator |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
const RECORD_BUFFER = 50; |
11
|
|
|
|
12
|
|
|
private $_rowBuffer; |
13
|
|
|
protected $_currentRow = 0; |
14
|
|
|
protected $_moveNextRow = 0; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var resource |
18
|
|
|
*/ |
19
|
|
|
private $_cursor; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
* @param resource $cursor |
24
|
|
|
*/ |
25
|
|
|
public function __construct($cursor) |
26
|
|
|
{ |
27
|
|
|
$this->_cursor = $cursor; |
28
|
|
|
$this->_rowBuffer = array(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
|
|
public function count() |
35
|
|
|
{ |
36
|
|
|
return sqlrcur_rowCount($this->_cursor); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function hasNext() |
43
|
|
|
{ |
44
|
|
|
if (count($this->_rowBuffer) >= SQLRelayIterator::RECORD_BUFFER) { |
45
|
|
|
return true; |
46
|
|
|
} else if (is_null($this->_cursor)) { |
47
|
|
|
return (count($this->_rowBuffer) > 0); |
48
|
|
|
} else if ($this->_currentRow < $this->count()) { |
49
|
|
|
$sr = new SingleRow(); |
50
|
|
|
|
51
|
|
|
$colCount = sqlrcur_colCount($this->_cursor); |
52
|
|
|
for ($col = 0; $col < $colCount; $col++) { |
53
|
|
|
$fieldName = strtolower(sqlrcur_getColumnName($this->_cursor, $col)); |
54
|
|
|
$value = sqlrcur_getField($this->_cursor, $this->_currentRow, $col); |
55
|
|
|
|
56
|
|
|
if (is_null($value)) { |
57
|
|
|
$sr->addField($fieldName, ""); |
58
|
|
|
} elseif (is_object($value)) { |
59
|
|
|
$sr->addField($fieldName, "[OBJECT]"); |
60
|
|
|
} else { |
61
|
|
|
$value = AnyDataset::fixUTF8($value); |
62
|
|
|
$sr->addField($fieldName, $value); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->_currentRow++; |
67
|
|
|
|
68
|
|
|
// Enfileira o registo |
69
|
|
|
array_push($this->_rowBuffer, $sr); |
70
|
|
|
// Traz novos até encher o Buffer |
71
|
|
|
if (count($this->_rowBuffer) < DBIterator::RECORD_BUFFER) { |
72
|
|
|
$this->hasNext(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return true; |
76
|
|
|
} else { |
77
|
|
|
sqlrcur_free($this->_cursor); |
78
|
|
|
$this->_cursor = null; |
79
|
|
|
|
80
|
|
|
return (count($this->_rowBuffer) > 0); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function __destruct() |
85
|
|
|
{ |
86
|
|
|
if (!is_null($this->_cursor)) { |
87
|
|
|
@sqlrcur_free($this->_cursor); |
|
|
|
|
88
|
|
|
$this->_cursor = null; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return SingleRow |
94
|
|
|
* @throws IteratorException |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function moveNext() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if (!$this->hasNext()) { |
99
|
|
|
throw new IteratorException("No more records. Did you used hasNext() before moveNext()?"); |
100
|
|
|
} else { |
101
|
|
|
$sr = array_shift($this->_rowBuffer); |
102
|
|
|
$this->_moveNextRow++; |
103
|
|
|
return $sr; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
function key() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
return $this->_moveNextRow; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.