[Dbix-class] Merging ResultSets

Vladimir Melnik v.melnik at uplink.ua
Tue Apr 18 12:10:54 GMT 2017


Hello,

Is that possible to merge multiple ResultSets into one? For example, I’ve got 3 different ResultSets (as the yields of 3 different SQL-queries), but I’m pretty sure that they are based on the same class. Yes, I can work with their Results as with elements of a list, but sometimes it would be much more complicated to work with them as with a single ResultSet. Is that possible?

In other words, here's what I have to do now:
	my $rs1 = $schema->resultset('Foo')->search({ bar => 1 });
	my $rs2 = $schema->resultset('Foo')->search({ bar => 2 });
	my $rs3 = $schema->resultset('Foo')->search({ bar => 3 });
	foreach my $rs ($rs1, $rs2, $rs3) {
	    foreach my $r ($rs->all) {
		$r->do_something;
	    }
	}

...and here’s what I’d like to do:
	my $rs1 = $schema->resultset('Foo')->search({ bar => 1 });
	my $rs2 = $schema->resultset('Foo')->search({ bar => 2 });
	my $rs3 = $schema->resultset('Foo')->search({ bar => 3 });
	my $rs_merged = ...; # <-- Something cool happens here
	foreach ($rs_merged->all) {
	    $r->do_something;
	}

...maybe something like that?
	my $rs1 = $schema->resultset('Foo')->search({ bar => 1 });
	my $rs2 = $schema->resultset('Foo')->search({ bar => 2 });
	my $rs3 = $schema->resultset('Foo')->search({ bar => 3 });
	my $rs_merged = $schema->resultset('Foo');
	foreach my $rs ($rs1, $rs2, $rs3) {
	    foreach my $r ($rs->all) {
		$rs_merged->new_result($r->get_columns)
	    }
	}
	foreach ($rs_merged->all) {
	    $r->do_something;
	}

Thanks in advance for any hints and clues!

-- 
V.Melnik



More information about the DBIx-Class mailing list