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 = bless { _column => $column, _parent_resultset => $rs }, $class;
39 $new->throw_exception("column must be supplied") unless $column;
49 =item Return Value: $value
53 Returns the next value of the column in the resultset (or C<undef> if
56 Much like L<DBIx::Class::ResultSet/next> but just returning the
63 $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]}) unless ($self->{_resultset});
64 my ($row) = $self->{_resultset}->cursor->next;
74 =item Return Value: @values
78 Returns all values of the column in the resultset (or C<undef> if
81 Much like L<DBIx::Class::ResultSet/all> but returns values rather
88 return map {$_->[0]} $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]})->cursor->all;
97 =item Return Value: $lowest_value
101 my $first_year = $year_col->min();
103 Wrapper for ->func. Returns the lowest value of the column in the
104 resultset (or C<undef> if there are none).
109 return shift->func('MIN');
116 =item Arguments: none
118 =item Return Value: $highest_value
122 my $last_year = $year_col->max();
124 Wrapper for ->func. Returns the highest value of the column in the
125 resultset (or C<undef> if there are none).
130 return shift->func('MAX');
137 =item Arguments: none
139 =item Return Value: $sum_of_values
143 my $total = $prices_col->sum();
145 Wrapper for ->func. Returns the sum of all the values in the column of
146 the resultset. Use on varchar-like columns at your own risk.
151 return shift->func('SUM');
158 =item Arguments: $function
160 =item Return Value: $function_return_value
164 $rs = $schema->resultset("CD")->search({});
165 $length = $rs->get_column('title')->func('LENGTH');
167 Runs a query using the function on the column and returns the
168 value. Produces the following SQL:
170 SELECT LENGTH( title ) FROM cd me
175 my ($self,$function) = @_;
176 my ($row) = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_column}}, as => [$self->{_column}]})->cursor->next;
184 Luke Saunders <luke.saunders@gmail.com>
190 You may distribute this code under the same terms as Perl itself.