1Data::ObjectDriver::ResUusletrSeCto(n3t)ributed Perl DocDuamtean:t:aOtbijoenctDriver::ResultSet(3)
2
3
4
6 Data::ObjectDriver::ResultSet - Manage a DB query
7
9 # Get a resultset object for Object::Widget, which inherits from
10 # Data::ObjectDriver::BaseObject
11 my $result = Object::Widget->result($terms, $args);
12
13 $result->add_term({color => 'blue'});
14
15 $result->add_limit(10);
16 $result->add_offset(100);
17
18 while (not $result->is_empty) {
19 my $widget = $result->next;
20
21 # Do stuff with $widget
22 }
23
25 This object is returned by the 'result' method found in the
26 Data::ObjectDriver::BaseObject class. This object manages a query and
27 the resulting data. It allows additional search terms and arguments to
28 be added and will not submit the query until a method that returns data
29 is called. By passing this object around code in multiple places can
30 alter the query easily until the data is needed.
31
32 Once a method returning data is called (next, count, etc) the query is
33 submitted to the database and the returned data is managed by the
34 ResultSet object like an iterator.
35
37 $result_set = $class->result($terms, $args)
38 This method is actually defined in Data::ObjectDriver::BaseObject but
39 it is the way a new ResultSet object is created.
40
41 Arguments:
42
43 $terms - A hashref. Same format as the first argument to
44 Data::ObjectDriver::DBI::search
45 $args - A hashref. Same format as the second argument to
46 Data::ObjectDriver::DBI::search
47
48 Return value:
49
50 This method returns a Data::ObjectDriver::ResultSet object
51
52 $new_result = Data::ObjectDriver::ResultSet->iterator(\@data)
53 Create a new result set object that takes existing data and operates
54 only as an iterator, without any of the query managment.
55
56 Arguments:
57
58 $data - An array ref of data elements
59
60 Return value:
61
62 A Data::ObjectDriver::ResultSet object
63
64 add_constraint
65 Apply a constraint to the result. The format of the two arguments is
66 the same as for Data::ObjectDriver::DBI::search
67
68 Arguments:
69
70 $terms - A hashref of object fields and values constraining them. Same
71 as first parameter to result method.
72 $args - A hashref of values that affect the returned data, such as
73 limit and sort by. Same as first parameter to result method.
74
75 ; Return value : Returns 1 if successful and 0 otherwise
76
77 ; Notes : Do we fail if called after we've retrieved the result set?
78 Ignore it? Requery?
79
80 ; Example
81
82 $res->add_constraint({object_id => $id}, {limit => 100})
83
84 add_term
85 Apply a single search term to the result. Equivalent to:
86
87 $res->add_constraint($terms)
88
89 Arguments:
90
91 $terms - A hashref of object fields and values constraining them
92
93 ; Return value : Returns 1 if successful and 0 otherwise
94
95 ; Notes : Same question as for add_constraint
96
97 ; Example
98
99 $res->add_term({object_id => $id})
100
101 clear_term
102 Clear a single search term from the result.
103
104 Arguments:
105
106 @terms - An array of term names to clear
107
108 ; Return value : Returns 1 if successful and 0 otherwise
109
110 ; Notes : none
111
112 ; Example
113
114 $res->clear_term(qw(limit offset))
115
116 add_limit
117 Apply a limit to the result. Equivalent to:
118
119 $res->add_constraint({}, {limit => $limit})
120
121 Arguments:
122
123 $limit - A scalar numeric value giving the limit of the number of
124 objects returned
125
126 ; Return value : Returns 1 if successful and 0 otherwise
127
128 ; Notes :
129
130 ; Example
131
132 $res->add_limit(100)
133
134 clear_limit
135 Clear any limit value in the result.
136
137 Arguments:
138
139 none
140
141 ; Return value : Returns 1 if successful and 0 otherwise
142
143 ; Notes : None
144
145 ; Example
146
147 $res->clear_limit
148
149 add_offset
150 Add an offset for the results returned. Result set must also have a
151 limit set at some point.
152
153 Arguments:
154
155 $offset - A scalar numeric value giving the offset for the first object
156 returned
157
158 ; Return value : Returns 1 if successful and 0 otherwise
159
160 ; Notes : none
161
162 ; Example
163
164 $res->add_offset(5_000)
165
166 clear_offset
167 Clear any offset value in the result.
168
169 Arguments:
170
171 none
172
173 ; Return value : Returns 1 if successful and 0 otherwise
174
175 ; Notes :
176
177 ; Example
178
179 $res->clear_offset
180
181 add_order
182 Add a sort order for the results returned.
183
184 Arguments:
185
186 [0] = $order = - A scalar string value giving the sort order for the
187 results, one of ascend or descend
188
189 ; Return value : Returns 1 if successful and 0 otherwise
190
191 ; Notes : >none''
192
193 ; Example
194
195 $res->add_order('ascend')
196
197 clear_order
198 Clear any offset value in the result.
199
200 Arguments:
201
202 none
203
204 ; Return value : Returns 1 if successful and 0 otherwise
205
206 ; Notes : none
207
208 ; Example
209
210 $res->clear_order
211
212 index
213 Return the current index into the result set.
214
215 Arguments:
216
217 none
218
219 ; Return value : An integer giving the zero based index of the current
220 element in the result set.
221
222 ; Notes : none
223
224 ; Example
225
226 $idx = $res->index;
227
228 next
229 Retrieve the next item in the resultset
230
231 Arguments:
232
233 none
234
235 ; Return value : The next object or undef if past the end of the result
236 set
237
238 ; Notes : Calling this method will force a DB query. All subsequent
239 calls to curr will return this object
240
241 ; Example
242
243 $obj = $res->next;
244
245 peek_next
246 Retrieve the next item in the resultset WITHOUT advancing the cursor.
247
248 Arguments:
249
250 none
251
252 ; Return value : The next object or undef if past the end of the result
253 set
254
255 ; Notes : Calling this method will force a DB query. All subsequent
256 calls to curr will return this object
257
258 ; Example
259
260 while ($bottle = $res->next){
261
262 if ($bottle->type eq 'Bud Light'
263 && $res->peek_next->type eq 'Chimay'){
264
265 $bottle->pass; #don't spoil my palate
266
267 }else{
268 $bottle->drink;
269 }
270 }
271
272 prev
273 Retrieve the previous item in the result set
274
275 Arguments:
276
277 none
278
279 ; Return value : The previous object or undef if before the beginning
280 of the result set
281
282 ; Notes : All subsequent calls to curr will return this object
283
284 ; Example
285
286 $obj = $res->prev;
287
288 curr
289 Retrieve the current item in the result set. This item is set by calls
290 to next and prev
291
292 Arguments:
293
294 none
295
296 ; Return value : The current object or undef if past the boundaries of
297 the result set
298
299 ; Notes : none
300
301 ; Example
302
303 $obj = $res->curr
304
305 slice
306 Return a slice of the result set. This is logically equivalent to
307 setting a limit and offset and then retrieving all the objects via
308 -next>. If you call slice and then call next, you will get undef and
309 additionally is_empty will be true.
310
311 Arguments:
312
313 $from - Scalar integer giving the start of the slice range
314 $to - Scalar integer giving the end of the slice range
315
316 ; Return value : An array of objects
317
318 ; Notes : Objects are index from 0 just like perl arrays.
319
320 ; Example
321
322 my @objs = $res->slice(0, 20)
323
324 count
325 Get the count of the items in the result set.
326
327 Arguments:
328
329 none
330
331 ; Return value : A scalar count of the number of items in the result
332 set
333
334 ; Notes : This will cause a count() query on the database if the result
335 set hasn't been retrieved yet. If the result set has been retrieved it
336 will just return the number of objects stored in the result set object.
337
338 ; Example
339
340 $num = $res->count
341
342 is_finished
343 Returns whether we've arrived at the end of the result set
344
345 Arguments:
346
347 none
348
349 ; Return value : Returns 1 if we are finished iterating though the
350 result set and 0 otherwise
351
352 ; Notes : none
353
354 ; Example
355
356 while (not $res->is_finished) {
357 my $obj = $res->next;
358 # Stuff ...
359 }
360
361 dod_debug
362 Set this and you'll see $Data::ObjectDriver::DEBUG output when I go to
363 get the results.
364
365 rewind
366 Move back to the start of the iterator for this instance of results of
367 a query.
368
369 first
370 Returns the first object in the result set.
371
372 Arguments:
373
374 none
375
376 ; Return value : The first object in the result set
377
378 ; Notes : Resets the current cursor so that calls to curr return this
379 value.
380
381 ; Example
382
383 $obj = $res->first
384
385 last
386 Returns the last object in the result set.
387
388 Arguments:
389
390 none
391
392 ; Return value : The last object in the result set
393
394 ; Notes : Resets the current cursor so that calls to curr return this
395 value.
396
397 ; Example
398
399 $obj = $res->last
400
401 is_last
402 Returns 1 if the cursor is on the last row of the result set, 0 if it
403 is not.
404
405 Arguments:
406
407 none
408
409 ; Return value : Returns 1 if the cursor is on the last row of the
410 result set, 0 if it is not.
411
412 ; Example
413
414 if ( $res->is_last ) {
415 ## do some stuff
416 }
417
418
419
420perl v5.12.0 2010-03-22 Data::ObjectDriver::ResultSet(3)