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