1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Connect; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Convert; |
6
|
|
|
use Iterator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Abstract query-result class. |
10
|
|
|
* Once again, this should be subclassed by an actual database implementation. It will only |
11
|
|
|
* ever be constructed by a subclass of SS_Database. The result of a database query - an iteratable object |
12
|
|
|
* that's returned by DB::SS_Query |
13
|
|
|
* |
14
|
|
|
* Primarily, the SS_Query class takes care of the iterator plumbing, letting the subclasses focusing |
15
|
|
|
* on providing the specific data-access methods that are required: {@link nextRecord()}, {@link numRecords()} |
16
|
|
|
* and {@link seek()} |
17
|
|
|
*/ |
18
|
|
|
abstract class Query implements \IteratorAggregate |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Return an array containing all the values from a specific column. If no column is set, then the first will be |
23
|
|
|
* returned |
24
|
|
|
* |
25
|
|
|
* @param string $column |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function column($column = null) |
29
|
|
|
{ |
30
|
|
|
$result = []; |
31
|
|
|
|
32
|
|
|
foreach ($this as $record) { |
33
|
|
|
if ($column) { |
|
|
|
|
34
|
|
|
$result[] = $record[$column]; |
35
|
|
|
} else { |
36
|
|
|
$result[] = $record[key($record)]; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $result; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return an array containing all values in the leftmost column, where the keys are the |
45
|
|
|
* same as the values. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function keyedColumn() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$column = []; |
52
|
|
|
|
53
|
|
|
foreach ($this as $record) { |
54
|
|
|
$val = $record[key($record)]; |
55
|
|
|
$column[$val] = $val; |
56
|
|
|
} |
57
|
|
|
return $column; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return a map from the first column to the second column. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function map() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$column = array(); |
68
|
|
|
foreach ($this as $record) { |
69
|
|
|
$key = reset($record); |
70
|
|
|
$val = next($record); |
71
|
|
|
$column[$key] = $val; |
72
|
|
|
} |
73
|
|
|
return $column; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the first record in the result |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function record() |
82
|
|
|
{ |
83
|
|
|
return $this->getIterator()->current(); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @deprecated Use record() instead |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function first() |
91
|
|
|
{ |
92
|
|
|
return $this->record(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the first column of the first record. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function value() |
101
|
|
|
{ |
102
|
|
|
$record = $this->record(); |
103
|
|
|
if ($record) { |
|
|
|
|
104
|
|
|
return $record[key($record)]; |
105
|
|
|
} |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return an HTML table containing the full result-set |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function table() |
115
|
|
|
{ |
116
|
|
|
$first = true; |
117
|
|
|
$result = "<table>\n"; |
118
|
|
|
|
119
|
|
|
foreach ($this as $record) { |
120
|
|
|
if ($first) { |
121
|
|
|
$result .= "<tr>"; |
122
|
|
|
foreach ($record as $k => $v) { |
123
|
|
|
$result .= "<th>" . Convert::raw2xml($k) . "</th> "; |
124
|
|
|
} |
125
|
|
|
$result .= "</tr> \n"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$result .= "<tr>"; |
129
|
|
|
foreach ($record as $k => $v) { |
130
|
|
|
$result .= "<td>" . Convert::raw2xml($v) . "</td> "; |
131
|
|
|
} |
132
|
|
|
$result .= "</tr> \n"; |
133
|
|
|
|
134
|
|
|
$first = false; |
135
|
|
|
} |
136
|
|
|
$result .= "</table>\n"; |
137
|
|
|
|
138
|
|
|
if ($first) { |
139
|
|
|
return "No records found"; |
140
|
|
|
} |
141
|
|
|
return $result; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return the next record in the query result. |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
abstract public function getIterator(); |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return the total number of items in the query result. |
153
|
|
|
* |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
|
|
abstract public function numRecords(); |
157
|
|
|
} |
158
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: