And score! (all works)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSetColumn.pm
CommitLineData
2bb7b40b 1package DBIx::Class::ResultSetColumn;
2use strict;
3use warnings;
4use base 'DBIx::Class';
66521001 5use List::Util;
2bb7b40b 6
7=head1 NAME
8
9 DBIx::Class::ResultSetColumn - helpful methods for messing
10 with a single column of the resultset
11
12=head1 SYNOPSIS
13
14 $rs = $schema->resultset('CD')->search({ artist => 'Tool' });
15 $rs_column = $rs->get_column('year');
16 $max_year = $rs_column->max; #returns latest year
17
18=head1 DESCRIPTION
19
eb98561c 20A convenience class used to perform operations on a specific column of
21a resultset.
2bb7b40b 22
23=cut
24
25=head1 METHODS
26
27=head2 new
28
29 my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
30
eb98561c 31Creates a new resultset column object from the resultset and column
32passed as params. Used internally by L<DBIx::Class::ResultSet/get_column>.
2bb7b40b 33
34=cut
35
36sub new {
37 my ($class, $rs, $column) = @_;
38 $class = ref $class if ref $class;
7ae9706c 39
40 $rs->throw_exception("column must be supplied") unless $column;
41
3f6cc7e4 42 my $new_parent_rs = $rs->search_rs; # we don't want to mess up the original, so clone it
7ae9706c 43
44 # prefetch causes additional columns to be fetched, but we can not just make a new
45 # rs via the _resolved_attrs trick - we need to retain the separation between
bed3a173 46 # +select/+as and select/as. At the same time we want to preserve any joins that the
47 # prefetch would otherwise generate.
48 my $init_attrs = $new_parent_rs->{attrs} ||= {};
49 delete $init_attrs->{collapse};
50 $init_attrs->{join} = $rs->_merge_attr( delete $init_attrs->{join}, delete $init_attrs->{prefetch} );
b6e85b48 51
152002c4 52 # If $column can be found in the 'as' list of the parent resultset, use the
53 # corresponding element of its 'select' list (to keep any custom column
54 # definition set up with 'select' or '+select' attrs), otherwise use $column
55 # (to create a new column definition on-the-fly).
7ae9706c 56 my $attrs = $new_parent_rs->_resolved_attrs;
57
b6e85b48 58 my $as_list = $attrs->{as} || [];
59 my $select_list = $attrs->{select} || [];
60 my $as_index = List::Util::first { ($as_list->[$_] || "") eq $column } 0..$#$as_list;
b6e85b48 61 my $select = defined $as_index ? $select_list->[$as_index] : $column;
62
63 my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
2bb7b40b 64 return $new;
65}
66
70bb942d 67=head2 as_query (EXPERIMENTAL)
658fa250 68
69=over 4
70
428a645e 71=item Arguments: none
658fa250 72
4dc99a01 73=item Return Value: \[ $sql, @bind ]
658fa250 74
75=back
76
77Returns the SQL query and bind vars associated with the invocant.
78
03834f77 79This is generally used as the RHS for a subquery.
c7a9d102 80
6a9530d1 81B<NOTE>: This feature is still experimental.
82
c7a9d102 83=cut
84
0f6fc705 85sub as_query { return shift->_resultset->as_query(@_) }
c7a9d102 86
2bb7b40b 87=head2 next
88
89=over 4
90
91=item Arguments: none
92
93=item Return Value: $value
94
95=back
96
eb98561c 97Returns the next value of the column in the resultset (or C<undef> if
98there is none).
2bb7b40b 99
eb98561c 100Much like L<DBIx::Class::ResultSet/next> but just returning the
101one value.
2bb7b40b 102
103=cut
104
105sub next {
106 my $self = shift;
66521001 107 my ($row) = $self->_resultset->cursor->next;
2bb7b40b 108 return $row;
109}
110
111=head2 all
112
113=over 4
114
115=item Arguments: none
116
117=item Return Value: @values
118
119=back
120
eb98561c 121Returns all values of the column in the resultset (or C<undef> if
122there are none).
2bb7b40b 123
eb98561c 124Much like L<DBIx::Class::ResultSet/all> but returns values rather
125than row objects.
2bb7b40b 126
127=cut
128
129sub all {
130 my $self = shift;
66521001 131 return map { $_->[0] } $self->_resultset->cursor->all;
132}
133
134=head2 reset
135
136=over 4
137
138=item Arguments: none
139
140=item Return Value: $self
141
142=back
143
144Resets the underlying resultset's cursor, so you can iterate through the
145elements of the column again.
146
147Much like L<DBIx::Class::ResultSet/reset>.
148
149=cut
150
151sub reset {
152 my $self = shift;
153 $self->_resultset->cursor->reset;
154 return $self;
155}
156
157=head2 first
158
159=over 4
160
161=item Arguments: none
162
163=item Return Value: $value
164
165=back
166
167Resets the underlying resultset and returns the next value of the column in the
168resultset (or C<undef> if there is none).
169
170Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
171
172=cut
173
174sub first {
175 my $self = shift;
3484603a 176 my ($row) = $self->_resultset->cursor->reset->next;
66521001 177 return $row;
2bb7b40b 178}
179
180=head2 min
181
182=over 4
183
184=item Arguments: none
185
186=item Return Value: $lowest_value
187
188=back
189
eb98561c 190 my $first_year = $year_col->min();
191
192Wrapper for ->func. Returns the lowest value of the column in the
193resultset (or C<undef> if there are none).
2bb7b40b 194
195=cut
196
197sub min {
6b051e14 198 return shift->func('MIN');
2bb7b40b 199}
200
4fa7bc22 201=head2 min_rs
202
203=over 4
204
205=item Arguments: none
206
207=item Return Value: $resultset
208
209=back
210
211 my $rs = $year_col->min_rs();
212
213Wrapper for ->func_rs for function MIN().
214
215=cut
216
217sub min_rs { return shift->func_rs('MIN') }
218
2bb7b40b 219=head2 max
220
221=over 4
222
223=item Arguments: none
224
225=item Return Value: $highest_value
226
227=back
228
eb98561c 229 my $last_year = $year_col->max();
230
231Wrapper for ->func. Returns the highest value of the column in the
232resultset (or C<undef> if there are none).
2bb7b40b 233
234=cut
235
236sub max {
6b051e14 237 return shift->func('MAX');
2bb7b40b 238}
239
4fa7bc22 240=head2 max_rs
241
242=over 4
243
244=item Arguments: none
245
246=item Return Value: $resultset
247
248=back
249
250 my $rs = $year_col->max_rs();
251
252Wrapper for ->func_rs for function MAX().
253
254=cut
255
256sub max_rs { return shift->func_rs('MAX') }
257
2bb7b40b 258=head2 sum
259
260=over 4
261
262=item Arguments: none
263
264=item Return Value: $sum_of_values
265
266=back
267
eb98561c 268 my $total = $prices_col->sum();
269
270Wrapper for ->func. Returns the sum of all the values in the column of
271the resultset. Use on varchar-like columns at your own risk.
2bb7b40b 272
273=cut
274
275sub sum {
6b051e14 276 return shift->func('SUM');
2bb7b40b 277}
278
4fa7bc22 279=head2 sum_rs
280
281=over 4
282
283=item Arguments: none
284
285=item Return Value: $resultset
286
287=back
288
289 my $rs = $year_col->sum_rs();
290
291Wrapper for ->func_rs for function SUM().
292
293=cut
294
295sub sum_rs { return shift->func_rs('SUM') }
296
2bb7b40b 297=head2 func
298
299=over 4
300
301=item Arguments: $function
302
303=item Return Value: $function_return_value
304
305=back
306
e8419341 307 $rs = $schema->resultset("CD")->search({});
308 $length = $rs->get_column('title')->func('LENGTH');
2bb7b40b 309
eb98561c 310Runs a query using the function on the column and returns the
311value. Produces the following SQL:
312
313 SELECT LENGTH( title ) FROM cd me
2bb7b40b 314
315=cut
316
317sub func {
6b051e14 318 my ($self,$function) = @_;
4fa7bc22 319 my $cursor = $self->func_rs($function)->cursor;
5d62876f 320
321 if( wantarray ) {
322 return map { $_->[ 0 ] } $cursor->all;
323 }
324
325 return ( $cursor->next )[ 0 ];
2bb7b40b 326}
327
4fa7bc22 328=head2 func_rs
329
330=over 4
331
332=item Arguments: $function
333
334=item Return Value: $resultset
335
336=back
337
338Creates the resultset that C<func()> uses to run its query.
339
340=cut
341
342sub func_rs {
343 my ($self,$function) = @_;
344 return $self->{_parent_resultset}->search(
345 undef, {
346 select => {$function => $self->{_select}},
347 as => [$self->{_as}],
348 },
349 );
350}
351
5d1fc7dc 352=head2 throw_exception
353
354See L<DBIx::Class::Schema/throw_exception> for details.
355
356=cut
357
358sub throw_exception {
359 my $self=shift;
360 if (ref $self && $self->{_parent_resultset}) {
361 $self->{_parent_resultset}->throw_exception(@_)
362 } else {
363 croak(@_);
364 }
365}
366
b6e85b48 367# _resultset
368#
369# Arguments: none
370#
371# Return Value: $resultset
372#
373# $year_col->_resultset->next
374#
375# Returns the underlying resultset. Creates it from the parent resultset if
376# necessary.
377#
66521001 378sub _resultset {
379 my $self = shift;
380
381 return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
382 {
383 select => [$self->{_select}],
384 as => [$self->{_as}]
385 }
386 );
387}
388
2bb7b40b 3891;
390
391=head1 AUTHORS
392
393Luke Saunders <luke.saunders@gmail.com>
394
eb98561c 395Jess Robinson
396
2bb7b40b 397=head1 LICENSE
398
399You may distribute this code under the same terms as Perl itself.
400
401=cut