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