|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Reduce\Db; |
|
4
|
|
|
|
|
5
|
|
|
use Iterator; |
|
6
|
|
|
use ArrayAccess; |
|
7
|
|
|
use Countable; |
|
8
|
|
|
use JsonSerializable; |
|
9
|
|
|
use Reduce\Db\Row; |
|
10
|
|
|
use Reduce\Db\Query\QueryBuilder; |
|
11
|
|
|
|
|
12
|
|
|
class ResultSet implements Iterator, ArrayAccess, Countable, JsonSerializable |
|
13
|
|
|
{ |
|
14
|
|
|
protected $queryBuilder; |
|
15
|
|
|
protected $data; |
|
16
|
|
|
protected $single = false; |
|
17
|
|
|
protected $executed = false; |
|
18
|
|
|
protected $tableName; |
|
19
|
|
|
|
|
20
|
10 |
|
public function __construct($tableName, QueryBuilder $queryBuilder, $singleResultSet = false) |
|
21
|
|
|
{ |
|
22
|
10 |
|
$this->tableName = $tableName; |
|
23
|
10 |
|
$this->queryBuilder = $queryBuilder; |
|
24
|
10 |
|
$this->single = $singleResultSet; |
|
25
|
|
|
|
|
26
|
10 |
|
$this->queryBuilder->select('*')->from($tableName); |
|
27
|
10 |
|
} |
|
28
|
|
|
|
|
29
|
6 |
|
public function __call($name, $args) |
|
30
|
|
|
{ |
|
31
|
6 |
|
if (method_exists($this->getQueryBuilder(), $name)) { |
|
32
|
6 |
|
call_user_func_array([$this->getQueryBuilder(), $name], $args); |
|
33
|
6 |
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$condition = sprintf('%s.id = %s.%s_id', $this->getTableName(), $name, $this->getTableName()); |
|
37
|
|
|
|
|
38
|
|
|
$this->queryBuilder->leftJoin($this->getTableName(), $name, $name, $condition); |
|
39
|
|
|
|
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
public function getTableName() |
|
44
|
|
|
{ |
|
45
|
1 |
|
return $this->tableName; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
8 |
|
public function getQueryBuilder() |
|
49
|
|
|
{ |
|
50
|
8 |
|
return $this->queryBuilder; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function execute() |
|
54
|
|
|
{ |
|
55
|
|
|
if (!$this->executed) { |
|
56
|
|
|
$this->data = $this->getQueryBuilder()->execute()->fetchAll(); |
|
57
|
|
|
$this->executed = true; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
protected function createRow($data = []) |
|
62
|
|
|
{ |
|
63
|
1 |
|
return new Row($this, $data); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function offsetGet($key) |
|
67
|
|
|
{ |
|
68
|
1 |
|
if ($this->single) { |
|
69
|
1 |
|
$this->getQueryBuilder()->where('id = ?', $key); |
|
70
|
|
|
|
|
71
|
1 |
|
return $this->createRow(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->execute(); |
|
75
|
|
|
|
|
76
|
|
|
if (isset($this->data[$key])) { |
|
77
|
|
|
return $this->data[$key]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return []; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function offsetSet($key, $value) |
|
84
|
|
|
{} |
|
85
|
|
|
|
|
86
|
|
|
public function offsetUnset($key) |
|
87
|
|
|
{} |
|
88
|
|
|
|
|
89
|
|
|
public function offsetExists($key) |
|
90
|
|
|
{ |
|
91
|
|
|
return !! $this->offsetGet($key); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function count() |
|
95
|
|
|
{ |
|
96
|
|
|
$this->execute(); |
|
97
|
|
|
return count($this->data); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function rewind() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->execute(); |
|
103
|
|
|
reset($this->data); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function current() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->createRow(current($this->data)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function key() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->execute(); |
|
114
|
|
|
return key($this->data); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function next() |
|
118
|
|
|
{ |
|
119
|
|
|
next($this->data); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function valid() |
|
123
|
|
|
{ |
|
124
|
|
|
$key = key($this->data); |
|
125
|
|
|
return ($key !== null); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function toArray() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->execute(); |
|
131
|
|
|
return $this->data; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function jsonSerialize() |
|
135
|
|
|
{ |
|
136
|
|
|
$this->execute(); |
|
137
|
|
|
return $this->toArray(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
7 |
|
public function __toString() |
|
141
|
|
|
{ |
|
142
|
7 |
|
return $this->getQueryBuilder()->__toString(); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|