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;
57 =head2 as_query (EXPERIMENTAL)
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.
71 B<NOTE>: This feature is still experimental.
75 sub as_query { return shift->_resultset->as_query }
83 =item Return Value: $value
87 Returns the next value of the column in the resultset (or C<undef> if
90 Much like L<DBIx::Class::ResultSet/next> but just returning the
97 my ($row) = $self->_resultset->cursor->next;
105 =item Arguments: none
107 =item Return Value: @values
111 Returns all values of the column in the resultset (or C<undef> if
114 Much like L<DBIx::Class::ResultSet/all> but returns values rather
121 return map { $_->[0] } $self->_resultset->cursor->all;
128 =item Arguments: none
130 =item Return Value: $self
134 Resets the underlying resultset's cursor, so you can iterate through the
135 elements of the column again.
137 Much like L<DBIx::Class::ResultSet/reset>.
143 $self->_resultset->cursor->reset;
151 =item Arguments: none
153 =item Return Value: $value
157 Resets the underlying resultset and returns the next value of the column in the
158 resultset (or C<undef> if there is none).
160 Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
166 my ($row) = $self->_resultset->cursor->reset->next;
174 =item Arguments: none
176 =item Return Value: $lowest_value
180 my $first_year = $year_col->min();
182 Wrapper for ->func. Returns the lowest value of the column in the
183 resultset (or C<undef> if there are none).
188 return shift->func('MIN');
195 =item Arguments: none
197 =item Return Value: $resultset
201 my $rs = $year_col->min_rs();
203 Wrapper for ->func_rs for function MIN().
207 sub min_rs { return shift->func_rs('MIN') }
213 =item Arguments: none
215 =item Return Value: $highest_value
219 my $last_year = $year_col->max();
221 Wrapper for ->func. Returns the highest value of the column in the
222 resultset (or C<undef> if there are none).
227 return shift->func('MAX');
234 =item Arguments: none
236 =item Return Value: $resultset
240 my $rs = $year_col->max_rs();
242 Wrapper for ->func_rs for function MAX().
246 sub max_rs { return shift->func_rs('MAX') }
252 =item Arguments: none
254 =item Return Value: $sum_of_values
258 my $total = $prices_col->sum();
260 Wrapper for ->func. Returns the sum of all the values in the column of
261 the resultset. Use on varchar-like columns at your own risk.
266 return shift->func('SUM');
273 =item Arguments: none
275 =item Return Value: $resultset
279 my $rs = $year_col->sum_rs();
281 Wrapper for ->func_rs for function SUM().
285 sub sum_rs { return shift->func_rs('SUM') }
291 =item Arguments: $function
293 =item Return Value: $function_return_value
297 $rs = $schema->resultset("CD")->search({});
298 $length = $rs->get_column('title')->func('LENGTH');
300 Runs a query using the function on the column and returns the
301 value. Produces the following SQL:
303 SELECT LENGTH( title ) FROM cd me
308 my ($self,$function) = @_;
309 my $cursor = $self->func_rs($function)->cursor;
312 return map { $_->[ 0 ] } $cursor->all;
315 return ( $cursor->next )[ 0 ];
322 =item Arguments: $function
324 =item Return Value: $resultset
328 Creates the resultset that C<func()> uses to run its query.
333 my ($self,$function) = @_;
334 return $self->{_parent_resultset}->search(
336 select => {$function => $self->{_select}},
337 as => [$self->{_as}],
342 =head2 throw_exception
344 See L<DBIx::Class::Schema/throw_exception> for details.
348 sub throw_exception {
350 if (ref $self && $self->{_parent_resultset}) {
351 $self->{_parent_resultset}->throw_exception(@_)
361 # Return Value: $resultset
363 # $year_col->_resultset->next
365 # Returns the underlying resultset. Creates it from the parent resultset if
371 return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
373 select => [$self->{_select}],
383 Luke Saunders <luke.saunders@gmail.com>
389 You may distribute this code under the same terms as Perl itself.