App-Repository

 view release on metacpan or  search on metacpan

lib/App/SessionObject/RepositoryObjectSet.pm  view on Meta::CPAN


#############################################################################
## $Id: RepositoryObjectSet.pm 9934 2007-09-11 18:04:00Z spadkins $
#############################################################################

package App::SessionObject::RepositoryObjectSet;
$VERSION = (q$Revision: 9934 $ =~ /(\d[\d\.]*)/)[0];  # VERSION numbers generated by svn

use App;
use App::Repository;
use App::SessionObject;

@ISA = ( "App::SessionObject" );

use strict;

=head1 NAME

App::SessionObject::RepositoryObjectSet - A set of repository objects described by a set of query parameters

=head1 SYNOPSIS

    use App::SessionObject::RepositoryObjectSet;

    ...

=cut

=head1 DESCRIPTION

A RepositoryObjectSet is a set of repository objects (i.e. rows in 
a database).

By using a RepositoryObjectSet instead of simply doing a query, you get
a variety of benefits.

 * session-level caching
 * find domains of given columns (get_column_values())
 * create unique and non-unique indexes of the object set based on
   groups of columns (get_index(), get_unique_index())
 * efficiently fetch single objects within the set or subsets of objects
   which share common values in a set of attributes

=cut

###########################################################################
# Support Routines
###########################################################################

sub _init {
    &App::sub_entry if ($App::trace);
    my ($self, $args) = @_;
    $self->SUPER::_init();
    my $table   = $self->{table} || die "table not defined";
    $self->_clear_cache_if_auto_params_changed() if ($self->{auto_params});   # sets params from auto_params
    $self->_clear_cache_if_auto_columns_changed() if ($self->{auto_columns}); # sets columns from auto_columns
    if (!$self->{columns} && !$self->{temporary}) {
        my $context = $self->{context};
        my $repname = $self->{repository};
        my $rep     = $context->repository($repname);
        $self->{columns} = $rep->_get_default_columns($table);
    }
    &App::sub_exit() if ($App::trace);
}

# This should only be relevant for temporary
sub set_objects {
    &App::sub_entry if ($App::trace);
    my ($self, $objects, $columns) = @_;
    if ($self->{temporary}) {
        $self->{objects} = $objects;
        delete $self->{index};
        delete $self->{unique_index};
        delete $self->{column_values};
        delete $self->{max_age_time};
        delete $self->{ext_summary};
        delete $self->{summary};
        if ($columns) {
            $self->{columns} = $columns;
        }
        elsif (!$self->{columns} && $#$objects > -1) {
            $columns = [ sort keys %{$objects->[0]} ];
            $self->{columns} = $columns;
        }
    }
    else {
        die "set_objects() is not allowed on a non-temporary object set";
    }
    &App::sub_exit() if ($App::trace);
}

sub _clear_cache {
    &App::sub_entry if ($App::trace);
    my ($self) = @_;
    if (!$self->{temporary}) {
        delete $self->{objects};
        delete $self->{index};
        delete $self->{unique_index};
        delete $self->{column_values};
        delete $self->{max_age_time};
        delete $self->{ext_summary};
        delete $self->{summary};
    }
    &App::sub_exit() if ($App::trace);
}

sub _clear_cache_if_objects_expired {
    &App::sub_entry if ($App::trace);
    my ($self, $options) = @_;
    my $max_age = $options->{max_age};
    $max_age = $self->{max_age} if (!defined $max_age);
    if (defined $max_age && $self->{objects}) {
        my $max_age_time = $self->{max_age_time};



( run in 1.430 second using v1.01-cache-2.11-cpan-99c4e6809bf )