| 1 | package Jifty::DBI::Collection::Union; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # WARNING --- This is still development code. It is experimental. | ||
| 6 | |||
| 7 | our $VERSION = '0'; | ||
| 8 | |||
| 9 | # This could inherit from Jifty::DBI, but there are _a lot_ | ||
| 10 | # of things in Jifty::DBI that we don't want, like Limit and | ||
| 11 | # stuff. It probably makes sense to (eventually) split out | ||
| 12 | # Jifty::DBI::Collection to contain all the iterator logic. | ||
| 13 | # This could inherit from that. | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Jifty::DBI::Collection::Union - Deal with multiple L<Jifty::DBI::Collection> | ||
| 18 | result sets as one | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use Jifty::DBI::Collection::Union; | ||
| 23 | my $U = new Jifty::DBI::Collection::Union; | ||
| 24 | $U->add( $tickets1 ); | ||
| 25 | $U->add( $tickets2 ); | ||
| 26 | |||
| 27 | $U->GotoFirstItem; | ||
| 28 | while (my $z = $U->Next) { | ||
| 29 | printf "%5d %30.30s\n", $z->Id, $z->Subject; | ||
| 30 | } | ||
| 31 | |||
| 32 | =head1 WARNING | ||
| 33 | |||
| 34 | This module is still experimental. | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | Implements a subset of the L<Jifty::DBI::Collection> methods, but | ||
| 39 | enough to do iteration over a bunch of results. Useful for displaying | ||
| 40 | the results of two unrelated searches (for the same kind of objects) | ||
| 41 | in a single list. | ||
| 42 | |||
| 43 | =head1 METHODS | ||
| 44 | |||
| 45 | =head2 new | ||
| 46 | |||
| 47 | Create a new L<Jifty::DBI::Collection::Union> object. No arguments. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub new { | ||
| 52 | bless { | ||
| 53 | data => [], | ||
| 54 | curp => 0, # current offset in data | ||
| 55 | item => 0, # number of indiv items from First | ||
| 56 | count => undef, | ||
| 57 | }, | ||
| 58 | shift; | ||
| 59 | } | ||
| 60 | |||
| 61 | =head2 add COLLECTION | ||
| 62 | |||
| 63 | Add L<Jifty::DBI::Collection> object I<COLLECTION> to the Union | ||
| 64 | object. | ||
| 65 | |||
| 66 | It must be the same type as the first object added. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub add { | ||
| 71 | my $self = shift; | ||
| 72 | my $newobj = shift; | ||
| 73 | |||
| 74 | unless ( @{ $self->{data} } == 0 | ||
| 75 | || ref($newobj) eq ref( $self->{data}[0] ) ) | ||
| 76 | { | ||
| 77 | die | ||
| 78 | "All elements of a Jifty::DBI::Collection::Union must be of the same type. Looking for a " | ||
| 79 | . ref( $self->{data}[0] ) . "."; | ||
| 80 | } | ||
| 81 | |||
| 82 | $self->{count} = undef; | ||
| 83 | push @{ $self->{data} }, $newobj; | ||
| 84 | } | ||
| 85 | |||
| 86 | =head2 first | ||
| 87 | |||
| 88 | Return the very first element of the Union (which is the first element | ||
| 89 | of the first Collection). Also reset the current pointer to that | ||
| 90 | element. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub first { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | die "No elements in Jifty::DBI::Collection::Union" | ||
| 98 | unless @{ $self->{data} }; | ||
| 99 | |||
| 100 | $self->{curp} = 0; | ||
| 101 | $self->{item} = 0; | ||
| 102 | $self->{data}[0]->First; | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 next | ||
| 106 | |||
| 107 | Return the next element in the Union. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub next { | ||
| 112 | my $self = shift; | ||
| 113 | |||
| 114 | return undef unless defined $self->{data}[ $self->{curp} ]; | ||
| 115 | |||
| 116 | my $cur = $self->{data}[ $self->{curp} ]; | ||
| 117 | if ( $cur->_items_counter == $cur->count ) { | ||
| 118 | |||
| 119 | # move to the next element | ||
| 120 | $self->{curp}++; | ||
| 121 | return undef unless defined $self->{data}[ $self->{curp} ]; | ||
| 122 | $cur = $self->{data}[ $self->{curp} ]; | ||
| 123 | $self->{data}[ $self->{curp} ]->goto_first_item; | ||
| 124 | } | ||
| 125 | $self->{item}++; | ||
| 126 | $cur->next; | ||
| 127 | } | ||
| 128 | |||
| 129 | =head2 last | ||
| 130 | |||
| 131 | Returns the last item | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub last { | ||
| 136 | die "Last doesn't work right now"; | ||
| 137 | my $self = shift; | ||
| 138 | $self->goto_item( ( $self->count ) - 1 ); | ||
| 139 | return ( $self->next ); | ||
| 140 | } | ||
| 141 | |||
| 142 | =head2 count | ||
| 143 | |||
| 144 | Returns the total number of elements in the Union'ed Collection | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub count { | ||
| 149 | my $self = shift; | ||
| 150 | my $sum = 0; | ||
| 151 | |||
| 152 | # cache the results | ||
| 153 | return $self->{count} if defined $self->{count}; | ||
| 154 | |||
| 155 | $sum += $_->count for ( @{ $self->{data} } ); | ||
| 156 | |||
| 157 | $self->{count} = $sum; | ||
| 158 | |||
| 159 | return $sum; | ||
| 160 | } | ||
| 161 | |||
| 162 | =head2 goto_first_item | ||
| 163 | |||
| 164 | Starts the recordset counter over from the first item. the next time | ||
| 165 | you call L</next>, you'll get the first item returned by the database, | ||
| 166 | as if you'd just started iterating through the result set. | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub goto_first_item { | ||
| 171 | my $self = shift; | ||
| 172 | $self->goto_item(0); | ||
| 173 | } | ||
| 174 | |||
| 175 | sub goto_item { | ||
| 176 | my $self = shift; | ||
| 177 | my $item = shift; | ||
| 178 | |||
| 179 | die "We currently only support going to the First item" | ||
| 180 | unless $item == 0; | ||
| 181 | |||
| 182 | $self->{curp} = 0; | ||
| 183 | $self->{item} = 0; | ||
| 184 | $self->{data}[0]->goto_item(0); | ||
| 185 | |||
| 186 | return $item; | ||
| 187 | } | ||
| 188 | |||
| 189 | =head2 is_last | ||
| 190 | |||
| 191 | Returns true if the current row is the last record in the set. | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | sub is_last { | ||
| 196 | my $self = shift; | ||
| 197 | |||
| 198 | $self->{item} == $self->count ? 1 : undef; | ||
| 199 | } | ||
| 200 | |||
| 201 | =head2 items_array_ref | ||
| 202 | |||
| 203 | Return a refernece to an array containing all objects found by this search. | ||
| 204 | |||
| 205 | Will destroy any positional state. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub items_array_ref { | ||
| 210 | my $self = shift; | ||
| 211 | |||
| 212 | return [] unless $self->count; | ||
| 213 | |||
| 214 | $self->goto_first_item(); | ||
| 215 | my @ret; | ||
| 216 | while ( my $r = $self->next ) { | ||
| 217 | push @ret, $r; | ||
| 218 | } | ||
| 219 | |||
| 220 | return \@ret; | ||
| 221 | } | ||
| 222 | |||
| 223 | =head1 AUTHOR | ||
| 224 | |||
| 225 | Copyright (c) 2004 Robert Spier | ||
| 226 | |||
| 227 | All rights reserved. | ||
| 228 | |||
| 229 | This library is free software; you can redistribute it | ||
| 230 | and/or modify it under the same terms as Perl itself. | ||
| 231 | |||
| 232 | =head1 SEE ALSO | ||
| 233 | |||
| 234 | L<Jifty::DBI>, L<Jifty::DBI::Collection> | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | 1; | ||
| 239 | |||
| 240 | __END__ | ||
| 241 |