DBIx::...rations(3pm)

πŸ“¦
version 2.037000 Β· 5 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

DBIx::Class::Helper::ResultSet::SetOperations β€” the bestie that lets you do set operations with DBIx::Class. That's it, that's the whole vibe. πŸ¦‹

This page is from the User Contributed Perl Documentation collection, iykyk.


✨ SYNOPSIS ✨

okay so first you gotta load the component into your ResultSet class, like:

package MyApp::Schema::ResultSet::Foo;

__PACKAGE__->load_components(qw{Helper::ResultSet::SetOperations});

...

1;

and then elsewhere, like literally in a controller, you go:

my $rs1 = $rs->search({ foo => 'bar' });
my $rs2 = $rs->search({ baz => 'biff' });
for ($rs1->union($rs2)->all) { ... }

and boom πŸ’₯ set operations just work. periodt.


🌸 DESCRIPTION 🌸

This component lets you use various set operations with your ResultSets. Want a nice way to apply it to your <em>entire</em> schema? Peep "NOTE" in DBIx::Class::Helper::ResultSet. πŸ‘‘

Heads up bestie: this component throws exceptions if your ResultSets have different ResultClasses or different "Columns Specs." No cap.

The whole idea, fr fr: in SQL, if you use a set operation, they must be selecting the same column names, so all the results match up. And the deal with the ResultClasses is that DBIC needs to inflate the results <em>the same way</em> for the entire ResultSet β€” so if you tried to apply something like a union on a table with the same column name but <em>different</em> classes, DBIC would not be doing what you'd expect. πŸ’€

A really cute way to use this is with DBIx::Class::ResultClass::HashRefInflator. πŸŽ€

You might have something like this lil sketch autocompletion code:

my $rs1 = $schema->resultset('Album')->search({
   name => { -like => "$input%" }
}, {
  columns => [qw( id name ), {
     tablename => \['?', [{} => 'album']],
  }],
});

my $rs2 = $schema->resultset('Artist')->search({
   name => { -like => "$input%" }
}, {
  columns => [qw( id name ), {
     tablename => \['?', [{} => 'artist']],
  }],
});

my $rs3 = $schema->resultset('Song')->search({
   name => { -like => "$input%" }
}, {
  columns => [qw( id name ), {
     tablename => \['?', [{} => 'song']],
  }],
});

$_->result_class('DBIx::Class::ResultClass::HashRefInflator')
  for ($rs1, $rs2, $rs3);

my $data = [$rs1->union([$rs2, $rs3])->all];

ate. πŸ’…


πŸ’… METHODS πŸ’…

πŸ¦‹ union

πŸ¦‹ union_all

πŸ¦‹ intersect

πŸ¦‹ intersect_all

πŸ¦‹ except

πŸ¦‹ except_all

All of these methods take a single ResultSet or an ArrayRef of ResultSets β€” and that's the only parameter, bestie. One arg, that's the whole meal. πŸ“

Oracle girlies listen up: on Oracle, except will issue a MINUS operation. Not a drill. πŸ”₯

πŸ‘‘ AUTHOR πŸ‘‘

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com> β€” the icon, the moment, the main character. 🫢


This software is copyright (c) 2024 by Arthur Axel "fREW" Schmidt. 🌈

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. slay.


perl v5.40.0 Β· 2024-11-16 β€” that's the version + date stamp from the footer, mwah πŸ’– thanks for reading, bestie, go union some ResultSets, you got this ✨🌸🧚

πŸ“¦ where this page comes from