Checking in failing test
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSetColumn.pm
CommitLineData
2bb7b40b 1package DBIx::Class::ResultSetColumn;
2use strict;
3use warnings;
4use base 'DBIx::Class';
66521001 5use List::Util;
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
b6e85b48 42
152002c4 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).
b6e85b48 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;
b6e85b48 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;
6b051e14 53 $new->throw_exception("column must be supplied") unless $column;
2bb7b40b 54 return $new;
55}
56
658fa250 57=head2 as_query
58
59=over 4
60
61=item Arguments: none
62
4dc99a01 63=item Return Value: \[ $sql, @bind ]
658fa250 64
65=back
66
67Returns the SQL query and bind vars associated with the invocant.
68
69=cut
70
71sub as_query { return shift->_resultset->as_query }
72
2bb7b40b 73=head2 next
74
75=over 4
76
77=item Arguments: none
78
79=item Return Value: $value
80
81=back
82
eb98561c 83Returns the next value of the column in the resultset (or C<undef> if
84there is none).
2bb7b40b 85
eb98561c 86Much like L<DBIx::Class::ResultSet/next> but just returning the
87one value.
2bb7b40b 88
89=cut
90
91sub next {
92 my $self = shift;
66521001 93 my ($row) = $self->_resultset->cursor->next;
2bb7b40b 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
eb98561c 107Returns all values of the column in the resultset (or C<undef> if
108there are none).
2bb7b40b 109
eb98561c 110Much like L<DBIx::Class::ResultSet/all> but returns values rather
111than row objects.
2bb7b40b 112
113=cut
114
115sub all {
116 my $self = shift;
66521001 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
130Resets the underlying resultset's cursor, so you can iterate through the
131elements of the column again.
132
133Much like L<DBIx::Class::ResultSet/reset>.
134
135=cut
136
137sub 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
153Resets the underlying resultset and returns the next value of the column in the
154resultset (or C<undef> if there is none).
155
156Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
157
158=cut
159
160sub first {
161 my $self = shift;
3484603a 162 my ($row) = $self->_resultset->cursor->reset->next;
66521001 163 return $row;
2bb7b40b 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
eb98561c 176 my $first_year = $year_col->min();
177
178Wrapper for ->func. Returns the lowest value of the column in the
179resultset (or C<undef> if there are none).
2bb7b40b 180
181=cut
182
183sub min {
6b051e14 184 return shift->func('MIN');
2bb7b40b 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
eb98561c 197 my $last_year = $year_col->max();
198
199Wrapper for ->func. Returns the highest value of the column in the
200resultset (or C<undef> if there are none).
2bb7b40b 201
202=cut
203
204sub max {
6b051e14 205 return shift->func('MAX');
2bb7b40b 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
eb98561c 218 my $total = $prices_col->sum();
219
220Wrapper for ->func. Returns the sum of all the values in the column of
221the resultset. Use on varchar-like columns at your own risk.
2bb7b40b 222
223=cut
224
225sub sum {
6b051e14 226 return shift->func('SUM');
2bb7b40b 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
e8419341 239 $rs = $schema->resultset("CD")->search({});
240 $length = $rs->get_column('title')->func('LENGTH');
2bb7b40b 241
eb98561c 242Runs a query using the function on the column and returns the
243value. Produces the following SQL:
244
245 SELECT LENGTH( title ) FROM cd me
2bb7b40b 246
247=cut
248
249sub func {
6b051e14 250 my ($self,$function) = @_;
5d1fc7dc 251 my $cursor = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_select}}, as => [$self->{_as}]})->cursor;
5d62876f 252
253 if( wantarray ) {
254 return map { $_->[ 0 ] } $cursor->all;
255 }
256
257 return ( $cursor->next )[ 0 ];
2bb7b40b 258}
259
5d1fc7dc 260=head2 throw_exception
261
262See L<DBIx::Class::Schema/throw_exception> for details.
263
264=cut
265
266sub 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
b6e85b48 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#
66521001 286sub _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
2bb7b40b 2971;
298
299=head1 AUTHORS
300
301Luke Saunders <luke.saunders@gmail.com>
302
eb98561c 303Jess Robinson
304
2bb7b40b 305=head1 LICENSE
306
307You may distribute this code under the same terms as Perl itself.
308
309=cut