File Coverage

blib/lib/DBIx/Class/Helper/ResultSet/Shortcut.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::ResultSet::Shortcut;
2             $DBIx::Class::Helper::ResultSet::Shortcut::VERSION = '2.034002';
3             # ABSTRACT: Shortcuts to common searches (->order_by, etc)
4              
5 56     56   86120 use strict;
  56         113  
  56         1398  
6 56     56   240 use warnings;
  56         100  
  56         2052  
7              
8 56         275 use parent (qw(
9             DBIx::Class::Helper::ResultSet::Shortcut::AddColumns
10             DBIx::Class::Helper::ResultSet::Shortcut::Columns
11             DBIx::Class::Helper::ResultSet::Shortcut::Distinct
12             DBIx::Class::Helper::ResultSet::Shortcut::GroupBy
13             DBIx::Class::Helper::ResultSet::Shortcut::HasRows
14             DBIx::Class::Helper::ResultSet::Shortcut::HRI
15             DBIx::Class::Helper::ResultSet::Shortcut::Limit
16             DBIx::Class::Helper::ResultSet::Shortcut::OrderByMagic
17             DBIx::Class::Helper::ResultSet::Shortcut::Prefetch
18             DBIx::Class::Helper::ResultSet::Shortcut::LimitedPage
19             DBIx::Class::Helper::ResultSet::Shortcut::ResultsExist
20             DBIx::Class::Helper::ResultSet::Shortcut::Rows
21             DBIx::Class::Helper::ResultSet::Shortcut::Page
22             DBIx::Class::Helper::ResultSet::Shortcut::Search
23 56     56   266 ));
  56         99  
