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;
39 my $object_ref = { _column => $column,
40 _parent_resultset => $rs };
42 my $new = bless $object_ref, $class;
43 $new->throw_exception("column must be supplied") unless ($column);
53 =item Return Value: $value
57 Returns the next value of the column in the resultset (or C<undef> if
60 Much like L<DBIx::Class::ResultSet/next> but just returning the
68 $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]}) unless ($self->{_resultset});
69 my ($row) = $self->{_resultset}->cursor->next;
79 =item Return Value: @values
83 Returns all values of the column in the resultset (or C<undef> if
86 Much like L<DBIx::Class::ResultSet/all> but returns values rather
93 return map {$_->[0]} $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]})->cursor->all;
100 =item Arguments: none
102 =item Return Value: $lowest_value
106 my $first_year = $year_col->min();
108 Wrapper for ->func. Returns the lowest value of the column in the
109 resultset (or C<undef> if there are none).
115 return $self->func('MIN');
122 =item Arguments: none
124 =item Return Value: $highest_value
128 my $last_year = $year_col->max();
130 Wrapper for ->func. Returns the highest value of the column in the
131 resultset (or C<undef> if there are none).
137 return $self->func('MAX');
144 =item Arguments: none
146 =item Return Value: $sum_of_values
150 my $total = $prices_col->sum();
152 Wrapper for ->func. Returns the sum of all the values in the column of
153 the resultset. Use on varchar-like columns at your own risk.
159 return $self->func('SUM');
166 =item Arguments: $function
168 =item Return Value: $function_return_value
172 $rs = $schema->resultset("CD")->search({});
173 $length = $rs->get_column('title')->func('LENGTH');
175 Runs a query using the function on the column and returns the
176 value. Produces the following SQL:
178 SELECT LENGTH( title ) FROM cd me
184 my $function = shift;
186 my ($row) = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_column}}, as => [$self->{_column}]})->cursor->next;
194 Luke Saunders <luke.saunders@gmail.com>
200 You may distribute this code under the same terms as Perl itself.