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 # If $column can be found in the 'as' list of the parent resultset, use the
44 # corresponding element of its 'select' list (to keep any custom column
45 # definition set up with 'select' or '+select' attrs), otherwise use $column
46 # (to create a new column definition on-the-fly).
47 my $as_list = $attrs->{as} || [];
48 my $select_list = $attrs->{select} || [];
49 my $as_index = List::Util::first { ($as_list->[$_] || "") eq $column } 0..$#$as_list;
50 my $select = defined $as_index ? $select_list->[$as_index] : $column;
52 my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
53 $new->throw_exception("column must be supplied") unless $column;
63 =item Return Value: \[ $sql, @bind ]
67 Returns the SQL query and bind vars associated with the invocant.
69 This is generally used as the RHS for a subquery.
73 sub as_query { return shift->_resultset->as_query }
81 =item Return Value: $value
85 Returns the next value of the column in the resultset (or C<undef> if
88 Much like L<DBIx::Class::ResultSet/next> but just returning the
95 my ($row) = $self->_resultset->cursor->next;
103 =item Arguments: none
105 =item Return Value: @values
109 Returns all values of the column in the resultset (or C<undef> if
112 Much like L<DBIx::Class::ResultSet/all> but returns values rather
119 return map { $_->[0] } $self->_resultset->cursor->all;
126 =item Arguments: none
128 =item Return Value: $self
132 Resets the underlying resultset's cursor, so you can iterate through the
133 elements of the column again.
135 Much like L<DBIx::Class::ResultSet/reset>.
141 $self->_resultset->cursor->reset;
149 =item Arguments: none
151 =item Return Value: $value
155 Resets the underlying resultset and returns the next value of the column in the
156 resultset (or C<undef> if there is none).
158 Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
164 my ($row) = $self->_resultset->cursor->reset->next;
172 =item Arguments: none
174 =item Return Value: $lowest_value
178 my $first_year = $year_col->min();
180 Wrapper for ->func. Returns the lowest value of the column in the
181 resultset (or C<undef> if there are none).
186 return shift->func('MIN');
193 =item Arguments: none
195 =item Return Value: $resultset
199 my $rs = $year_col->min_rs();
201 Wrapper for ->func_rs for function MIN().
205 sub min_rs { return shift->func_rs('MIN') }
211 =item Arguments: none
213 =item Return Value: $highest_value
217 my $last_year = $year_col->max();
219 Wrapper for ->func. Returns the highest value of the column in the
220 resultset (or C<undef> if there are none).
225 return shift->func('MAX');
232 =item Arguments: none
234 =item Return Value: $resultset
238 my $rs = $year_col->max_rs();
240 Wrapper for ->func_rs for function MAX().
244 sub max_rs { return shift->func_rs('MAX') }
250 =item Arguments: none
252 =item Return Value: $sum_of_values
256 my $total = $prices_col->sum();
258 Wrapper for ->func. Returns the sum of all the values in the column of
259 the resultset. Use on varchar-like columns at your own risk.
264 return shift->func('SUM');
271 =item Arguments: none
273 =item Return Value: $resultset
277 my $rs = $year_col->sum_rs();
279 Wrapper for ->func_rs for function SUM().
283 sub sum_rs { return shift->func_rs('SUM') }
289 =item Arguments: $function
291 =item Return Value: $function_return_value
295 $rs = $schema->resultset("CD")->search({});
296 $length = $rs->get_column('title')->func('LENGTH');
298 Runs a query using the function on the column and returns the
299 value. Produces the following SQL:
301 SELECT LENGTH( title ) FROM cd me
306 my ($self,$function) = @_;
307 my $cursor = $self->func_rs($function)->cursor;
310 return map { $_->[ 0 ] } $cursor->all;
313 return ( $cursor->next )[ 0 ];
320 =item Arguments: $function
322 =item Return Value: $resultset
326 Creates the resultset that C<func()> uses to run its query.
331 my ($self,$function) = @_;
332 return $self->{_parent_resultset}->search(
334 select => {$function => $self->{_select}},
335 as => [$self->{_as}],
340 =head2 throw_exception
342 See L<DBIx::Class::Schema/throw_exception> for details.
346 sub throw_exception {
348 if (ref $self && $self->{_parent_resultset}) {
349 $self->{_parent_resultset}->throw_exception(@_)
359 # Return Value: $resultset
361 # $year_col->_resultset->next
363 # Returns the underlying resultset. Creates it from the parent resultset if
369 return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
371 select => [$self->{_select}],
381 Luke Saunders <luke.saunders@gmail.com>
387 You may distribute this code under the same terms as Perl itself.