24              
25             1;
26              
27             __END__
28              
29             =pod
30              
31             =head1 NAME
32              
33             DBIx::Class::Helper::ResultSet::Shortcut - Shortcuts to common searches (->order_by, etc)
34              
35             =head1 SYNOPSIS
36              
37             package MyApp::Schema::ResultSet::Foo;
38              
39             __PACKAGE__->load_components(qw{Helper::ResultSet::Shortcut});
40              
41             ...
42              
43             1;
44              
45             And then elsewhere:
46              
47             # let's say you grab a resultset from somewhere else
48             my $foo_rs = get_common_rs()
49             # but I'd like it sorted!
50             ->order_by({ -desc => 'power_level' })
51             # and without those other dumb columns
52             ->columns([qw/cromulence_ratio has_jimmies_rustled/])
53             # but get rid of those duplicates
54             ->distinct
55             # and put those straight into hashrefs, please
56             ->hri
57             # but only give me the first 3
58             ->rows(3);
59              
60             =head1 DESCRIPTION
61              
62             This helper provides convenience methods for resultset modifications.
63              
64             See L<DBIx::Class::Helper::ResultSet/NOTE> for a nice way to apply it to your
65             entire schema.
66              
67             =head1 SEE ALSO
68              
69             This component is actually a number of other components put together. It will
70             get more components added to it over time. If you are worried about all the
71             extra methods you won't use or something, using the individual shortcuts is
72             a simple solution. All the documentation will remain here, but the individual
73             components are:
74              
75             =over 2
76              
77             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::HRI>
78              
79             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::OrderBy>
80              
81             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::OrderByMagic>
82              
83             (adds the "magic string" functionality to
84             C<DBIx::Class::Helper::ResultSet::Shortcut::OrderBy>))
85              
86             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::GroupBy>
87              
88             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::Distinct>
89              
90             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::Rows>
91              
92             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::Limit>
93              
94             (inherits from C<DBIx::Class::Helper::ResultSet::Shortcut::Rows>)
95              
96             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::HasRows>
97              
98             (inherits from C<DBIx::Class::Helper::ResultSet::Shortcut::Rows>)
99              
100             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::Columns>
101              
102             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::AddColumns>
103              
104             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::Page>
105              
106             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::LimitedPage>
107              
108             (inherits from C<DBIx::Class::Helper::ResultSet::Shortcut::Page> and
109             L<DBIx::Class::Helper::ResultSet::Shortcut::Rows>)
110              
111             =item * L<DBIx::Class::Helper::ResultSet::Shortcut::ResultsExist>
112              
113             =back
114              
115             =head1 METHODS
116              
117             =head2 distinct
118              
119             $foo_rs->distinct
120              
121             # equivalent to...
122             $foo_rs->search(undef, { distinct => 1 });
123              
124             =head2 group_by
125              
126             $foo_rs->group_by([ qw/ some column names /])
127              
128             # equivalent to...
129             $foo_rs->search(undef, { group_by => [ qw/ some column names /] });
130              
131             =head2 order_by
132              
133             $foo_rs->order_by({ -desc => 'col1' });
134              
135             # equivalent to...
136             $foo_rs->search(undef, { order_by => { -desc => 'col1' } });
137              
138             You can also specify the order as a "magic string", e.g.:
139              
140             $foo_rs->order_by('!col1') # ->order_by({ -desc => 'col1' })
141             $foo_rs->order_by('col1,col2') # ->order_by([qw(col1 col2)])
142             $foo_rs->order_by('col1,!col2') # ->order_by([{ -asc => 'col1' }, { -desc => 'col2' }])
143             $foo_rs->order_by(qw(col1 col2)) # ->order_by([qw(col1 col2)])
144              
145             Can mix it all up as well:
146              
147             $foo_rs->order_by(qw(col1 col2 col3), 'col4,!col5')
148              
149             =head2 hri
150              
151             $foo_rs->hri;
152              
153             # equivalent to...
154             $foo_rs->search(undef, {
155             result_class => 'DBIx::Class::ResultClass::HashRefInflator'
156             });
157              
158             =head2 rows
159              
160             $foo_rs->rows(10);
161              
162             # equivalent to...
163             $foo_rs->search(undef, { rows => 10 })
164              
165             =head2 limit
166              
167             This is an alias for C<rows>.
168              
169             $foo_rs->limit(10);
170              
171             # equivalent to...
172             $foo_rs->rows(10);
173              
174             =head2 has_rows
175              
176             A lighter way to check the resultset contains any data rather than
177             calling C<< $rs->count >>.
178              
179             =head2 page
180              
181             $foo_rs->page(2);
182              
183             # equivalent to...
184             $foo_rs->search(undef, { page => 2 })
185              
186             =head2 limited_page
187              
188             $foo_rs->limited_page(2, 3);
189              
190             # equivalent to...
191             $foo_rs->search(undef, { page => 2, rows => 3 })
192              
193             =head2 columns
194              
195             $foo_rs->columns([qw/ some column names /]);
196              
197             # equivalent to...
198             $foo_rs->search(undef, { columns => [qw/ some column names /] });
199              
200             =head2 add_columns
201              
202             $foo_rs->add_columns([qw/ some column names /]);
203              
204             # equivalent to...
205             $foo_rs->search(undef, { '+columns' => [qw/ some column names /] });
206              
207             =head2 prefetch
208              
209             $foo_rs->prefetch('bar');
210              
211             # equivalent to...
212             $foo_rs->search(undef, { prefetch => 'bar' });
213              
214             =head2 results_exist
215              
216             my $results_exist = $schema->resultset('Bar')->search({...})->results_exist;
217              
218             # there is no easily expressable equivalent, so this is not exactly a
219             # shortcut. Nevertheless kept in this class for historical reasons
220              
221             Uses C<EXISTS> SQL function to check if the query would return anything.
222             Usually much less resource intensive the more common C<< foo() if $rs->count >>
223             idiom.
224              
225             =head2 results_exist_as_query
226              
227             ...->search(
228             {},
229             { '+columns' => {
230             subquery_has_members => $some_correlated_rs->results_exist_as_query
231             }},
232             );
233              
234             # there is no easily expressable equivalent, so this is not exactly a
235             # shortcut. Nevertheless kept in this class for historical reasons
236              
237             The query generator behind L</results_exist>. Can be used standalone in
238             complex queries returning a boolean result within a larger query context.
239              
240             =head2 null(@columns || \@columns)
241              
242             $rs->null('status');
243             $rs->null(['status', 'title']);
244              
245             =head2 not_null(@columns || \@columns)
246              
247             $rs->not_null('status');
248             $rs->not_null(['status', 'title']);
249              
250             =head2 like($column || \@columns, $cond)
251              
252             $rs->like('lyrics', '%zebra%');
253             $rs->like(['lyrics', 'title'], '%zebra%');
254              
255             =head2 not_like($column || \@columns, $cond)
256              
257             $rs->not_like('lyrics', '%zebra%');
258             $rs->not_like(['lyrics', 'title'], '%zebra%');
259              
260             =head1 AUTHOR
261              
262             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
263              
264             =head1 COPYRIGHT AND LICENSE
265              
266             This software is copyright (c) 2019 by Arthur Axel "fREW" Schmidt.
267              
268             This is free software; you can redistribute it and/or modify it under
269             the same terms as the Perl 5 programming language system itself.
270              
271             =cut