File: Shortcut.pm

package info (click to toggle)
libdbix-class-helpers-perl 2.023007-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 792 kB
  • ctags: 294
  • sloc: perl: 3,872; sql: 161; makefile: 7
file content (232 lines) | stat: -rw-r--r-- 5,822 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package DBIx::Class::Helper::ResultSet::Shortcut;
$DBIx::Class::Helper::ResultSet::Shortcut::VERSION = '2.023007';
# ABSTRACT: Shortcuts to common searches (->order_by, etc)

use strict;
use warnings;

use base (qw(
   DBIx::Class::Helper::ResultSet::Shortcut::AddColumns
   DBIx::Class::Helper::ResultSet::Shortcut::Columns
   DBIx::Class::Helper::ResultSet::Shortcut::Distinct
   DBIx::Class::Helper::ResultSet::Shortcut::GroupBy
   DBIx::Class::Helper::ResultSet::Shortcut::HasRows
   DBIx::Class::Helper::ResultSet::Shortcut::HRI
   DBIx::Class::Helper::ResultSet::Shortcut::Limit
   DBIx::Class::Helper::ResultSet::Shortcut::OrderByMagic
   DBIx::Class::Helper::ResultSet::Shortcut::Prefetch
   DBIx::Class::Helper::ResultSet::Shortcut::LimitedPage
   DBIx::Class::Helper::ResultSet::Shortcut::ResultsExist
   DBIx::Class::Helper::ResultSet::Shortcut::Rows
   DBIx::Class::Helper::ResultSet::Shortcut::Page
));

1;

__END__

=pod

=head1 NAME

DBIx::Class::Helper::ResultSet::Shortcut - Shortcuts to common searches (->order_by, etc)

=head1 SYNOPSIS

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

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

 ...

 1;

And then elsewhere:

 # 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);

=head1 DESCRIPTION

This helper provides convenience methods for resultset modifications.

See L<DBIx::Class::Helper::ResultSet/NOTE> for a nice way to apply it to your
entire schema.

=head1 SEE ALSO

This component is actually a number of other components put together.  It will
get more components added to it over time.  If you are worried about all the
extra methods you won't use or something, using the individual shortcuts is
a simple solution.  All the documentation will remain here, but the individual
components are:

=over 2

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::HRI>

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::OrderBy>

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::OrderByMagic>

(adds the "magic string" functionality to
C<DBIx::Class::Helper::ResultSet::Shortcut::OrderBy>))

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::GroupBy>

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::Distinct>

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::Rows>

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::Limit>

(inherits from C<DBIx::Class::Helper::ResultSet::Shortcut::Rows>)

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::HasRows>

(inherits from C<DBIx::Class::Helper::ResultSet::Shortcut::Rows>)

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::Columns>

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::AddColumns>

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::Page>

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::LimitedPage>

(inherits from C<DBIx::Class::Helper::ResultSet::Shortcut::Page> and
L<DBIx::Class::Helper::ResultSet::Shortcut::Rows>)

=item * L<DBIx::Class::Helper::ResultSet::Shortcut::ResultsExist>

=back

=head1 METHODS

=head2 distinct

 $foo_rs->distinct

 # equivalent to...
 $foo_rs->search(undef, { distinct => 1 });

=head2 group_by

 $foo_rs->group_by([ qw/ some column names /])

 # equivalent to...
 $foo_rs->search(undef, { group_by => [ qw/ some column names /] });

=head2 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", e.g.:

 $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)])

Can mix it all up as well:

 $foo_rs->order_by(qw(col1 col2 col3), 'col4,!col5')

=head2 hri

 $foo_rs->hri;

 # equivalent to...
 $foo_rs->search(undef, {
    result_class => 'DBIx::Class::ResultClass::HashRefInflator'
 });

=head2 rows

 $foo_rs->rows(10);

 # equivalent to...
 $foo_rs->search(undef, { rows => 10 })

=head2 limit

This is an alias for C<rows>.

  $foo_rs->limit(10);

  # equivalent to...
  $foo_rs->rows(10);

=head2 has_rows

A lighter way to check the resultset contains any data rather than
calling C<< $rs->count >>.

=head2 page

 $foo_rs->page(2);

 # equivalent to...
 $foo_rs->search(undef, { page => 2 })

=head2 limited_page

 $foo_rs->limited_page(2, 3);

 # equivalent to...
 $foo_rs->search(undef, { page => 2, rows => 3 })

=head2 columns

 $foo_rs->columns([qw/ some column names /]);

 # equivalent to...
 $foo_rs->search(undef, { columns => [qw/ some column names /] });

=head2 add_columns

 $foo_rs->add_columns([qw/ some column names /]);

 # equivalent to...
 $foo_rs->search(undef, { '+columns' => [qw/ some column names /] });

=head2 prefetch

 $foo_rs->prefetch('bar');

 # equivalent to...
 $foo_rs->search(undef, { prefetch => 'bar' });

=head2 results_exist

 my $results_exist = $schema->resultset('Bar')->search({...})->results_exist;

Uses C<EXISTS> SQL function to check if the query would return anything.
Possibly lighter weight than the much more common C<< foo() if $rs->count >>
idiom.

=head1 AUTHOR

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 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.

=cut