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;
40 $rs->throw_exception("column must be supplied") unless $column;
42 my $new_parent_rs = $rs->search_rs; # we don't want to mess up the original, so clone it
44 # prefetch causes additional columns to be fetched, but we can not just make a new
45 # rs via the _resolved_attrs trick - we need to retain the separation between
46 # +select/+as and select/as. At the same time we want to preserve any joins that the
47 # prefetch would otherwise generate.
48 my $init_attrs = $new_parent_rs->{attrs} ||= {};
49 delete $init_attrs->{collapse};
50 $init_attrs->{join} = $rs->_merge_attr( delete $init_attrs->{join}, delete $init_attrs->{prefetch} );
52 # If $column can be found in the 'as' list of the parent resultset, use the
53 # corresponding element of its 'select' list (to keep any custom column
54 # definition set up with 'select' or '+select' attrs), otherwise use $column
55 # (to create a new column definition on-the-fly).
56 my $attrs = $new_parent_rs->_resolved_attrs;
58 my $as_list = $attrs->{as} || [];
59 my $select_list = $attrs->{select} || [];
60 my $as_index = List::Util::first { ($as_list->[$_] || "") eq $column } 0..$#$as_list;
61 my $select = defined $as_index ? $select_list->[$as_index] : $column;
63 my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
67 =head2 as_query (EXPERIMENTAL)
73 =item Return Value: \[ $sql, @bind ]
77 Returns the SQL query and bind vars associated with the invocant.
79 This is generally used as the RHS for a subquery.
81 B<NOTE>: This feature is still experimental.
85 sub as_query { return shift->_resultset->as_query(@_) }
93 =item Return Value: $value
97 Returns the next value of the column in the resultset (or C<undef> if
100 Much like L<DBIx::Class::ResultSet/next> but just returning the
107 my ($row) = $self->_resultset->cursor->next;
115 =item Arguments: none
117 =item Return Value: @values
121 Returns all values of the column in the resultset (or C<undef> if
124 Much like L<DBIx::Class::ResultSet/all> but returns values rather
131 return map { $_->[0] } $self->_resultset->cursor->all;
138 =item Arguments: none
140 =item Return Value: $self
144 Resets the underlying resultset's cursor, so you can iterate through the
145 elements of the column again.
147 Much like L<DBIx::Class::ResultSet/reset>.
153 $self->_resultset->cursor->reset;
161 =item Arguments: none
163 =item Return Value: $value
167 Resets the underlying resultset and returns the next value of the column in the
168 resultset (or C<undef> if there is none).
170 Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
176 my ($row) = $self->_resultset->cursor->reset->next;
184 =item Arguments: none
186 =item Return Value: $lowest_value
190 my $first_year = $year_col->min();
192 Wrapper for ->func. Returns the lowest value of the column in the
193 resultset (or C<undef> if there are none).
198 return shift->func('MIN');
205 =item Arguments: none
207 =item Return Value: $resultset
211 my $rs = $year_col->min_rs();
213 Wrapper for ->func_rs for function MIN().
217 sub min_rs { return shift->func_rs('MIN') }
223 =item Arguments: none
225 =item Return Value: $highest_value
229 my $last_year = $year_col->max();
231 Wrapper for ->func. Returns the highest value of the column in the
232 resultset (or C<undef> if there are none).
237 return shift->func('MAX');
244 =item Arguments: none
246 =item Return Value: $resultset
250 my $rs = $year_col->max_rs();
252 Wrapper for ->func_rs for function MAX().
256 sub max_rs { return shift->func_rs('MAX') }
262 =item Arguments: none
264 =item Return Value: $sum_of_values
268 my $total = $prices_col->sum();
270 Wrapper for ->func. Returns the sum of all the values in the column of
271 the resultset. Use on varchar-like columns at your own risk.
276 return shift->func('SUM');
283 =item Arguments: none
285 =item Return Value: $resultset
289 my $rs = $year_col->sum_rs();
291 Wrapper for ->func_rs for function SUM().
295 sub sum_rs { return shift->func_rs('SUM') }
301 =item Arguments: $function
303 =item Return Value: $function_return_value
307 $rs = $schema->resultset("CD")->search({});
308 $length = $rs->get_column('title')->func('LENGTH');
310 Runs a query using the function on the column and returns the
311 value. Produces the following SQL:
313 SELECT LENGTH( title ) FROM cd me
318 my ($self,$function) = @_;
319 my $cursor = $self->func_rs($function)->cursor;
322 return map { $_->[ 0 ] } $cursor->all;
325 return ( $cursor->next )[ 0 ];
332 =item Arguments: $function
334 =item Return Value: $resultset
338 Creates the resultset that C<func()> uses to run its query.
343 my ($self,$function) = @_;
344 return $self->{_parent_resultset}->search(
346 select => {$function => $self->{_select}},
347 as => [$self->{_as}],
352 =head2 throw_exception
354 See L<DBIx::Class::Schema/throw_exception> for details.
358 sub throw_exception {
360 if (ref $self && $self->{_parent_resultset}) {
361 $self->{_parent_resultset}->throw_exception(@_)
371 # Return Value: $resultset
373 # $year_col->_resultset->next
375 # Returns the underlying resultset. Creates it from the parent resultset if
381 return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
383 select => [$self->{_select}],
393 Luke Saunders <luke.saunders@gmail.com>
399 You may distribute this code under the same terms as Perl itself.