/
query_iterator.class.php
118 lines (99 loc) · 3.26 KB
/
query_iterator.class.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
<?php
namespace PluSQL;
use Iterator;
class QueryIterator implements Iterator
{
private $index;
private $starting_index;
private $query;
private $table;
private $current_row;
private $constrain;
private $current_key_signature;
public function __construct(Query $query,$table,$index = 0)
{
$this->index = $index;
$this->starting_index = $index;
$this->query = $query;
$this->table = $table;
$this->current_row = NULL;
$this->constrain = array();
$test = new QueryRow($this->query,$this->table,$this->index);
$test->keySignature();
}
public function constrainKeys($pairs)
{
$this->constrain = $pairs;
}
public function checkConstraints()
{
$row = $this->query->rowAtIndex($this->index);
foreach($this->constrain as $name => $value)
{
if(!isset($row[$name]))
throw new InvalidQueryRowException('I am somehow constrained to field: '.$name.' but that doesn\'t exist in the query ... que?');
if($row[$name] != $value)
throw new InvalidQueryRowException('Constrain no que es pendeho mucho');
}
}
public function current()
{
try
{
$this->checkConstraints();
$this->current_row = new QueryRow($this->query,$this->table,$this->index);
$ret = $this->current_row;
}
catch(InvalidQueryRowException $exc)
{
$ret = FALSE;
}
return $ret;
}
public function key()
{
return $this->index;
}
public function next()
{
try
{
$next_row = new QueryRow($this->query,$this->table,$this->index);
while($next_row->keySignature() == $this->current_row->keySignature())
{
$this->index++;
$next_row = new QueryRow($this->query,$this->table,$this->index);
$this->checkConstraints();
}
$this->current_row = $next_row;
$ret = $next_row;
}
catch(InvalidQueryRowException $exc)
{
$ret = FALSE;
}
return $ret;
}
public function rewind()
{
$this->index = $this->starting_index;
$this->current_row = NULL;
}
public function valid()
{
try
{
$ret = is_object(new QueryRow($this->query,$this->table,$this->index));
$this->checkConstraints();
}
catch(InvalidQueryRowException $exc)
{
$ret = FALSE;
}
return $ret;
}
public function __get($name)
{
return $this->current()->$name;
}
}