* Fixed test cases of ResultSetColumn vs. +select/+as (they used to unconditionally...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSetColumn.pm
1 package DBIx::Class::ResultSetColumn;
2 use strict;
3 use warnings;
4 use base 'DBIx::Class';
5 use List::Util qw(first);
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
20 A convenience class used to perform operations on a specific column of
21 a resultset.
22
23 =cut
24
25 =head1 METHODS
26
27 =head2 new
28
29   my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
30
31 Creates a new resultset column object from the resultset and column
32 passed as params. Used internally by L<DBIx::Class::ResultSet/get_column>.
33
34 =cut
35
36 sub new {
37   my ($class, $rs, $column) = @_;
38   $class = ref $class if ref $class;
39   my $new_parent_rs = $rs->search_rs; # we don't want to mess up the original, so clone it
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;
47   $new->throw_exception("column must be supplied") unless $column;
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
61 Returns the next value of the column in the resultset (or C<undef> if
62 there is none).
63
64 Much like L<DBIx::Class::ResultSet/next> but just returning the 
65 one value.
66
67 =cut
68
69 sub next {
70   my $self = shift;
71   $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_select}], as => [$self->{_as}]}) unless ($self->{_resultset});
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
86 Returns all values of the column in the resultset (or C<undef> if
87 there are none).
88
89 Much like L<DBIx::Class::ResultSet/all> but returns values rather
90 than row objects.
91
92 =cut
93
94 sub all {
95   my $self = shift;
96   return map {$_->[0]} $self->{_parent_resultset}->search(undef, {select => [$self->{_select}], as => [$self->{_as}]})->cursor->all;
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
109   my $first_year = $year_col->min();
110
111 Wrapper for ->func. Returns the lowest value of the column in the
112 resultset (or C<undef> if there are none).
113
114 =cut
115
116 sub min {
117   return shift->func('MIN');
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
130   my $last_year = $year_col->max();
131
132 Wrapper for ->func. Returns the highest value of the column in the
133 resultset (or C<undef> if there are none).
134
135 =cut
136
137 sub max {
138   return shift->func('MAX');
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
151   my $total = $prices_col->sum();
152
153 Wrapper for ->func. Returns the sum of all the values in the column of
154 the resultset. Use on varchar-like columns at your own risk.
155
156 =cut
157
158 sub sum {
159   return shift->func('SUM');
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
172   $rs = $schema->resultset("CD")->search({});
173   $length = $rs->get_column('title')->func('LENGTH');
174
175 Runs a query using the function on the column and returns the
176 value. Produces the following SQL:
177
178   SELECT LENGTH( title ) FROM cd me
179
180 =cut
181
182 sub func {
183   my ($self,$function) = @_;
184   my $cursor = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_select}}, as => [$self->{_as}]})->cursor;
185   
186   if( wantarray ) {
187     return map { $_->[ 0 ] } $cursor->all;
188   }
189
190   return ( $cursor->next )[ 0 ];
191 }
192
193 =head2 throw_exception
194
195 See L<DBIx::Class::Schema/throw_exception> for details.
196   
197 =cut 
198     
199 sub 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
209 1;
210
211 =head1 AUTHORS
212
213 Luke Saunders <luke.saunders@gmail.com>
214
215 Jess Robinson
216
217 =head1 LICENSE
218
219 You may distribute this code under the same terms as Perl itself.
220
221 =cut