|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Provides a layer for advanced queries |
|
5
|
|
|
* |
|
6
|
|
|
* PHP Version 5 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Core |
|
9
|
|
|
* @package Datamapper |
|
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
11
|
|
|
* @copyright 2013 cSphere Team |
|
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
13
|
|
|
* @link http://www.csphere.eu |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
namespace csphere\core\datamapper; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Provides a layer for advanced queries |
|
20
|
|
|
* |
|
21
|
|
|
* @category Core |
|
22
|
|
|
* @package Datamapper |
|
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
24
|
|
|
* @copyright 2013 cSphere Team |
|
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
26
|
|
|
* @link http://www.csphere.eu |
|
27
|
|
|
**/ |
|
28
|
|
|
|
|
29
|
|
|
class Finder extends \csphere\core\datamapper\Base |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Parts for a query |
|
33
|
|
|
**/ |
|
34
|
|
|
private $_parts = ['joins' => '', |
|
35
|
|
|
'columns' => '*', |
|
36
|
|
|
'where' => [], |
|
37
|
|
|
'order' => [], |
|
38
|
|
|
'group' => [], |
|
39
|
|
|
'having' => []]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Reset layout for parts of a query |
|
43
|
|
|
**/ |
|
44
|
|
|
private $_reset = ['joins' => '', |
|
|
|
|
|
|
45
|
|
|
'columns' => '*', |
|
46
|
|
|
'where' => [], |
|
47
|
|
|
'order' => [], |
|
48
|
|
|
'group' => [], |
|
49
|
|
|
'having' => []]; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
|
|
|
|
|
52
|
|
|
* Get first record matching filters |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
**/ |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
public function first() |
|
|
|
|
|
|
58
|
|
|
{ |
|
|
|
|
|
|
59
|
|
|
// Construct query and fetch result |
|
|
|
|
|
|
60
|
|
|
$sql = \csphere\core\sql\DML::select( |
|
|
|
|
|
|
61
|
|
|
$this->schema, |
|
62
|
|
|
$this->_parts['joins'], |
|
63
|
|
|
$this->_parts['columns'], |
|
64
|
|
|
$this->_parts['where'], |
|
65
|
|
|
$this->_parts['order'], |
|
66
|
|
|
$this->_parts['group'], |
|
67
|
|
|
$this->_parts['having'] |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$result = $this->database->query($sql['statement'], $sql['input'], 0, 1); |
|
71
|
|
|
|
|
72
|
|
|
// Reset parts array |
|
73
|
|
|
$this->_parts = $this->_reset; |
|
74
|
|
|
|
|
75
|
|
|
return $result; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Removes all entries that match given parameters. The scope can be reduced |
|
80
|
|
|
* by using where and join. All other method-interactions might be ignored. |
|
81
|
|
|
* |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
public function remove() |
|
85
|
|
|
{ |
|
86
|
|
|
// Construct query and execute it |
|
87
|
|
|
$sql = \csphere\core\sql\DML::delete( |
|
88
|
|
|
$this->schema, |
|
89
|
|
|
$this->_parts['where'], |
|
90
|
|
|
$this->_parts['joins'] |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
$result = $this->database->exec($sql['statement'], $sql['input']); |
|
94
|
|
|
|
|
95
|
|
|
// Reset parts array |
|
96
|
|
|
$this->_parts = $this->_reset; |
|
97
|
|
|
|
|
98
|
|
|
return $result; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Find all records matching filters |
|
103
|
|
|
* |
|
104
|
|
|
* @param integer $first Number of the first dataset to show |
|
105
|
|
|
* @param integer $max Number of datasets to show from first on |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
**/ |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
public function find($first, $max) |
|
111
|
|
|
{ |
|
112
|
|
|
// Sort by serial ascending if nothing else is specified |
|
113
|
|
|
if ($this->_parts['order'] == []) { |
|
114
|
|
|
|
|
115
|
|
|
$this->_parts['order'] = [$this->serial, 'ASC']; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Construct query and fetch result |
|
119
|
|
|
$sql = \csphere\core\sql\DML::select( |
|
120
|
|
|
$this->schema, |
|
121
|
|
|
$this->_parts['joins'], |
|
122
|
|
|
$this->_parts['columns'], |
|
123
|
|
|
$this->_parts['where'], |
|
124
|
|
|
$this->_parts['order'], |
|
125
|
|
|
$this->_parts['group'], |
|
126
|
|
|
$this->_parts['having'] |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
$result = $this->database->query( |
|
130
|
|
|
$sql['statement'], $sql['input'], $first, $max |
|
131
|
|
|
); |
|
132
|
|
|
|
|
133
|
|
|
// Handle array dimension for max=1 since db layer uses fetch for that case |
|
134
|
|
|
if ($max == 1 && $result != []) { |
|
135
|
|
|
$result = [$result]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// Reset parts array |
|
139
|
|
|
$this->_parts = $this->_reset; |
|
140
|
|
|
|
|
141
|
|
|
return $result; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Find amount of records matching filters |
|
146
|
|
|
* |
|
147
|
|
|
* @return integer |
|
148
|
|
|
**/ |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
public function count() |
|
151
|
|
|
{ |
|
152
|
|
|
// Construct query and fetch result |
|
153
|
|
|
$sql = \csphere\core\sql\DML::select( |
|
154
|
|
|
$this->schema, |
|
155
|
|
|
$this->_parts['joins'], |
|
156
|
|
|
'COUNT(DISTINCT( ' . $this->serial . ')) AS count', |
|
157
|
|
|
$this->_parts['where'] |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
$result = $this->database->query( |
|
161
|
|
|
$sql['statement'], $sql['input'], 0, 0 |
|
162
|
|
|
); |
|
163
|
|
|
|
|
164
|
|
|
$result = isset($result[0]['count']) ? $result[0]['count'] : 0; |
|
165
|
|
|
|
|
166
|
|
|
// Reset parts array |
|
167
|
|
|
$this->_parts = $this->_reset; |
|
168
|
|
|
|
|
169
|
|
|
return $result; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Add a natural (inner) join to the query. By default the given foreignPlugin |
|
174
|
|
|
* and foreignTable are joined with the plugin table. To set another plugin and |
|
175
|
|
|
* table that joins the foreign table, you have to use the last two parameters. |
|
176
|
|
|
* |
|
177
|
|
|
* If the table name is the plugin name then provide an empty string for table. |
|
178
|
|
|
* |
|
179
|
|
|
* Serial is the key which is used for the join. If no foreign is given the |
|
180
|
|
|
* serial key is used for both sides as the column name for the join. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $foreignPlugin Foreign Plugin name (the one we want to |
|
183
|
|
|
* @param string $foreignTable Foreign Table name |
|
184
|
|
|
* @param string $serial Serial column name (native table) |
|
185
|
|
|
* @param string $foreignColumn Foreign column name (other table) |
|
186
|
|
|
* @param string $plugin Plugin name (the one we already have) |
|
187
|
|
|
* @param string $table Table name (the one we already have) |
|
188
|
|
|
* |
|
189
|
|
|
* @return \csphere\core\datamapper\Finder |
|
190
|
|
|
**/ |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
public function join( |
|
193
|
|
|
$foreignPlugin, |
|
194
|
|
|
$foreignTable, |
|
195
|
|
|
$serial, |
|
196
|
|
|
$foreignColumn = '', |
|
197
|
|
|
$plugin = '', |
|
198
|
|
|
$table = '' |
|
199
|
|
|
) { |
|
200
|
|
|
// Use plugin and table of this object by default |
|
201
|
|
|
if ($plugin == '') { |
|
202
|
|
|
|
|
203
|
|
|
$plugin = $this->schema; |
|
204
|
|
|
|
|
205
|
|
|
} elseif ($table != '') { |
|
206
|
|
|
|
|
207
|
|
|
$plugin .= '_' . $table; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// Add foreign table to table name if used |
|
211
|
|
|
if ($foreignTable != '') { |
|
212
|
|
|
|
|
213
|
|
|
$foreignPlugin .= '_' . $foreignTable; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
$this->_parts['joins'][] = [ |
|
217
|
|
|
$plugin, $foreignPlugin, $serial, $foreignColumn |
|
218
|
|
|
]; |
|
219
|
|
|
|
|
220
|
|
|
return $this; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Add a where condition to the query |
|
225
|
|
|
* |
|
226
|
|
|
* @param string $column Column name |
|
227
|
|
|
* @param string $operation Operation, e.g. = or != |
|
228
|
|
|
* @param mixed $value Value as string or e.g. for BETWEEN as an array |
|
229
|
|
|
* @param boolean $not Negate the condition if set to true |
|
230
|
|
|
* @param boolean $xor Change concatination from AND to OR if set to true |
|
231
|
|
|
* |
|
232
|
|
|
* @return \csphere\core\datamapper\Finder |
|
233
|
|
|
**/ |
|
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
public function where($column, $operation, $value, $not = false, $xor = false) |
|
236
|
|
|
{ |
|
237
|
|
|
$this->_parts['where'][] = [$column, $operation, $value, $not, $xor]; |
|
238
|
|
|
|
|
239
|
|
|
return $this; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Define columns for the query |
|
244
|
|
|
* |
|
245
|
|
|
* @param mixed $list List of columns as a string or an array |
|
246
|
|
|
* |
|
247
|
|
|
* @return \csphere\core\datamapper\Finder |
|
248
|
|
|
**/ |
|
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
public function columns($list) |
|
251
|
|
|
{ |
|
252
|
|
|
$this->_parts['columns'] = $list; |
|
253
|
|
|
|
|
254
|
|
|
return $this; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Add a where condition to the query |
|
259
|
|
|
* |
|
260
|
|
|
* @param string $column Column name |
|
261
|
|
|
* @param boolean $desc Start with last record if set to true |
|
262
|
|
|
* |
|
263
|
|
|
* @return \csphere\core\datamapper\Finder |
|
264
|
|
|
**/ |
|
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
public function orderBy($column, $desc = false) |
|
267
|
|
|
{ |
|
268
|
|
|
$this->_parts['order'][] = [$column, $desc]; |
|
269
|
|
|
|
|
270
|
|
|
return $this; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Define group by condition for the query |
|
275
|
|
|
* |
|
276
|
|
|
* @param mixed $list List of columns as a string or as an array |
|
277
|
|
|
* |
|
278
|
|
|
* @return \csphere\core\datamapper\Finder |
|
279
|
|
|
**/ |
|
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
public function groupBy($list) |
|
282
|
|
|
{ |
|
283
|
|
|
if (!is_array($list)) { |
|
284
|
|
|
|
|
285
|
|
|
$list = [$list]; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
$this->_parts['group'] = $list; |
|
289
|
|
|
|
|
290
|
|
|
return $this; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Add a having condition to the query |
|
295
|
|
|
* |
|
296
|
|
|
* @param string $column Column name |
|
297
|
|
|
* @param string $operation Operation, e.g. = or != |
|
298
|
|
|
* @param mixed $value Value as string or e.g. for BETWEEN as an array |
|
299
|
|
|
* @param boolean $not Negate the condition if set to true |
|
300
|
|
|
* @param boolean $xor Change concatination from AND to OR if set to true |
|
301
|
|
|
* |
|
302
|
|
|
* @return \csphere\core\datamapper\Finder |
|
303
|
|
|
**/ |
|
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
public function having($column, $operation, $value, $not = false, $xor = false) |
|
306
|
|
|
{ |
|
307
|
|
|
$this->_parts['having'][] = [$column, $operation, $value, $not, $xor]; |
|
308
|
|
|
|
|
309
|
|
|
return $this; |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|