1 package DBIx::Class::ResultSetColumn;
4 use base 'DBIx::Class';
9 DBIx::Class::ResultSetColumn - helpful methods for messing
10 with a single column of the resultset
14 $rs = $schema->resultset('CD')->search({ artist => 'Tool' });
15 $rs_column = $rs->get_column('year');
16 $max_year = $rs_column->max; #returns latest year
20 A convenience class used to perform operations on a specific column of
29 my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
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>.
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
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;
47 my $select = defined $as_index ? $select_list->[$as_index] : $column;
49 my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
50 $new->throw_exception("column must be supplied") unless $column;
60 =item Return Value: $value
64 Returns the next value of the column in the resultset (or C<undef> if
67 Much like L<DBIx::Class::ResultSet/next> but just returning the
74 my ($row) = $self->_resultset->cursor->next;
84 =item Return Value: @values
88 Returns all values of the column in the resultset (or C<undef> if
91 Much like L<DBIx::Class::ResultSet/all> but returns values rather
98 return map { $_->[0] } $self->_resultset->cursor->all;
105 =item Arguments: none
107 =item Return Value: $self
111 Resets the underlying resultset's cursor, so you can iterate through the
112 elements of the column again.
114 Much like L<DBIx::Class::ResultSet/reset>.
120 $self->_resultset->cursor->reset;
128 =item Arguments: none
130 =item Return Value: $value
134 Resets the underlying resultset and returns the next value of the column in the
135 resultset (or C<undef> if there is none).
137 Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
143 my ($row) = $self->_resultset->cursor->reset->next;
151 =item Arguments: none
153 =item Return Value: $lowest_value
157 my $first_year = $year_col->min();
159 Wrapper for ->func. Returns the lowest value of the column in the
160 resultset (or C<undef> if there are none).
165 return shift->func('MIN');
172 =item Arguments: none
174 =item Return Value: $highest_value
178 my $last_year = $year_col->max();
180 Wrapper for ->func. Returns the highest value of the column in the
181 resultset (or C<undef> if there are none).
186 return shift->func('MAX');
193 =item Arguments: none
195 =item Return Value: $sum_of_values
199 my $total = $prices_col->sum();
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.
207 return shift->func('SUM');
214 =item Arguments: $function
216 =item Return Value: $function_return_value
220 $rs = $schema->resultset("CD")->search({});
221 $length = $rs->get_column('title')->func('LENGTH');
223 Runs a query using the function on the column and returns the
224 value. Produces the following SQL:
226 SELECT LENGTH( title ) FROM cd me
231 my ($self,$function) = @_;
232 my $cursor = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_select}}, as => [$self->{_as}]})->cursor;
235 return map { $_->[ 0 ] } $cursor->all;
238 return ( $cursor->next )[ 0 ];
241 =head2 throw_exception
243 See L<DBIx::Class::Schema/throw_exception> for details.
247 sub throw_exception {
249 if (ref $self && $self->{_parent_resultset}) {
250 $self->{_parent_resultset}->throw_exception(@_)
260 # Return Value: $resultset
262 # $year_col->_resultset->next
264 # Returns the underlying resultset. Creates it from the parent resultset if
270 return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
272 select => [$self->{_select}],
283 Luke Saunders <luke.saunders@gmail.com>
289 You may distribute this code under the same terms as Perl itself.