Helper/ResultSet/Shortcut.pm

filter: Statementstmt 100% 11 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 14 / 14 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Helper/ResultSet/OneRow.pm ↑ Summary Helper/ResultSet/Shortcut/Columns.pm »
1 package DBIx::Class::Helper::ResultSet::Shortcut;
2 55 $DBIx::Class::Helper::ResultSet::Shortcut::VERSION = '2.032002';
3 # ABSTRACT: Shortcuts to common searches (->order_by, etc)
4  
5 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 55 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 55 ▶));
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 55 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 Uses C<EXISTS> SQL function to check if the query would return anything.
219 Possibly lighter weight than the much more common C<< foo() if $rs->count >>
220 idiom.
221  
222 =head2 null(@columns || \@columns)
223  
224 $rs->null('status');
225 $rs->null(['status', 'title']);
226  
227 =head2 not_null(@columns || \@columns)
228  
229 $rs->not_null('status');
230 $rs->not_null(['status', 'title']);
231  
232 =head2 like($column || \@columns, $cond)
233  
234 $rs->like('lyrics', '%zebra%');
235 $rs->like(['lyrics', 'title'], '%zebra%');
236  
237 =head2 not_like($column || \@columns, $cond)
238  
239 $rs->not_like('lyrics', '%zebra%');
240 $rs->not_like(['lyrics', 'title'], '%zebra%');
241  
242 =head1 AUTHOR
243  
244 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
245  
246 =head1 COPYRIGHT AND LICENSE
247  
248 This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt.
249  
250 This is free software; you can redistribute it and/or modify it under
251 the same terms as the Perl 5 programming language system itself.
252  
253 =cut
« Helper/ResultSet/OneRow.pm ↑ Summary Helper/ResultSet/Shortcut/Columns.pm »