cursor.php
5.95 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/*
Copyright (c) 2009-2014 F3::Factory/Bong Cosca, All rights reserved.
This file is part of the Fat-Free Framework (http://fatfree.sf.net).
THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Please see the license.txt file for more information.
*/
namespace DB;
//! Simple cursor implementation
abstract class Cursor extends \Magic {
//@{ Error messages
const
E_Field='Undefined field %s';
//@}
protected
//! Query results
$query=array(),
//! Current position
$ptr=0,
//! Event listeners
$trigger=array();
/**
* Return database type
* @return string
**/
abstract function dbtype();
/**
* Return fields of mapper object as an associative array
* @return array
* @param $obj object
**/
abstract function cast($obj=NULL);
/**
* Return records (array of mapper objects) that match criteria
* @return array
* @param $filter string|array
* @param $options array
* @param $ttl int
**/
abstract function find($filter=NULL,array $options=NULL,$ttl=0);
/**
* Count records that match criteria
* @return int
* @param $filter array
* @param $ttl int
**/
abstract function count($filter=NULL,$ttl=0);
/**
* Insert new record
* @return array
**/
abstract function insert();
/**
* Update current record
* @return array
**/
abstract function update();
/**
* Hydrate mapper object using hive array variable
* @return NULL
* @param $key string
* @param $func callback
**/
abstract function copyfrom($key,$func=NULL);
/**
* Populate hive array variable with mapper fields
* @return NULL
* @param $key string
**/
abstract function copyto($key);
/**
* Return TRUE if current cursor position is not mapped to any record
* @return bool
**/
function dry() {
return empty($this->query[$this->ptr]);
}
/**
* Return first record (mapper object) that matches criteria
* @return object|FALSE
* @param $filter string|array
* @param $options array
* @param $ttl int
**/
function findone($filter=NULL,array $options=NULL,$ttl=0) {
return ($data=$this->find($filter,$options,$ttl))?$data[0]:FALSE;
}
/**
* Return array containing subset of records matching criteria,
* total number of records in superset, specified limit, number of
* subsets available, and actual subset position
* @return array
* @param $pos int
* @param $size int
* @param $filter string|array
* @param $options array
* @param $ttl int
**/
function paginate(
$pos=0,$size=10,$filter=NULL,array $options=NULL,$ttl=0) {
$total=$this->count($filter,$ttl);
$count=ceil($total/$size);
$pos=max(0,min($pos,$count-1));
return array(
'subset'=>$this->find($filter,
array_merge(
$options?:array(),
array('limit'=>$size,'offset'=>$pos*$size)
),
$ttl
),
'total'=>$total,
'limit'=>$size,
'count'=>$count,
'pos'=>$pos<$count?$pos:0
);
}
/**
* Map to first record that matches criteria
* @return array|FALSE
* @param $filter string|array
* @param $options array
* @param $ttl int
**/
function load($filter=NULL,array $options=NULL,$ttl=0) {
return ($this->query=$this->find($filter,$options,$ttl)) &&
$this->skip(0)?$this->query[$this->ptr=0]:FALSE;
}
/**
* Map to first record in cursor
* @return mixed
**/
function first() {
return $this->skip(-$this->ptr);
}
/**
* Map to last record in cursor
* @return mixed
**/
function last() {
return $this->skip(($ofs=count($this->query)-$this->ptr)?$ofs-1:0);
}
/**
* Map to nth record relative to current cursor position
* @return mixed
* @param $ofs int
**/
function skip($ofs=1) {
$this->ptr+=$ofs;
return $this->ptr>-1 && $this->ptr<count($this->query)?
$this->query[$this->ptr]:FALSE;
}
/**
* Map next record
* @return mixed
**/
function next() {
return $this->skip();
}
/**
* Map previous record
* @return mixed
**/
function prev() {
return $this->skip(-1);
}
/**
* Save mapped record
* @return mixed
**/
function save() {
return $this->query?$this->update():$this->insert();
}
/**
* Delete current record
* @return int|bool
**/
function erase() {
$this->query=array_slice($this->query,0,$this->ptr,TRUE)+
array_slice($this->query,$this->ptr,NULL,TRUE);
$this->ptr=0;
}
/**
* Define onload trigger
* @return callback
* @param $func callback
**/
function onload($func) {
return $this->trigger['load']=$func;
}
/**
* Define beforeinsert trigger
* @return callback
* @param $func callback
**/
function beforeinsert($func) {
return $this->trigger['beforeinsert']=$func;
}
/**
* Define afterinsert trigger
* @return callback
* @param $func callback
**/
function afterinsert($func) {
return $this->trigger['afterinsert']=$func;
}
/**
* Define oninsert trigger
* @return callback
* @param $func callback
**/
function oninsert($func) {
return $this->afterinsert($func);
}
/**
* Define beforeupdate trigger
* @return callback
* @param $func callback
**/
function beforeupdate($func) {
return $this->trigger['beforeupdate']=$func;
}
/**
* Define afterupdate trigger
* @return callback
* @param $func callback
**/
function afterupdate($func) {
return $this->trigger['afterupdate']=$func;
}
/**
* Define onupdate trigger
* @return callback
* @param $func callback
**/
function onupdate($func) {
return $this->afterupdate($func);
}
/**
* Define beforeerase trigger
* @return callback
* @param $func callback
**/
function beforeerase($func) {
return $this->trigger['beforeerase']=$func;
}
/**
* Define aftererase trigger
* @return callback
* @param $func callback
**/
function aftererase($func) {
return $this->trigger['aftererase']=$func;
}
/**
* Define onerase trigger
* @return callback
* @param $func callback
**/
function onerase($func) {
return $this->aftererase($func);
}
/**
* Reset cursor
* @return NULL
**/
function reset() {
$this->query=array();
$this->ptr=0;
}
}