Jifty::DBI::Collection::Unique - Ensure uniqueness of records in a collection


Jifty-DBI documentation Contained in the Jifty-DBI distribution.

Index


Code Index:

add_record

Overrides add_record to ensure uniqueness.

NAME

Top

Jifty::DBI::Collection::Unique - Ensure uniqueness of records in a collection

SYNOPSIS

Top

    package Foo::Collection;
    use base 'Jifty::DBI::Collection';

    use Jifty::DBI::Collection::Unique; # mixin

    my $collection = Foo::Collection->New();
    $collection->SetupComplicatedJoins;
    $collection->OrderByMagic;

    while (my $thing = $collection->Next) {
        # $thing is going to be distinct
    }

DESCRIPTION

Top

Currently, Jifty::DBI makes exceptions for databases which cannot handle both SELECT DISTINCT and ordering in the same statement; it drops the DISTINCT requirement. This, of course, means that you can get the same row twice, which you might not want. If that's the case, use this module as a mix-in, and it will provide you with an AddRecord method which ensures that a record will not appear twice in the same search.

AUTHOR

Top

Simon Cozens.

COPYRIGHT

Top


Jifty-DBI documentation Contained in the Jifty-DBI distribution.
package Jifty::DBI::Collection::Unique;
use strict;
use warnings;

use base 'Exporter';
our @EXPORT  = qw(AddRecord);
our $VERSION = "0.01";

sub add_record {
    my $self   = shift;
    my $record = shift;

    # We're a mixin, so we can't override _CleanSlate, but if an object
    # gets reused, we need to clean ourselves out.  If there are no items,
    # we're clearly doing a new search
    $self->{"dbix_sb_unique_cache"} = {} unless ( @{ $self->{'items'} }[0] );
    return if $self->{"dbix_sb_unique_cache"}->{ $record->id }++;
    push @{ $self->{'items'} }, $record;
}

1;