[Dbix-class] Tracking "parent" resultset.

Rob Kinyon rob.kinyon at gmail.com
Sun May 16 23:26:02 GMT 2010


On Sun, May 16, 2010 at 14:30, Bill Moseley <moseley at hank.org> wrote:
> In another thread about chaining resultsets and keeping previous sets around
> for testing Matt noted:
>>
>> ... if you keep all the intermediate resultsets around in the stash or
>>
>> wherever (or better still refactor to resultset methods and have your
>>
>> custom resultset class keep the intermediate resultsets around) then it
>>
>> seems like it should be pretty easy to walk back up the chain making
>>
>> the extra queries until one returns data - at which point you know the
>> last
>>
>> step you walked back up is the problem child for this request.
>
> Ok, it's a foggy Sunday morning I'll give that a try.
> In review, what I want to do is:
> $rs = $schema->resultset( 'Cd' );
> $rs = $rs->search( { year => 2010 }, { comment => 'Year not 2010' } );
> $rs = $rs->search( { position => { '>', 5 }}, { comment => 'None more than
> 5th position' } );
> $rs = $rs->search( { deleted => 0 }, { comment => 'None deleted' } );
> $failed_commet = $rs->reason_failed if $rs == 0;
> In other words, have each $rs know its parent result set and be able to walk
> up (or down, might be faster) the list of result sets to find out what
> condition returned no results.
> Is this a safe-n-sane approach for tracking parents?  Does result_rs catch
> all times a new $rs might be created?  Or do I need to override new()?
> In my ResultSet base class:
> has 'parent' => ( is => 'rw' );
> has 'comment' => ( is => 'rw' );
> around 'search_rs' => sub {
>     my ( $orig, $rs, $cond, $attr,  @rest ) = @_;
>     my $comment = delete $attr->{comment};
>     my $new_rs = $rs->$orig( $cond, $attr, @rest );
>     $new_rs->parent( $rs ) if $rs->{cond};
>     $new_rs->comment( $comment ) if $comment;
>     return $new_rs;
> };

A given resultset can have more than one "parent." There are two
obvious scenarios - subqueries (with the as_query feature) and unions
(via DBIx::Class::Helpers). Just keep track of it on your own using an
array. :)

Rob



More information about the DBIx-Class mailing list