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