* Rewrote fetching the 'select' and 'as' values from the parent resultset to somethi...
[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;
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
43   my $as_list = $attrs->{as} || [];
44   my $select_list = $attrs->{select} || [];
45   my $as_index = List::Util::first { ($as_list->[$_] || "") eq $column } 0..$#$as_list;
46
47   my $select = defined $as_index ? $select_list->[$as_index] : $column;
48
49   my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
50   $new->throw_exception("column must be supplied") unless $column;
51   return $new;
52 }
53
54 =head2 next
55
56 =over 4
57
58 =item Arguments: none
59
60 =item Return Value: $value
61
62 =back
63
64 Returns the next value of the column in the resultset (or C<undef> if
65 there is none).
66
67 Much like L<DBIx::Class::ResultSet/next> but just returning the 
68 one value.
69
70 =cut
71
72 sub next {
73   my $self = shift;
74   my ($row) = $self->_resultset->cursor->next;
75   return $row;
76 }
77
78 =head2 all
79
80 =over 4
81
82 =item Arguments: none
83
84 =item Return Value: @values
85
86 =back
87
88 Returns all values of the column in the resultset (or C<undef> if
89 there are none).
90
91 Much like L<DBIx::Class::ResultSet/all> but returns values rather
92 than row objects.
93
94 =cut
95
96 sub all {
97   my $self = shift;
98   return map { $_->[0] } $self->_resultset->cursor->all;
99 }
100
101 =head2 reset
102
103 =over 4
104
105 =item Arguments: none
106
107 =item Return Value: $self
108
109 =back
110
111 Resets the underlying resultset's cursor, so you can iterate through the
112 elements of the column again.
113
114 Much like L<DBIx::Class::ResultSet/reset>.
115
116 =cut
117
118 sub reset {
119   my $self = shift;
120   $self->_resultset->cursor->reset;
121   return $self;
122 }
123
124 =head2 first
125
126 =over 4
127
128 =item Arguments: none
129
130 =item Return Value: $value
131
132 =back
133
134 Resets the underlying resultset and returns the next value of the column in the
135 resultset (or C<undef> if there is none).
136
137 Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
138
139 =cut
140
141 sub first {
142   my $self = shift;
143   my ($row) = $self->_resultset->cursor->reset->next;
144   return $row;
145 }
146
147 =head2 min
148
149 =over 4
150
151 =item Arguments: none
152
153 =item Return Value: $lowest_value
154
155 =back
156
157   my $first_year = $year_col->min();
158
159 Wrapper for ->func. Returns the lowest value of the column in the
160 resultset (or C<undef> if there are none).
161
162 =cut
163
164 sub min {
165   return shift->func('MIN');
166 }
167
168 =head2 max
169
170 =over 4
171
172 =item Arguments: none
173
174 =item Return Value: $highest_value
175
176 =back
177
178   my $last_year = $year_col->max();
179
180 Wrapper for ->func. Returns the highest value of the column in the
181 resultset (or C<undef> if there are none).
182
183 =cut
184
185 sub max {
186   return shift->func('MAX');
187 }
188
189 =head2 sum
190
191 =over 4
192
193 =item Arguments: none
194
195 =item Return Value: $sum_of_values
196
197 =back
198
199   my $total = $prices_col->sum();
200
201 Wrapper for ->func. Returns the sum of all the values in the column of
202 the resultset. Use on varchar-like columns at your own risk.
203
204 =cut
205
206 sub sum {
207   return shift->func('SUM');
208 }
209
210 =head2 func
211
212 =over 4
213
214 =item Arguments: $function
215
216 =item Return Value: $function_return_value
217
218 =back
219
220   $rs = $schema->resultset("CD")->search({});
221   $length = $rs->get_column('title')->func('LENGTH');
222
223 Runs a query using the function on the column and returns the
224 value. Produces the following SQL:
225
226   SELECT LENGTH( title ) FROM cd me
227
228 =cut
229
230 sub func {
231   my ($self,$function) = @_;
232   my $cursor = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_select}}, as => [$self->{_as}]})->cursor;
233   
234   if( wantarray ) {
235     return map { $_->[ 0 ] } $cursor->all;
236   }
237
238   return ( $cursor->next )[ 0 ];
239 }
240
241 =head2 throw_exception
242
243 See L<DBIx::Class::Schema/throw_exception> for details.
244   
245 =cut 
246     
247 sub throw_exception {
248   my $self=shift;
249   if (ref $self && $self->{_parent_resultset}) {
250     $self->{_parent_resultset}->throw_exception(@_)
251   } else {
252     croak(@_);
253   }
254 }
255
256 # _resultset
257 #
258 # Arguments: none
259 #
260 # Return Value: $resultset
261 #
262 #  $year_col->_resultset->next
263 #
264 # Returns the underlying resultset. Creates it from the parent resultset if
265 # necessary.
266
267 sub _resultset {
268   my $self = shift;
269
270   return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
271     {
272       select => [$self->{_select}],
273       as => [$self->{_as}]
274     }
275   );
276 }
277
278
279 1;
280
281 =head1 AUTHORS
282
283 Luke Saunders <luke.saunders@gmail.com>
284
285 Jess Robinson
286
287 =head1 LICENSE
288
289 You may distribute this code under the same terms as Perl itself.
290
291 =cut