* Fixed test cases of ResultSetColumn vs. +select/+as (they used to unconditionally...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSetColumn.pm
CommitLineData
2bb7b40b 1package DBIx::Class::ResultSetColumn;
2use strict;
3use warnings;
4use base 'DBIx::Class';
5d1fc7dc 5use List::Util qw(first);
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;
3f6cc7e4 39 my $new_parent_rs = $rs->search_rs; # we don't want to mess up the original, so clone it
5d1fc7dc 40 my $attrs = $new_parent_rs->_resolved_attrs;
41 $new_parent_rs->{attrs}->{$_} = undef for qw(prefetch include_columns +select +as); # prefetch, include_columns, +select, +as cause additional columns to be fetched
42 my ($select, $as) =
43 map { defined $_ ? ($attrs->{select}->[$_], $attrs->{as}->[$_]) : ($column, $column) }
44 first { ($attrs->{as} || [])->[$_] eq $column }
45 0..$#{$attrs->{as} || []};
46 my $new = bless { _select => $select, _as => $as, _parent_resultset => $new_parent_rs }, $class;
6b051e14 47 $new->throw_exception("column must be supplied") unless $column;
2bb7b40b 48 return $new;
49}
50
51=head2 next
52
53=over 4
54
55=item Arguments: none
56
57=item Return Value: $value
58
59=back
60
eb98561c 61Returns the next value of the column in the resultset (or C<undef> if
62there is none).
2bb7b40b 63
eb98561c 64Much like L<DBIx::Class::ResultSet/next> but just returning the
65one value.
2bb7b40b 66
67=cut
68
69sub next {
70 my $self = shift;
5d1fc7dc 71 $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_select}], as => [$self->{_as}]}) unless ($self->{_resultset});
2bb7b40b 72 my ($row) = $self->{_resultset}->cursor->next;
73 return $row;
74}
75
76=head2 all
77
78=over 4
79
80=item Arguments: none
81
82=item Return Value: @values
83
84=back
85
eb98561c 86Returns all values of the column in the resultset (or C<undef> if
87there are none).
2bb7b40b 88
eb98561c 89Much like L<DBIx::Class::ResultSet/all> but returns values rather
90than row objects.
2bb7b40b 91
92=cut
93
94sub all {
95 my $self = shift;
5d1fc7dc 96 return map {$_->[0]} $self->{_parent_resultset}->search(undef, {select => [$self->{_select}], as => [$self->{_as}]})->cursor->all;
2bb7b40b 97}
98
99=head2 min
100
101=over 4
102
103=item Arguments: none
104
105=item Return Value: $lowest_value
106
107=back
108
eb98561c 109 my $first_year = $year_col->min();
110
111Wrapper for ->func. Returns the lowest value of the column in the
112resultset (or C<undef> if there are none).
2bb7b40b 113
114=cut
115
116sub min {
6b051e14 117 return shift->func('MIN');
2bb7b40b 118}
119
120=head2 max
121
122=over 4
123
124=item Arguments: none
125
126=item Return Value: $highest_value
127
128=back
129
eb98561c 130 my $last_year = $year_col->max();
131
132Wrapper for ->func. Returns the highest value of the column in the
133resultset (or C<undef> if there are none).
2bb7b40b 134
135=cut
136
137sub max {
6b051e14 138 return shift->func('MAX');
2bb7b40b 139}
140
141=head2 sum
142
143=over 4
144
145=item Arguments: none
146
147=item Return Value: $sum_of_values
148
149=back
150
eb98561c 151 my $total = $prices_col->sum();
152
153Wrapper for ->func. Returns the sum of all the values in the column of
154the resultset. Use on varchar-like columns at your own risk.
2bb7b40b 155
156=cut
157
158sub sum {
6b051e14 159 return shift->func('SUM');
2bb7b40b 160}
161
162=head2 func
163
164=over 4
165
166=item Arguments: $function
167
168=item Return Value: $function_return_value
169
170=back
171
e8419341 172 $rs = $schema->resultset("CD")->search({});
173 $length = $rs->get_column('title')->func('LENGTH');
2bb7b40b 174
eb98561c 175Runs a query using the function on the column and returns the
176value. Produces the following SQL:
177
178 SELECT LENGTH( title ) FROM cd me
2bb7b40b 179
180=cut
181
182sub func {
6b051e14 183 my ($self,$function) = @_;
5d1fc7dc 184 my $cursor = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_select}}, as => [$self->{_as}]})->cursor;
5d62876f 185
186 if( wantarray ) {
187 return map { $_->[ 0 ] } $cursor->all;
188 }
189
190 return ( $cursor->next )[ 0 ];
2bb7b40b 191}
192
5d1fc7dc 193=head2 throw_exception
194
195See L<DBIx::Class::Schema/throw_exception> for details.
196
197=cut
198
199sub throw_exception {
200 my $self=shift;
201 if (ref $self && $self->{_parent_resultset}) {
202 $self->{_parent_resultset}->throw_exception(@_)
203 } else {
204 croak(@_);
205 }
206}
207
208
2bb7b40b 2091;
210
211=head1 AUTHORS
212
213Luke Saunders <luke.saunders@gmail.com>
214
eb98561c 215Jess Robinson
216
2bb7b40b 217=head1 LICENSE
218
219You may distribute this code under the same terms as Perl itself.
220
221=cut