~ubuntu-branches/debian/squeeze/movabletype-opensource/squeeze

« back to all changes in this revision

Viewing changes to extlib/Data/ObjectDriver/ResultSet.pm

  • Committer: Bazaar Package Importer
  • Author(s): Dominic Hargreaves
  • Date: 2008-06-13 23:28:40 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080613232840-ya4jfxv1jgl45a3d
Tags: 4.2~rc2-1
* New upstream release candidate
* Update Standards-Version (no changes)
* Ensure that schema upgrade message is always seen

Show diffs side-by-side

added added

removed removed

Lines of Context:
91
91
        my $cur_terms    = $self->_terms || {};
92
92
        my $filter_terms = $self->_filter_terms || {};
93
93
        foreach my $k (keys %$terms) {
 
94
            $self->_results_loaded(0) unless $cur_terms->{$k};
94
95
            $cur_terms->{$k} = $terms->{$k};
95
96
            $filter_terms->{$k} = 1 if $self->_results_loaded;
96
97
        }
107
108
        foreach my $k (keys %$args) {
108
109
            my $val = $args->{$k};
109
110
 
110
 
            # If we get a limit arg that is bigger than our existing limit, then
 
111
            # If we get a limit arg that is bigger than our existing limit (and
 
112
            # we *have* an existing limit), then
111
113
            # make sure we force a requery.  Same for any filter arguments.
112
114
            # Same for offset arg that is  smaller than existing one.
113
 
            if ((($k eq 'limit')  and (($cur_args->{'limit'}||0)  < $val)) or
 
115
            if ((($k eq 'limit')  and
 
116
            ( exists $cur_args->{'limit'} && defined $cur_args->{'limit'} && ($cur_args->{'limit'}||0)  < $val)) or
114
117
                (($k eq 'offset') and (($cur_args->{'offset'}||0) > $val)) or
115
118
                ($k eq 'filters')) {
116
119
                $self->_results_loaded(0);
136
139
          if ref $term_names ne 'ARRAY';
137
140
 
138
141
        foreach my $n (@$term_names) {
139
 
            delete $terms->{$n};
 
142
            if (delete $terms->{$n}) {
 
143
                $self->_results_loaded(0);
 
144
            }
140
145
        }
141
146
    }
142
147
 
211
216
    }
212
217
}
213
218
 
 
219
# look at next() without incrementing the cursor
 
220
# like if you just want to see what's coming down the road at you
 
221
sub peek_next {
 
222
    my $self = shift;
 
223
 
 
224
    return if $self->is_finished;
 
225
 
 
226
    # Load the results and return an object
 
227
    my $results = $self->_load_results;
 
228
 
 
229
    my $obj = $results->[$self->_cursor + 1];
 
230
 
 
231
    return $obj;
 
232
}
 
233
 
 
234
 
 
235
 
214
236
sub prev {
215
237
    my $self = shift;
216
238
 
239
261
sub slice {
240
262
    my $self = shift;
241
263
    my ($start, $end) = @_;
242
 
    my $limit = $end - $start;
243
264
 
244
265
    # Do we already have results?
245
266
    if ($self->_results) {
246
 
        return @{ $self->_results }[$start, $end];
 
267
        return \@{ $self->_results }[$start..$end];
247
268
    }
248
269
 
 
270
    my $limit = $end - $start + 1;
 
271
 
249
272
    $self->add_offset($start);
250
273
    $self->add_limit($limit);
251
274
 
394
417
sub _load_results {
395
418
    my $self = shift;
396
419
 
397
 
    # An iterator only ResultSet doesn't have a class (or any paramaters for
398
 
    # that matter) so don't try to search for anything.
399
 
    return unless $self->class;
400
 
 
401
420
    # If results are already loaded, see if they need to be filtered and return
402
421
    # them
403
422
    if ($self->_results_loaded) {
406
425
        return $self->_results;
407
426
    }
408
427
 
 
428
    # An iterator only ResultSet doesn't have a class (or any paramaters for
 
429
    # that matter) so don't try to search for anything.
 
430
    return unless $self->class;
 
431
 
409
432
    local $Data::ObjectDriver::DEBUG = 1 if $self->dod_debug;
410
433
    my @r = $self->class->search($self->_terms, $self->_args);
411
434
 
415
438
    return \@r;
416
439
}
417
440
 
 
441
sub rewind {
 
442
    my $self = shift;
 
443
    $self->is_finished(0);
 
444
    $self->_cursor(-1);
 
445
    return $self;
 
446
}
418
447
1;
419
448
 
420
449
__END__
739
768
 
740
769
  $obj = $res->next;
741
770
 
 
771
=head2 peek_next
 
772
 
 
773
Retrieve the next item in the resultset WITHOUT advancing the cursor.
 
774
 
 
775
Arguments:
 
776
 
 
777
=over 4
 
778
 
 
779
=item I<none>
 
780
 
 
781
=back
 
782
 
 
783
; Return value
 
784
: The next object or undef if past the end of the result set
 
785
 
 
786
; Notes
 
787
: Calling this method will force a DB query.  All subsequent calls to I<curr> will return this object
 
788
 
 
789
; Example
 
790
 
 
791
  while ($bottle = $res->next){
 
792
            
 
793
      if ($bottle->type eq 'Bud Light' 
 
794
          && $res->peek_next->type eq 'Chimay'){
 
795
 
 
796
          $bottle->pass; #don't spoil my palate
 
797
 
 
798
      }else{
 
799
          $bottle->drink;
 
800
      }
 
801
  }
 
802
            
 
803
 
742
804
=head2 prev
743
805
 
744
806
Retrieve the previous item in the result set
859
921
Set this and you'll see $Data::ObjectDriver::DEBUG output when 
860
922
I go to get the results.
861
923
 
 
924
=head2 rewind
 
925
 
 
926
Move back to the start of the iterator for this instance of results of a query.
 
927
 
862
928
=head2 first
863
929
 
864
930
Returns the first object in the result set.