| DBIx-SQLEngine documentation | Contained in the DBIx-SQLEngine distribution. |
DBIx::SQLEngine::RecordSet::Set - Array of Record Objects
use DBIx::SQLEngine::RecordSet::Set;
$record_set = DBIx::SQLEngine::RecordSet::Set->new( @records );
$record_set = $record_class->fetch_select( criteria => { status => 2 } );
print "Found " . $record_set->count() . " records";
$record_set->filter( { 'status' => 'New' } );
$record_set->sort( 'creation_date' );
foreach ( 0 .. $record_set->count() ) {
print $record_set->record( $_ )->name();
}
foreach ( $record_set->range_records( 11, 20 ) ) {
print $_->name();
}
This package is not yet complete.
The base implementation of RecordSet is an array of Record references.
$class->new ( @records ) : $recordset
Array constructor.
$recordset->clone() : $recordset
Create a shallow copy of the record set.
$recordset->init ( @records )
Array content setter.
$rs->records() : @records
Array content accessor.
$count = $rs->count();
Returns the number of records in this set.
$record = $rs->record( $position );
Return the record in the indicated position in the array. Returns nothing if position is undefined.
Indexes start with zero. Negative indexes are counted back from the end, with -1 being the last, -2 being the one before that, and so forth.
$record = $rs->last_record();
Return the last record in the array.
$clone = $rs->range_set( $start_pos, $stop_pos );
Return a copy of the current set containing only those records at or between the start and stop positions.
@records = $rs->range_records( $start_pos, $stop_pos );
Return the records at or between the start and stop positions.
Use of these methods requires the Data::Sorting module from CPAN.
See Data::Sorting for more information.
$rs->sort( @fieldnames );
Sort the contents of the set.
$clone = $rs->sorted_set( @fieldnames );
Return a sorted copy of the current set.
@records = $rs->sorted_records( @fieldnames );
Return the records from the current set, in sorted order.
Caution: This set of methods is currently not working.
$rs->filter( $criteria );
Remove non-matching records from the set.
$clone = $rs->filtered_set( $criteria );
Return a set containing only the matching records from the current set.
@records = $rs->filtered_records( $criteria );
Return the matching records from the current set.
See DBIx::SQLEngine for the overall interface and developer documentation.
See DBIx::SQLEngine::Docs::ReadMe for general information about this distribution, including installation and license information.
| DBIx-SQLEngine documentation | Contained in the DBIx-SQLEngine distribution. |
######################################################################## package DBIx::SQLEngine::RecordSet::Set; use strict; ######################################################################## ########################################################################
# $rs = DBIx::SQLEngine::RecordSet::Set->new( @records ); sub new { my $callee = shift; my $package = ref $callee || $callee; my $set = bless [], $package; $set->init( @_ ); return $set; } sub clone { my $self = shift; $self->new( @$self ); } ########################################################################
# $rs->init( @records ); sub init { my $self = shift; @$self = ( scalar @_ == 1 and ref($_[0]) eq 'ARRAY' ) ? @{ $_[0] } : @_; } # @records = $rs->records(); sub records { my $records = shift; @$records } ######################################################################## ########################################################################
# $count = $rs->count(); sub count { my $self = shift; scalar @$self; } # $record = $rs->record( $position ); sub record { my $self = shift; my $position = shift; return unless ( defined $position and length $position ); $position += $self->count if ( $position < 0 ); return unless ( $position !~ /\D/ and $position <= $#$self); $self->[ $position ]; } # @records = $rs->get_records( @positions ); sub get_records { my $self = shift; map { $self->record( $_ ) } @_ } ######################################################################## # $record = $rs->last_record(); sub last_record { my $self = shift; return unless $self->count; $self->record( $self->count - 1 ); } ######################################################################## ########################################################################
# $clone = $rs->range_set( $start_pos, $stop_pos ); sub range_set { my $self = shift; my ( $start, $end ) = @_; if ( $start < 0 ) { $start = 0 } if ( $end > $#$self ) { $end = $#$self } $self->new( $self->get_records( $start .. $end ) ); } # @records = $rs->range_records( $start_pos, $stop_pos ); sub range_records { my $self = shift; my ( $start, $end ) = @_; if ( $start < 0 ) { $start = 0 } if ( $end > $#$self ) { $end = $#$self } $self->get_records( $start .. $end ) } ######################################################################## ########################################################################
# $rs->sort( @fieldnames ); sub sort { my $self = shift; local @_ = @{ $_[0] } if ( scalar @_ == 1 and ref $_[0] eq 'ARRAY' ); require Data::Sorting; Data::Sorting::sort_array(@$self, @_); } # $clone = $rs->sorted_set( @fieldnames ); sub sorted_set { my $self = shift; my $clone = $self->clone(); $clone->sort( @_ ); return $clone; } # @records = $rs->sorted_records( @fieldnames ); sub sorted_records { my $self = shift; my $clone = $self->clone(); $clone->sort( @_ ); $clone->records(); } ######################################################################## sub reverse { my $rs = shift; @$rs = reverse @$rs; } ######################################################################## ########################################################################
use DBIx::SQLEngine::Criteria qw( new_group_from_values ); # $rs->filter( $criteria ); sub filter { my $self = shift; my $criteria = shift or return; if (ref $criteria eq 'ARRAY') { $criteria = new_group_from_values(@$criteria); } elsif (ref $criteria eq 'HASH') { $criteria = DBIx::SQLEngine::Criteria->new_from_hashref($criteria); } elsif (ref $criteria eq 'CODE') { @$self = grep { $criteria->( $_ ) } @$self; return; } @$self = $criteria->matchers($self); } # $clone = $rs->filtered_set( $criteria ); sub filtered_set { my $self = shift; my $clone = $self->clone(); $clone->filter( @_ ); return $clone; } # @records = $rs->filtered_records( $criteria ); sub filtered_records { my $self = shift; my $clone = $self->clone(); $clone->filter( @_ ); $clone->records(); } ######################################################################## ######################################################################## # @results = $rs->visit_sub( $subref, @$leading_args, @$trailing_args ); sub visit_sub { my $rs = shift; my $subref = shift; my @pre_args = map { ref($_) ? @$_ : defined($_) ? $_ : () } shift; my @post_args = map { ref($_) ? @$_ : defined($_) ? $_ : () } shift; my @result; foreach my $record ( $rs->records ) { push @result, $subref->( @pre_args, $record, @post_args ) } return @result; } ######################################################################## # $numeric = $rs->sum( $fieldname ); sub sum { my $rs = shift; my $field = shift; my $sum = 0; foreach ( $rs->visit_sub( sub { ( shift )->$field() } ) ) { $sum += $_; } return $sum; } ######################################################################## ######################################################################## # $rs->add_records( @records ); sub add_records { my $rs = shift; my %record_ids = map { $_->id => 1 } $rs->records; push @$rs, grep { ! ( $record_ids{ $_->id } ++ ) } @_ } ######################################################################## ########################################################################
######################################################################## 1;