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