-
Notifications
You must be signed in to change notification settings - Fork 823
/
Copy pathQuery.php
265 lines (238 loc) · 6.37 KB
/
Query.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
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace SilverStripe\ORM\Connect;
use SilverStripe\Core\Convert;
use Iterator;
/**
* Abstract query-result class. A query result provides an iterator that returns a map for each record of a query
* result.
*
* The map should be keyed by the column names, and the values should use the following types:
*
* - boolean returned as integer 1 or 0 (to ensure consistency with MySQL that doesn't have native booleans)
* - integer types returned as integers
* - floating point / decimal types returned as floats
* - strings returned as strings
* - dates / datetimes returned as strings
*
* Note that until SilverStripe 4.3, bugs meant that strings were used for every column type.
*
* Once again, this should be subclassed by an actual database implementation. It will only
* ever be constructed by a subclass of SS_Database. The result of a database query - an iteratable object
* that's returned by DB::SS_Query
*
* Primarily, the Query class takes care of the iterator plumbing, letting the subclasses focusing
* on providing the specific data-access methods that are required: {@link nextRecord()}, {@link numRecords()}
* and {@link seek()}
*/
abstract class Query implements Iterator
{
/**
* The current record in the interator.
*
* @var array
*/
protected $currentRecord = null;
/**
* The number of the current row in the interator.
*
* @var int
*/
protected $rowNum = -1;
/**
* Flag to keep track of whether iteration has begun, to prevent unnecessary seeks
*
* @var bool
*/
protected $queryHasBegun = false;
/**
* Return an array containing all the values from a specific column. If no column is set, then the first will be
* returned
*
* @param string $column
* @return array
*/
public function column($column = null)
{
$result = [];
while ($record = $this->next()) {
if ($column) {
$result[] = $record[$column];
} else {
$result[] = $record[key($record)];
}
}
return $result;
}
/**
* Return an array containing all values in the leftmost column, where the keys are the
* same as the values.
*
* @return array
*/
public function keyedColumn()
{
$column = [];
foreach ($this as $record) {
$val = $record[key($record)];
$column[$val] = $val;
}
return $column;
}
/**
* Return a map from the first column to the second column.
*
* @return array
*/
public function map()
{
$column = [];
foreach ($this as $record) {
$key = reset($record);
$val = next($record);
$column[$key] = $val;
}
return $column;
}
/**
* Returns the next record in the iterator.
*
* @return array
*/
public function record()
{
return $this->next();
}
/**
* Returns the first column of the first record.
*
* @return string
*/
public function value()
{
$record = $this->next();
if ($record) {
return $record[key($record)];
}
return null;
}
/**
* Return an HTML table containing the full result-set
*
* @return string
*/
public function table()
{
$first = true;
$result = "<table>\n";
foreach ($this as $record) {
if ($first) {
$result .= "<tr>";
foreach ($record as $k => $v) {
$result .= "<th>" . Convert::raw2xml($k) . "</th> ";
}
$result .= "</tr> \n";
}
$result .= "<tr>";
foreach ($record as $k => $v) {
$result .= "<td>" . Convert::raw2xml($v) . "</td> ";
}
$result .= "</tr> \n";
$first = false;
}
$result .= "</table>\n";
if ($first) {
return "No records found";
}
return $result;
}
/**
* Iterator function implementation. Rewind the iterator to the first item and return it.
* Makes use of {@link seek()} and {@link numRecords()}, takes care of the plumbing.
*
* @return void
*/
public function rewind()
{
if ($this->queryHasBegun && $this->numRecords() > 0) {
$this->queryHasBegun = false;
$this->currentRecord = null;
$this->seek(0);
}
}
/**
* Iterator function implementation. Return the current item of the iterator.
*
* @return array
*/
public function current()
{
if (!$this->currentRecord) {
return $this->next();
} else {
return $this->currentRecord;
}
}
/**
* Iterator function implementation. Return the first item of this iterator.
*
* @return array
*/
public function first()
{
$this->rewind();
return $this->current();
}
/**
* Iterator function implementation. Return the row number of the current item.
*
* @return int
*/
public function key()
{
return $this->rowNum;
}
/**
* Iterator function implementation. Return the next record in the iterator.
* Makes use of {@link nextRecord()}, takes care of the plumbing.
*
* @return array
*/
public function next()
{
$this->queryHasBegun = true;
$this->currentRecord = $this->nextRecord();
$this->rowNum++;
return $this->currentRecord;
}
/**
* Iterator function implementation. Check if the iterator is pointing to a valid item.
*
* @return bool
*/
public function valid()
{
if (!$this->queryHasBegun) {
$this->next();
}
return $this->currentRecord !== false;
}
/**
* Return the next record in the query result.
*
* @return array
*/
abstract public function nextRecord();
/**
* Return the total number of items in the query result.
*
* @return int
*/
abstract public function numRecords();
/**
* Go to a specific row number in the query result and return the record.
*
* @param int $rowNum Row number to go to.
* @return array
*/
abstract public function seek($rowNum);
}