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