DBI/Collection/Union.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 111 CC 11 SCAR 23.6
CC
11
Cov
0%
next
30.0
add
6.0
first
6.0
CRAP 10.5
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  
118 # do the search to avoid the count query and then search
119 $cur->_do_search if $cur->{'must_redo_search'};
120  
121 if ( $cur->_items_counter == $cur->count ) {
122  
123 # move to the next element
124 $self->{curp}++;
125 return undef unless defined $self->{data}[ $self->{curp} ];
126 $cur = $self->{data}[ $self->{curp} ];
127 $self->{data}[ $self->{curp} ]->goto_first_item;
128 }
129 $self->{item}++;
130 $cur->next;
131 }
132  
133 =head2 last
134  
135 Returns the last item
136  
137 =cut
138  
139 sub last {
140 die "Last doesn't work right now";
141 my $self = shift;
142 $self->goto_item( ( $self->count ) - 1 );
143 return ( $self->next );
144 }
145  
146 =head2 count
147  
148 Returns the total number of elements in the union collection
149  
150 =cut
151  
152 sub count {
153 my $self = shift;
154 my $sum = 0;
155  
156 # cache the results
157 return $self->{count} if defined $self->{count};
158  
159 $sum += $_->count for ( @{ $self->{data} } );
160  
161 $self->{count} = $sum;
162  
163 return $sum;
164 }
165  
166 =head2 goto_first_item
167  
168 Starts the recordset counter over from the first item. the next time
169 you call L</next>, you'll get the first item returned by the database,
170 as if you'd just started iterating through the result set.
171  
172 =cut
173  
174 sub goto_first_item {
175 my $self = shift;
176 $self->goto_item(0);
177 }
178  
179 =head2 goto_item
180  
181 Unlike L<Jifty::DBI::Collection/goto_item>, Union only supports going to the
182 first item in the collection.
183  
184 =cut
185  
186 sub goto_item {
187 my $self = shift;
188 my $item = shift;
189  
190 die "We currently only support going to the First item"
191 unless $item == 0;
192  
193 $self->{curp} = 0;
194 $self->{item} = 0;
195 $self->{data}[0]->goto_item(0);
196  
197 return $item;
198 }
199  
200 =head2 is_last
201  
202 Returns true if the current row is the last record in the set.
203  
204 =cut
205  
206 sub is_last {
207 my $self = shift;
208  
209 $self->{item} == $self->count ? 1 : undef;
210 }
211  
212 =head2 items_array_ref
213  
214 Return a reference to an array containing all objects found by this search.
215  
216 Will destroy any positional state.
217  
218 =cut
219  
220 sub items_array_ref {
221 my $self = shift;
222  
223 return [] unless $self->count;
224  
225 $self->goto_first_item();
226 my @ret;
227 while ( my $r = $self->next ) {
228 push @ret, $r;
229 }
230  
231 return \@ret;
232 }
233  
234 =head1 AUTHOR
235  
236 Copyright (c) 2004 Robert Spier
237  
238 All rights reserved.
239  
240 This library is free software; you can redistribute it
241 and/or modify it under the same terms as Perl itself.
242  
243 =head1 SEE ALSO
244  
245 L<Jifty::DBI>, L<Jifty::DBI::Collection>
246  
247 =cut
248  
249 1;
250  
251 __END__
252