|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Dataset; |
|
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
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (is_null($this->cursor)) { |
|
49
|
|
|
return (count($this->rowBuffer) > 0); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if ($this->currentRow < $this->count()) { |
|
53
|
|
|
$sr = new Row(); |
|
54
|
|
|
|
|
55
|
|
|
$colCount = sqlrcur_colCount($this->cursor); |
|
56
|
|
|
for ($col = 0; $col < $colCount; $col++) { |
|
57
|
|
|
$fieldName = strtolower(sqlrcur_getColumnName($this->cursor, $col)); |
|
58
|
|
|
$value = sqlrcur_getField($this->cursor, $this->currentRow, $col); |
|
59
|
|
|
|
|
60
|
|
|
if (is_null($value)) { |
|
61
|
|
|
$sr->addField($fieldName, ""); |
|
62
|
|
|
} elseif (is_object($value)) { |
|
63
|
|
|
$sr->addField($fieldName, "[OBJECT]"); |
|
64
|
|
|
} else { |
|
65
|
|
|
$value = AnyDataset::fixUTF8($value); |
|
66
|
|
|
$sr->addField($fieldName, $value); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->currentRow++; |
|
71
|
|
|
|
|
72
|
|
|
// Enfileira o registo |
|
73
|
|
|
array_push($this->rowBuffer, $sr); |
|
74
|
|
|
// Traz novos até encher o Buffer |
|
75
|
|
|
if (count($this->rowBuffer) < DbIterator::RECORD_BUFFER) { |
|
76
|
|
|
$this->hasNext(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
sqlrcur_free($this->cursor); |
|
83
|
|
|
$this->cursor = null; |
|
84
|
|
|
|
|
85
|
|
|
return (count($this->rowBuffer) > 0); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function __destruct() |
|
89
|
|
|
{ |
|
90
|
|
|
if (!is_null($this->cursor)) { |
|
91
|
|
|
@sqlrcur_free($this->cursor); |
|
|
|
|
|
|
92
|
|
|
$this->cursor = null; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return Row |
|
98
|
|
|
* @throws IteratorException |
|
99
|
|
|
*/ |
|
100
|
|
View Code Duplication |
public function moveNext() |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
if (!$this->hasNext()) { |
|
103
|
|
|
throw new IteratorException("No more records. Did you used hasNext() before moveNext()?"); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$sr = array_shift($this->rowBuffer); |
|
107
|
|
|
$this->moveNextRow++; |
|
108
|
|
|
return $sr; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function key() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->moveNextRow; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
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.