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