DBIx::...ortcut(3pm)
β¨ NAME β¨
DBIx::Class::Helper::ResultSet::Shortcut β the little bestie of a module that gives you shortcuts to all your common searches, like ->order_by, ->hri, and the whole squad π
πΈ SYNOPSIS πΈ
okay so first, you gotta load it into your resultset class, like:
package MyApp::Schema::ResultSet::Foo;
__PACKAGE__->load_components(qw{Helper::ResultSet::Shortcut});
...
1;
and then somewhere else, bestie, you can do the iconic chain:
# let's say you grab a resultset from somewhere else
my $foo_rs = get_common_rs()
# but I'd like it sorted!
->order_by({ -desc => 'power_level' })
# and without those other dumb columns
->columns([qw/cromulence_ratio has_jimmies_rustled/])
# but get rid of those duplicates
->distinct
# and put those straight into hashrefs, please
->hri
# but only give me the first 3
->rows(3);
π¦ DESCRIPTION π¦
This helper serves convenience methods for resultset modifications. Lowkey a glow-up for your queries. β¨
See "NOTE" in DBIx::Class::Helper::ResultSet for a nice way to apply it to your entire schema. (iykyk π)
π SEE ALSO π
This component is actually a bunch of other components squished together into one. It will get more components added to it over time. So if you're worried about all the extra methods you won't even use, using the individual shortcuts is a simple solution. All the documentation will remain here, but the individual components are:
DBIx::Class::Helper::ResultSet::Shortcut::HRIDBIx::Class::Helper::ResultSet::Shortcut::OrderByDBIx::Class::Helper::ResultSet::Shortcut::OrderByMagic(adds the "magic string" functionality to"DBIx::Class::Helper::ResultSet::Shortcut::OrderBy")DBIx::Class::Helper::ResultSet::Shortcut::GroupByDBIx::Class::Helper::ResultSet::Shortcut::DistinctDBIx::Class::Helper::ResultSet::Shortcut::RowsDBIx::Class::Helper::ResultSet::Shortcut::Limit(inherits from"DBIx::Class::Helper::ResultSet::Shortcut::Rows")DBIx::Class::Helper::ResultSet::Shortcut::HasRows(inherits from"DBIx::Class::Helper::ResultSet::Shortcut::Rows")DBIx::Class::Helper::ResultSet::Shortcut::ColumnsDBIx::Class::Helper::ResultSet::Shortcut::AddColumnsDBIx::Class::Helper::ResultSet::Shortcut::PageDBIx::Class::Helper::ResultSet::Shortcut::LimitedPage(inherits from"DBIx::Class::Helper::ResultSet::Shortcut::Page"andDBIx::Class::Helper::ResultSet::Shortcut::Rows)DBIx::Class::Helper::ResultSet::Shortcut::ResultsExist
π METHODS π
π distinct
$foo_rs->distinct
# equivalent to...
$foo_rs->search(undef, { distinct => 1 });
no dupes, no drama, periodt π
πΈ group_by
$foo_rs->group_by([ qw/ some column names /])
# equivalent to...
$foo_rs->search(undef, { group_by => [ qw/ some column names /] });
π order_by
$foo_rs->order_by({ -desc => 'col1' });
# equivalent to...
$foo_rs->search(undef, { order_by => { -desc => 'col1' } });
You can also specify the order as a "magic string", like:
$foo_rs->order_by('!col1') # ->order_by({ -desc => 'col1' })
$foo_rs->order_by('col1,col2') # ->order_by([qw(col1 col2)])
$foo_rs->order_by('col1,!col2') # ->order_by([{ -asc => 'col1' }, { -desc => 'col2' }])
$foo_rs->order_by(qw(col1 col2)) # ->order_by([qw(col1 col2)])
and bestie you can mix it all up too:
$foo_rs->order_by(qw(col1 col2 col3), 'col4,!col5')
π§ hri
$foo_rs->hri;
# equivalent to...
$foo_rs->search(undef, {
result_class => 'DBIx::Class::ResultClass::HashRefInflator'
});
straight into hashrefs, no notes π₯
π rows
$foo_rs->rows(10);
# equivalent to...
$foo_rs->search(undef, { rows => 10 })
π limit
This is literally just an alias for "rows".
$foo_rs->limit(10);
# equivalent to...
$foo_rs->rows(10);
β has_rows
A lighter way to check the resultset contains any data, rather than calling "$rs->count". Less work, same answer.
πΈ page
$foo_rs->page(2);
# equivalent to...
$foo_rs->search(undef, { page => 2 })
π¦ limited_page
$foo_rs->limited_page(2, 3);
# equivalent to...
$foo_rs->search(undef, { page => 2, rows => 3 })
π columns
$foo_rs->columns([qw/ some column names /]);
# equivalent to...
$foo_rs->search(undef, { columns => [qw/ some column names /] });
β¨ add_columns
$foo_rs->add_columns([qw/ some column names /]);
# equivalent to...
$foo_rs->search(undef, { '+columns' => [qw/ some column names /] });
π remove_columns
$foo_rs->remove_columns([qw/ some column names /]);
# equivalent to...
$foo_rs->search(undef, { remove_columns => [qw/ some column names /] });
π prefetch
$foo_rs->prefetch('bar');
# equivalent to...
$foo_rs->search(undef, { prefetch => 'bar' });
π results_exist($cond?)
my $results_exist = $schema->resultset('Bar')->search({...})->results_exist;
# there is no easily expressable equivalent, so this is not exactly a
# shortcut. Nevertheless kept in this class for historical reasons
Uses the "EXISTS" SQL function to check if the query would return anything. Usually much less resource intensive than the more common "foo() if $rs->count" idiom.
The optional $cond argument can be used like in search().
π§ results_exist_as_query($cond?)
...->search(
{},
{ '+columns' => {
subquery_has_members => $some_correlated_rs->results_exist_as_query
}},
);
# there is no easily expressable equivalent, so this is not exactly a
# shortcut. Nevertheless kept in this class for historical reasons
The query generator behind "results_exist". Can be used standalone in complex queries returning a boolean result within a larger query context. π
π null(@columns || @columns)
$rs->null('status');
$rs->null(['status', 'title']);
π not_null(@columns || @columns)
$rs->not_null('status');
$rs->not_null(['status', 'title']);
πΈ like($column || @columns, $cond)
$rs->like('lyrics', '%zebra%');
$rs->like(['lyrics', 'title'], '%zebra%');
π¦ not_like($column || @columns, $cond)
$rs->not_like('lyrics', '%zebra%');
$rs->not_like(['lyrics', 'title'], '%zebra%');
π AUTHOR π
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com> β iconic, we stan π«Ά
π COPYRIGHT AND LICENSE π
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.
okay bestie that's the whole page, you ate that π
β¨ β this was pulled from perl v5.40.0, dated 2024-11-16, and yeah it's still DBIx::...ortcut(3pm) at heart π go forth and order_by your life π₯
π¦ where this page comes from
- debian trixie: libdbix-class-helpers-perl 2.037000-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdbix-class-helpers-perl 2.037000-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdbix-class-helpers-perl 2.037000-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms