| 1 | # $Id: BaseObject.pm 418 2007-11-29 18:08:10Z garth $ | ||
| 2 | |||
| 3 | package Data::ObjectDriver::ResultSet; | ||
| 4 | |||
| 5 | ## Dependencies | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | |||
| 9 | use base qw( Class::Accessor::Fast ); | ||
| 10 | use List::Util qw(min); | ||
| 11 | |||
| 12 | ## Public/_Private Accessors | ||
| 13 | |||
| 14 | __PACKAGE__->mk_accessors(qw( | ||
| 15 | class | ||
| 16 | is_finished | ||
| 17 | |||
| 18 | dod_debug | ||
| 19 | |||
| 20 | _terms | ||
| 21 | _args | ||
| 22 | _filter_terms | ||
| 23 | _filter_args | ||
| 24 | |||
| 25 | _cursor | ||
| 26 | _results | ||
| 27 | _results_loaded | ||
| 28 | )); | ||
| 29 | |||
| 30 | ## Constructors | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | my $class = shift; | ||
| 34 | my ($param) = @_; | ||
| 35 | my $self = bless {}, ref $class || $class; | ||
| 36 | |||
| 37 | $self->class($param->{class}); | ||
| 38 | |||
| 39 | $self->is_finished(0); | ||
| 40 | $self->add_constraint($param->{terms}, $param->{args}); | ||
| 41 | |||
| 42 | $self->_cursor(-1); | ||
| 43 | |||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub iterator { | ||
| 48 | my $class = shift; | ||
| 49 | my ($objs) = @_; | ||
| 50 | |||
| 51 | my $self = bless {}, ref $class || $class; | ||
| 52 | $self->_results($objs); | ||
| 53 | $self->_results_loaded(1); | ||
| 54 | $self->_cursor(-1); | ||
| 55 | $self->is_finished(0); | ||
| 56 | |||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub clone { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | # note that this is a SHALLOW copy--any values that are references | ||
| 64 | # will be shared between the original and the clone | ||
| 65 | my $terms = $self->_terms ? { %{$self->_terms} } : {}; | ||
| 66 | my $args = $self->_args ? { %{$self->_args} } : {}; | ||
| 67 | |||
| 68 | my $clone = $self->new({class => $self->class, | ||
| 69 | terms => $terms, | ||
| 70 | args => $args, | ||
| 71 | }); | ||
| 72 | |||
| 73 | # Pull in filtered results if they've been loaded on the original object | ||
| 74 | if ($self->_results_loaded) { | ||
| 75 | my $res = $self->_load_results; | ||
| 76 | $clone->_results([@$res]) if $res; | ||
| 77 | } | ||
| 78 | |||
| 79 | return $clone; | ||
| 80 | } | ||
| 81 | |||
| 82 | ## Public Instance Methods | ||
| 83 | |||
| 84 | sub add_constraint { | ||
| 85 | my $self = shift; | ||
| 86 | my ($terms, $args) = @_; | ||
| 87 | |||
| 88 | if ($terms) { | ||
| 89 | die "First argument to 'add_constraint' must be a hash reference" | ||
| 90 | if ref $terms ne 'HASH'; | ||
| 91 | |||
| 92 | # Get current terms and any terms we are using as a filter on existing | ||
| 93 | # result sets | ||
| 94 | my $cur_terms = $self->_terms || {}; | ||
| 95 | my $filter_terms = $self->_filter_terms || {}; | ||
| 96 | foreach my $k (keys %$terms) { | ||
| 97 | $self->_results_loaded(0) unless $cur_terms->{$k}; | ||
| 98 | $cur_terms->{$k} = $terms->{$k}; | ||
| 99 | $filter_terms->{$k} = 1 if $self->_results_loaded; | ||
| 100 | } | ||
| 101 | $self->_terms($cur_terms); | ||
| 102 | $self->_filter_terms($filter_terms) if $self->_results_loaded; | ||
| 103 | } | ||
| 104 | |||
| 105 | if ($args) { | ||
| 106 | die "Second argument to 'add_constraint' must be a hash reference" | ||
| 107 | if ref $args ne 'HASH'; | ||
| 108 | |||
| 109 | my $cur_args = $self->_args || {}; | ||
| 110 | my $filter_args = $self->_filter_args || {}; | ||
| 111 | foreach my $k (keys %$args) { | ||
| 112 | my $val = $args->{$k}; | ||
| 113 | |||
| 114 | # If we get a limit arg that is bigger than our existing limit (and | ||
| 115 | # we *have* an existing limit), then | ||
| 116 | # make sure we force a requery. Same for any filter arguments. | ||
| 117 | # Same for offset arg that is smaller than existing one. | ||
| 118 | if ((($k eq 'limit') and | ||
| 119 | ( exists $cur_args->{'limit'} && defined $cur_args->{'limit'} && ($cur_args->{'limit'}||0) < $val)) or | ||
| 120 | (($k eq 'offset') and (($cur_args->{'offset'}||0) > $val)) or | ||
| 121 | ($k eq 'filters')) { | ||
| 122 | $self->_results_loaded(0); | ||
| 123 | } | ||
| 124 | |||
| 125 | $cur_args->{$k} = $val; | ||
| 126 | $filter_args->{$k} = 1 if $self->_results_loaded; | ||
| 127 | } | ||
| 128 | $self->_args($cur_args); | ||
| 129 | $self->_filter_args($filter_args) if $self->_results_loaded; | ||
| 130 | } | ||
| 131 | |||
| 132 | return 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub clear_constraint { | ||
| 136 | my $self = shift; | ||
| 137 | my ($term_names, $arg_names) = @_; | ||
| 138 | |||
| 139 | my $terms = $self->_terms; | ||
| 140 | if ($term_names and $terms) { | ||
| 141 | die "First argument to 'clear_constraint' must be an array reference" | ||
| 142 | if ref $term_names ne 'ARRAY'; | ||
| 143 | |||
| 144 | foreach my $n (@$term_names) { | ||
| 145 | if (delete $terms->{$n}) { | ||
| 146 | $self->_results_loaded(0); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | my $args = $self->_args; | ||
| 152 | if ($arg_names and $args) { | ||
| 153 | die "Second argument to 'clear_constraint' must be an array reference" | ||
| 154 | if ref $arg_names ne 'ARRAY'; | ||
| 155 | |||
| 156 | foreach my $n (@$arg_names) { | ||
| 157 | # If we get a limit arg that is bigger than our existing limit, then | ||
| 158 | # make sure we force a requery. Same for any filter arguments. | ||
| 159 | # Same for offset arg that is smaller than existing one. | ||
| 160 | if (($n eq 'limit') and (($args->{'limit'}||0) > 0) or | ||
| 161 | ($n eq 'offset') and (($args->{'offset'}||0) > 0) or | ||
| 162 | ($n eq 'filters')) { | ||
| 163 | $self->_results_loaded(0); | ||
| 164 | } | ||
| 165 | delete $args->{$n}; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | return 1; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub add_term { shift->add_constraint($_[0]) } | ||
| 173 | sub clear_term { shift->clear_constraint(\@_) } | ||
| 174 | sub get_term { my $self = shift; | ||
| 175 | $self->_terms && $self->_terms->{$_[0]} } | ||
| 176 | |||
| 177 | sub add_limit { shift->add_constraint(undef, {limit => $_[0]}) } | ||
| 178 | sub clear_limit { shift->clear_constraint(undef, ['limit']) } | ||
| 179 | sub get_limit { my $self = shift; | ||
| 180 | $self->_args && $self->_args->{limit} } | ||
| 181 | |||
| 182 | sub add_offset { shift->add_constraint(undef, {offset => $_[0]}) } | ||
| 183 | sub clear_offset { shift->clear_constraint(undef, ['offset']) } | ||
| 184 | sub get_offset { my $self = shift; | ||
| 185 | $self->_args && $self->_args->{offset} } | ||
| 186 | |||
| 187 | sub add_order { shift->add_constraint(undef, {sort => $_[0]}) } | ||
| 188 | sub clear_order { shift->clear_constraint(undef, ['sort']) } | ||
| 189 | sub get_order { my $self = shift; | ||
| 190 | $self->_args && $self->_args->{sort} } | ||
| 191 | |||
| 192 | sub add_filters { shift->add_constraint(undef, {filters => $_[0]}) } | ||
| 193 | sub clear_filters { shift->clear_constraint(undef, ['filters']) } | ||
| 194 | sub get_filters { my $self = shift; | ||
| 195 | $self->_args && $self->_args->{filters} } | ||
| 196 | sub index { | ||
| 197 | my $self = shift; | ||
| 198 | |||
| 199 | return $self->_cursor; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub next { | ||
| 203 | my $self = shift; | ||
| 204 | |||
| 205 | return if $self->is_finished; | ||
| 206 | |||
| 207 | $self->_cursor($self->_cursor + 1); | ||
| 208 | |||
| 209 | # Load the results and return an object | ||
| 210 | my $results = $self->_load_results; | ||
| 211 | |||
| 212 | my $obj = $results->[$self->_cursor]; | ||
| 213 | |||
| 214 | if ($obj) { | ||
| 215 | return $obj; | ||
| 216 | } else { | ||
| 217 | $self->is_finished(1); | ||
| 218 | return; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | # look at next() without incrementing the cursor | ||
| 223 | # like if you just want to see what's coming down the road at you | ||
| 224 | sub peek_next { | ||
| 225 | my $self = shift; | ||
| 226 | |||
| 227 | return if $self->is_finished; | ||
| 228 | |||
| 229 | # Load the results and return an object | ||
| 230 | my $results = $self->_load_results; | ||
| 231 | |||
| 232 | my $obj = $results->[$self->_cursor + 1]; | ||
| 233 | |||
| 234 | return $obj; | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | |||
| 239 | sub prev { | ||
| 240 | my $self = shift; | ||
| 241 | |||
| 242 | $self->_cursor($self->_cursor - 1); | ||
| 243 | |||
| 244 | # Boundary check | ||
| 245 | return if $self->_cursor == -1; | ||
| 246 | |||
| 247 | # Load the results and return an object | ||
| 248 | my $results = $self->_load_results; | ||
| 249 | |||
| 250 | my $obj = $results->[$self->_cursor]; | ||
| 251 | if ($obj) { | ||
| 252 | return $obj; | ||
| 253 | } else { | ||
| 254 | return; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | sub curr { | ||
| 259 | my $self = shift; | ||
| 260 | |||
| 261 | return $self->_load_results->[$self->_cursor]; | ||
| 262 | } | ||
| 263 | |||
| 264 | sub slice { | ||
| 265 | my $self = shift; | ||
| 266 | my ($start, $end) = @_; | ||
| 267 | |||
| 268 | # Do we already have results? | ||
| 269 | if ($self->_results) { | ||
| 270 | return [ @{ $self->_results }[$start..min($self->count-1, $end)] ]; | ||
| 271 | } | ||
| 272 | |||
| 273 | my $limit = $end - $start + 1; | ||
| 274 | |||
| 275 | $self->add_offset($start); | ||
| 276 | $self->add_limit($limit); | ||
| 277 | |||
| 278 | my $r = $self->_load_results; | ||
| 279 | |||
| 280 | return $r; | ||
| 281 | } | ||
| 282 | |||
| 283 | sub all { | ||
| 284 | my $self = shift; | ||
| 285 | |||
| 286 | return unless $self->count; | ||
| 287 | |||
| 288 | my @obj; | ||
| 289 | push @obj, $self->first; | ||
| 290 | while (my $obj = $self->next) { | ||
| 291 | push @obj, $obj; | ||
| 292 | } | ||
| 293 | |||
| 294 | $self->rewind; | ||
| 295 | return @obj; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub count { | ||
| 299 | my $self = shift; | ||
| 300 | |||
| 301 | # Get/load the results if we already have them | ||
| 302 | if ($self->_results_loaded or $self->get_limit) { | ||
| 303 | my $results = $self->_load_results; | ||
| 304 | return scalar @$results; | ||
| 305 | } else { | ||
| 306 | local $Data::ObjectDriver::DEBUG = 1 if $self->dod_debug; | ||
| 307 | return $self->class->count($self->_terms, $self->_args); | ||
| 308 | } | ||
| 309 | } | ||
| 310 | |||
| 311 | sub first { | ||
| 312 | my $self = shift; | ||
| 313 | |||
| 314 | # Clear is finished in case they are coming back from the last element | ||
| 315 | $self->is_finished(0); | ||
| 316 | $self->_cursor(0); | ||
| 317 | |||
| 318 | my $results = $self->_load_results; | ||
| 319 | |||
| 320 | my $obj = $results->[$self->_cursor]; | ||
| 321 | if ($obj) { | ||
| 322 | return $obj; | ||
| 323 | } else { | ||
| 324 | return; | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | sub last { | ||
| 329 | my $self = shift; | ||
| 330 | my $results; | ||
| 331 | |||
| 332 | $results = $self->_load_results; | ||
| 333 | $self->_cursor($#$results); | ||
| 334 | |||
| 335 | return $results->[$self->_cursor]; | ||
| 336 | } | ||
| 337 | |||
| 338 | sub is_last { | ||
| 339 | my $self = shift; | ||
| 340 | my $results = $self->_load_results; | ||
| 341 | return (scalar @{$results} == $self->_cursor + 1) ? 1 : 0; | ||
| 342 | } | ||
| 343 | |||
| 344 | ## Private Instance Methods | ||
| 345 | |||
| 346 | sub _filtered_results { | ||
| 347 | my $self = shift; | ||
| 348 | my $results = $self->_results; | ||
| 349 | my $filter_terms = $self->_filter_terms; | ||
| 350 | my $filter_args = $self->_filter_args; | ||
| 351 | |||
| 352 | # Return right away if we don't have any new filters | ||
| 353 | return $results unless $filter_terms or $filter_args; | ||
| 354 | |||
| 355 | my $new_results; | ||
| 356 | |||
| 357 | # Check terms | ||
| 358 | if ($filter_terms) { | ||
| 359 | while (my $obj = shift @$results) { | ||
| 360 | push @$new_results, $obj if $self->_in_terms_filter($obj); | ||
| 361 | } | ||
| 362 | } else { | ||
| 363 | $new_results = $results; | ||
| 364 | } | ||
| 365 | |||
| 366 | # Check args | ||
| 367 | if ($filter_args) { | ||
| 368 | if ($filter_args->{sort}) { | ||
| 369 | @$new_results = sort { $self->_dod_sort($a, $b) } @$new_results; | ||
| 370 | } | ||
| 371 | |||
| 372 | # See if we've got a new limit and offset | ||
| 373 | if ($filter_args->{offset}) { | ||
| 374 | my $offset = $self->_args->{offset}; | ||
| 375 | splice @$new_results, 0, $offset; | ||
| 376 | } | ||
| 377 | |||
| 378 | if ($filter_args->{limit}) { | ||
| 379 | my $limit = $self->_args->{limit}; | ||
| 380 | if (scalar @$new_results > $limit) { | ||
| 381 | # Truncate the array | ||
| 382 | splice @$new_results, $limit, $#$new_results; | ||
| 383 | } | ||
| 384 | } | ||
| 385 | } | ||
| 386 | |||
| 387 | $self->_filter_terms(undef); | ||
| 388 | $self->_filter_args(undef); | ||
| 389 | |||
| 390 | $self->_results($new_results); | ||
| 391 | |||
| 392 | return $new_results; | ||
| 393 | } | ||
| 394 | |||
| 395 | sub _dod_sort { | ||
| 396 | my $self = shift; | ||
| 397 | my ($a, $b) = @_; | ||
| 398 | my $sort = $self->_args->{sort}; | ||
| 399 | |||
| 400 | foreach my $order_data (@$sort) { | ||
| 401 | my $col = $order_data->{column}; | ||
| 402 | my $dir = $order_data->{direction}; | ||
| 403 | my $result; | ||
| 404 | |||
| 405 | $result = ($dir eq 'ascend') ? $a->$col cmp $b->$col | ||
| 406 | : $b->$col cmp $a->$col; | ||
| 407 | |||
| 408 | # Return the sort result unless they are the same | ||
| 409 | return $result unless $result == 0; | ||
| 410 | } | ||
| 411 | |||
| 412 | return 0; | ||
| 413 | } | ||
| 414 | |||
| 415 | sub _in_terms_filter { | ||
| 416 | my $self = shift; | ||
| 417 | my ($obj) = @_; | ||
| 418 | |||
| 419 | # Always matches filter if we don't have any filters | ||
| 420 | return 1 unless $self->_filter_terms; | ||
| 421 | |||
| 422 | if ($self->_filter_terms) { | ||
| 423 | # Look through each filter | ||
| 424 | foreach my $key (keys %{$self->_filter_terms}) { | ||
| 425 | # If something doesn't match return with undef | ||
| 426 | if ($obj->$key ne $self->_terms->{$key}) { | ||
| 427 | return; | ||
| 428 | } | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 432 | return 1; | ||
| 433 | } | ||
| 434 | |||
| 435 | sub _load_results { | ||
| 436 | my $self = shift; | ||
| 437 | |||
| 438 | # If results are already loaded, see if they need to be filtered and return | ||
| 439 | # them | ||
| 440 | if ($self->_results_loaded) { | ||
| 441 | my $results = $self->_filtered_results; | ||
| 442 | |||
| 443 | return $self->_results; | ||
| 444 | } | ||
| 445 | |||
| 446 | # An iterator only ResultSet doesn't have a class (or any parameters for | ||
| 447 | # that matter) so don't try to search for anything. | ||
| 448 | return unless $self->class; | ||
| 449 | |||
| 450 | local $Data::ObjectDriver::DEBUG = 1 if $self->dod_debug; | ||
| 451 | my @r = $self->class->search($self->_terms, $self->_args); | ||
| 452 | |||
| 453 | $self->_results(\@r); | ||
| 454 | $self->_results_loaded(1); | ||
| 455 | |||
| 456 | return \@r; | ||
| 457 | } | ||
| 458 | |||
| 459 | sub rewind { | ||
| 460 | my $self = shift; | ||
| 461 | $self->is_finished(0); | ||
| 462 | $self->_cursor(-1); | ||
| 463 | return $self; | ||
| 464 | } | ||
| 465 | 1; | ||
| 466 | |||
| 467 | __END__ |