| 1 | package Jifty::DBI::Collection::Unique; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use base 'Exporter'; | ||
| 6 | our @EXPORT = qw(AddRecord); | ||
| 7 | our $VERSION = "0.01"; | ||
| 8 | |||
| 9 | sub add_record { | ||
| 10 | my $self = shift; | ||
| 11 | my $record = shift; | ||
| 12 | |||
| 13 | # We're a mixin, so we can't override _CleanSlate, but if an object | ||
| 14 | # gets reused, we need to clean ourselves out. If there are no items, | ||
| 15 | # we're clearly doing a new search | ||
| 16 | $self->{"dbix_sb_unique_cache"} = {} unless ( @{ $self->{'items'} }[0] ); | ||
| 17 | return if $self->{"dbix_sb_unique_cache"}->{ $record->id }++; | ||
| 18 | push @{ $self->{'items'} }, $record; | ||
| 19 | } | ||
| 20 | |||
| 21 | 1; | ||
| 22 | |||
| 23 | =head1 NAME | ||
| 24 | |||
| 25 | Jifty::DBI::Collection::Unique - Ensure uniqueness of records in a collection | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | package Foo::Collection; | ||
| 30 | use base 'Jifty::DBI::Collection'; | ||
| 31 | |||
| 32 | use Jifty::DBI::Collection::Unique; # mixin | ||
| 33 | |||
| 34 | my $collection = Foo::Collection->New(); | ||
| 35 | $collection->SetupComplicatedJoins; | ||
| 36 | $collection->OrderByMagic; | ||
| 37 | |||
| 38 | while (my $thing = $collection->Next) { | ||
| 39 | # $thing is going to be distinct | ||
| 40 | } | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | Currently, Jifty::DBI makes exceptions for databases which | ||
| 45 | cannot handle both C<SELECT DISTINCT> and ordering in the same | ||
| 46 | statement; it drops the C<DISTINCT> requirement. This, of course, means | ||
| 47 | that you can get the same row twice, which you might not want. If that's | ||
| 48 | the case, use this module as a mix-in, and it will provide you with an | ||
| 49 | C<AddRecord> method which ensures that a record will not appear twice in | ||
| 50 | the same search. | ||
| 51 | |||
| 52 | =head1 AUTHOR | ||
| 53 | |||
| 54 | Simon Cozens. | ||
| 55 | |||
| 56 | =head1 COPYRIGHT | ||
| 57 | |||
| 58 | Copyright 2005 Best Practical Solutions, LLC | ||
| 59 | |||
| 60 | This library is free software; you can redistribute it and/or modify it | ||
| 61 | under the same terms as Perl itself. | ||
| 62 | |||
| 63 | =cut | ||
| 64 |