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 a resultset.
27 my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
29 Creates a new resultset column object from the resultset and column passed as params
34 my ($class, $rs, $column) = @_;
35 $class = ref $class if ref $class;
37 my $object_ref = { _column => $column,
38 _parent_resultset => $rs };
40 my $new = bless $object_ref, $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 (C<undef> is there is none).
57 Much like $rs->next but just returning the one value
64 $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]}) unless ($self->{_resultset});
65 my ($row) = $self->{_resultset}->cursor->next;
75 =item Return Value: @values
79 Returns all values of the column in the resultset (C<undef> is there are none).
81 Much like $rs->all but returns values rather than row objects
87 return map {$_->[0]} $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]})->cursor->all;
96 =item Return Value: $lowest_value
100 Wrapper for ->func. Returns the lowest value of the column in the resultset (C<undef> is there are none).
106 return $self->func('MIN');
113 =item Arguments: none
115 =item Return Value: $highest_value
119 Wrapper for ->func. Returns the highest value of the column in the resultset (C<undef> is there are none).
125 return $self->func('MAX');
132 =item Arguments: none
134 =item Return Value: $sum_of_values
138 Wrapper for ->func. Returns the sum of all the values in the column of the resultset. Use on varchar-like columns at your own risk.
144 return $self->func('SUM');
151 =item Arguments: $function
153 =item Return Value: $function_return_value
157 Runs a query using the function on the column and returns the value. For example
158 $rs = $schema->resultset("CD")->search({});
159 $length = $rs->get_column('title')->func('LENGTH');
161 Produces the following SQL
162 SELECT LENGTH( title ) from cd me
168 my $function = shift;
170 my ($row) = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_column}}, as => [$self->{_column}]})->cursor->next;
178 Luke Saunders <luke.saunders@gmail.com>
182 You may distribute this code under the same terms as Perl itself.