--- /dev/null
+package DBIx::Data::Collection::Set::Mapped;
+
+use strictures 1;
+
+use DBIx::Data::Stream::Mapped;
+
+sub new {
+ my $proto = shift;
+ bless({ %{$_[0]} }, ref($proto)||$proto);
+}
+
+sub _inner { shift->{inner} }
+sub _mapper { shift->{mapper} }
+
+sub flatten {
+ my ($self) = @_;
+ map $self->_do_map($_), $self->_inner->flatten;
+}
+
+sub to_stream {
+ my ($self) = @_;
+ my $mapper = $self->_mapper;
+ DBIx::Data::Stream::Mapped->new({
+ inner => $self->_inner->to_stream,
+ mapper => $self->_mapper
+ });
+}
+
+sub get {
+ my ($self, $spec) = @_;
+ if (my $got = $self->_inner->get($spec)) {
+ return $self->_do_map($got);
+ }
+ return undef;
+}
+
+sub _do_map {
+ my ($self, $to_map) = @_;
+ local $_ = $to_map;
+ $self->_mapper->($to_map);
+}
+
+1;
sub get {
my ($self, $spec) = @_;
my $inflator = $self->_inflator;
- $inflator->inflate(
- $self->_inner->get(
- $inflator->deflate_spec($spec)
- )
- );
+ if (my $got = $self->_inner->get($inflator->deflate_spec($spec))) {
+ return $inflator->inflate($got);
+ }
+ return undef;
}
sub replace {
$self->_inner->remove($self->_inflator->deflate_spec($spec));
}
+sub map {
+ require DBIx::Data::Collection::Set::Mapped;
+ DBIx::Data::Collection::Set::Mapped->new({
+ inner => $_[0], mapper => $_[1]
+ });
+}
+
1;
use strictures 1;
+use DBIx::Data::Stream::STH;
+
sub new {
my $proto = shift;
bless({ %{$_[0]} }, ref($proto)||$proto);
sub _sql { shift->{sql} }
sub _raw { shift->{raw} }
+sub _append_args { shift->{append_args} }
sub _run {
my $self = shift;
- my ($run_type, $sql_type, @args) = @_;
+ my ($run_type, $sql_type, $args) = @_;
my $sql = $self->_sql->{$sql_type}||die "No such sql type ${sql_type}";
- $self->_raw->${\"run_${run_type}"}($sql, @args);
+ if (my $append = $self->_append_args) {
+ $args = [ @{$args||[]}, @$append ];
+ }
+ $self->_raw->${\"run_${run_type}"}($sql, $args);
}
sub flatten { @{shift->_run('rowset', 'select_all', @_)} }
my $sth = (my $self = shift)->_sth;
return unless $sth;
# {Active} only means that there *may* be more results to fetch
- if ($sth->{Active} and my @next = $self->sth->fetchrow_array) {
- my %next; @next{@{$self->_column_order}} = @next;
- return \%next;
+ if ($sth->{Active} and my @next = $sth->fetchrow_array) {
+ return [ @next ];
}
$sth->finish;
# prepare_cached might recycle it now we're finished so get rid of it
map mkobj($_), [1,'Robert'],[2,'Joe'],[4,'Jim']
], 'Three members left');
+ is_deeply([$store->get({ id => 3 })], [undef], 'Retrieve nonexistent');
+
done_testing;
}