1 package DBIx::Class::ResultSetColumn;
4 use base 'DBIx::Class';
8 DBIx::Class::ResultSetColumn - helpful methods for messing
9 with a single column of the resultset
13 $rs = $schema->resultset('CD')->search({ artist => 'Tool' });
14 $rs_column = $rs->get_column('year');
15 $max_year = $rs_column->max; #returns latest year
19 A convenience class used to perform operations on a specific column of
28 my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
30 Creates a new resultset column object from the resultset and column
31 passed as params. Used internally by L<DBIx::Class::ResultSet/get_column>.
36 my ($class, $rs, $column) = @_;
37 $class = ref $class if ref $class;
38 my $new_parent_rs = $rs->search_rs; # we don't want to mess up the original, so clone it
39 $new_parent_rs->{attrs}->{prefetch} = undef; # prefetch causes additional columns to be fetched
40 my $new = bless { _column => $column, _parent_resultset => $new_parent_rs }, $class;
41 $new->throw_exception("column must be supplied") unless $column;
51 =item Return Value: $value
55 Returns the next value of the column in the resultset (or C<undef> if
58 Much like L<DBIx::Class::ResultSet/next> but just returning the
65 $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]}) unless ($self->{_resultset});
66 my ($row) = $self->{_resultset}->cursor->next;
76 =item Return Value: @values
80 Returns all values of the column in the resultset (or C<undef> if
83 Much like L<DBIx::Class::ResultSet/all> but returns values rather
90 return map {$_->[0]} $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]})->cursor->all;
99 =item Return Value: $lowest_value
103 my $first_year = $year_col->min();
105 Wrapper for ->func. Returns the lowest value of the column in the
106 resultset (or C<undef> if there are none).
111 return shift->func('MIN');
118 =item Arguments: none
120 =item Return Value: $highest_value
124 my $last_year = $year_col->max();
126 Wrapper for ->func. Returns the highest value of the column in the
127 resultset (or C<undef> if there are none).
132 return shift->func('MAX');
139 =item Arguments: none
141 =item Return Value: $sum_of_values
145 my $total = $prices_col->sum();
147 Wrapper for ->func. Returns the sum of all the values in the column of
148 the resultset. Use on varchar-like columns at your own risk.
153 return shift->func('SUM');
160 =item Arguments: $function
162 =item Return Value: $function_return_value
166 $rs = $schema->resultset("CD")->search({});
167 $length = $rs->get_column('title')->func('LENGTH');
169 Runs a query using the function on the column and returns the
170 value. Produces the following SQL:
172 SELECT LENGTH( title ) FROM cd me
177 my ($self,$function) = @_;
178 my $cursor = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_column}}, as => [$self->{_column}]})->cursor;
181 return map { $_->[ 0 ] } $cursor->all;
184 return ( $cursor->next )[ 0 ];
191 Luke Saunders <luke.saunders@gmail.com>
197 You may distribute this code under the same terms as Perl itself.