1 package DBIx::Class::ResultSetColumn;
6 use base 'DBIx::Class';
9 # not importing first() as it will clash with our own method
14 DBIx::Class::ResultSetColumn - helpful methods for messing
15 with a single column of the resultset
19 $rs = $schema->resultset('CD')->search({ artist => 'Tool' });
20 $rs_column = $rs->get_column('year');
21 $max_year = $rs_column->max; #returns latest year
25 A convenience class used to perform operations on a specific column of
34 my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
36 Creates a new resultset column object from the resultset and column
37 passed as params. Used internally by L<DBIx::Class::ResultSet/get_column>.
42 my ($class, $rs, $column) = @_;
43 $class = ref $class if ref $class;
45 $rs->throw_exception('column must be supplied') unless $column;
47 my $orig_attrs = $rs->_resolved_attrs;
48 my $alias = $rs->current_source_alias;
49 my $rsrc = $rs->result_source;
51 # If $column can be found in the 'as' list of the parent resultset, use the
52 # corresponding element of its 'select' list (to keep any custom column
53 # definition set up with 'select' or '+select' attrs), otherwise use $column
54 # (to create a new column definition on-the-fly).
55 my $as_list = $orig_attrs->{as} || [];
56 my $select_list = $orig_attrs->{select} || [];
57 my $as_index = List::Util::first { ($as_list->[$_] || "") eq $column } 0..$#$as_list;
58 my $select = defined $as_index ? $select_list->[$as_index] : $column;
60 my ($new_parent_rs, $colmap);
61 for ($rsrc->columns, $column) {
62 if ($_ =~ /^ \Q$alias\E \. ([^\.]+) $ /x) {
66 $colmap->{"$alias.$_"} = $_;
71 # analyze the order_by, and see if it is done over a function/nonexistentcolumn
72 # if this is the case we will need to wrap a subquery since the result of RSC
73 # *must* be a single column select
76 { ! exists $colmap->{$_->[0]} }
77 ( $rsrc->schema->storage->_extract_order_criteria ($orig_attrs->{order_by} ) )
79 # nuke the prefetch before collapsing to sql
80 my $subq_rs = $rs->search;
81 $subq_rs->{attrs}{join} = $subq_rs->_merge_joinpref_attr( $subq_rs->{attrs}{join}, delete $subq_rs->{attrs}{prefetch} );
82 $new_parent_rs = $subq_rs->as_subselect_rs;
85 $new_parent_rs ||= $rs->search_rs;
86 my $new_attrs = $new_parent_rs->{attrs} ||= {};
88 # prefetch causes additional columns to be fetched, but we can not just make a new
89 # rs via the _resolved_attrs trick - we need to retain the separation between
90 # +select/+as and select/as. At the same time we want to preserve any joins that the
91 # prefetch would otherwise generate.
92 $new_attrs->{join} = $rs->_merge_joinpref_attr( $new_attrs->{join}, delete $new_attrs->{prefetch} );
94 # {collapse} would mean a has_many join was injected, which in turn means
95 # we need to group *IF WE CAN* (only if the column in question is unique)
96 if (!$orig_attrs->{group_by} && $orig_attrs->{collapse}) {
98 if ($colmap->{$select} and $rsrc->_identifying_column_set([$colmap->{$select}])) {
99 $new_attrs->{group_by} = [ $select ];
100 delete @{$new_attrs}{qw(distinct _grouped_by_distinct)}; # it is ignored when group_by is present
104 "Attempting to retrieve non-unique column '$column' on a resultset containing "
105 . 'one-to-many joins will return duplicate results.'
110 my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
118 =item Arguments: none
120 =item Return Value: \[ $sql, L<@bind_values|DBIx::Class::ResultSet/DBIC BIND VALUES> ]
124 Returns the SQL query and bind vars associated with the invocant.
126 This is generally used as the RHS for a subquery.
130 sub as_query { return shift->_resultset->as_query(@_) }
136 =item Arguments: none
138 =item Return Value: $value
142 Returns the next value of the column in the resultset (or C<undef> if
145 Much like L<DBIx::Class::ResultSet/next> but just returning the
153 # using cursor so we don't inflate anything
154 my ($row) = $self->_resultset->cursor->next;
163 =item Arguments: none
165 =item Return Value: @values
169 Returns all values of the column in the resultset (or C<undef> if
172 Much like L<DBIx::Class::ResultSet/all> but returns values rather
180 # using cursor so we don't inflate anything
181 return map { $_->[0] } $self->_resultset->cursor->all;
188 =item Arguments: none
190 =item Return Value: $self
194 Resets the underlying resultset's cursor, so you can iterate through the
195 elements of the column again.
197 Much like L<DBIx::Class::ResultSet/reset>.
203 $self->_resultset->cursor->reset;
211 =item Arguments: none
213 =item Return Value: $value
217 Resets the underlying resultset and returns the next value of the column in the
218 resultset (or C<undef> if there is none).
220 Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
227 # using cursor so we don't inflate anything
228 $self->_resultset->cursor->reset;
229 my ($row) = $self->_resultset->cursor->next;
238 =item Arguments: none
240 =item Return Value: $value
244 Much like L<DBIx::Class::ResultSet/single> fetches one and only one column
245 value using the cursor directly. If additional rows are present a warning
246 is issued before discarding the cursor.
253 my $attrs = $self->_resultset->_resolved_attrs;
254 my ($row) = $self->_resultset->result_source->storage->select_single(
255 $attrs->{from}, $attrs->{select}, $attrs->{where}, $attrs
265 =item Arguments: none
267 =item Return Value: $lowest_value
271 my $first_year = $year_col->min();
273 Wrapper for ->func. Returns the lowest value of the column in the
274 resultset (or C<undef> if there are none).
279 return shift->func('MIN');
286 =item Arguments: none
288 =item Return Value: L<$resultset|DBIx::Class::ResultSet>
292 my $rs = $year_col->min_rs();
294 Wrapper for ->func_rs for function MIN().
298 sub min_rs { return shift->func_rs('MIN') }
304 =item Arguments: none
306 =item Return Value: $highest_value
310 my $last_year = $year_col->max();
312 Wrapper for ->func. Returns the highest value of the column in the
313 resultset (or C<undef> if there are none).
318 return shift->func('MAX');
325 =item Arguments: none
327 =item Return Value: L<$resultset|DBIx::Class::ResultSet>
331 my $rs = $year_col->max_rs();
333 Wrapper for ->func_rs for function MAX().
337 sub max_rs { return shift->func_rs('MAX') }
343 =item Arguments: none
345 =item Return Value: $sum_of_values
349 my $total = $prices_col->sum();
351 Wrapper for ->func. Returns the sum of all the values in the column of
352 the resultset. Use on varchar-like columns at your own risk.
357 return shift->func('SUM');
364 =item Arguments: none
366 =item Return Value: L<$resultset|DBIx::Class::ResultSet>
370 my $rs = $year_col->sum_rs();
372 Wrapper for ->func_rs for function SUM().
376 sub sum_rs { return shift->func_rs('SUM') }
382 =item Arguments: $function
384 =item Return Value: $function_return_value
388 $rs = $schema->resultset("CD")->search({});
389 $length = $rs->get_column('title')->func('LENGTH');
391 Runs a query using the function on the column and returns the
392 value. Produces the following SQL:
394 SELECT LENGTH( title ) FROM cd me
399 my ($self,$function) = @_;
400 my $cursor = $self->func_rs($function)->cursor;
403 return map { $_->[ 0 ] } $cursor->all;
406 return ( $cursor->next )[ 0 ];
413 =item Arguments: $function
415 =item Return Value: L<$resultset|DBIx::Class::ResultSet>
419 Creates the resultset that C<func()> uses to run its query.
424 my ($self,$function) = @_;
426 my $rs = $self->{_parent_resultset};
427 my $select = $self->{_select};
430 if ($rs->_resolved_attrs->{group_by}) {
431 $select = $self->{_as};
432 $rs = $rs->as_subselect_rs;
435 $rs->search( undef, {
436 columns => { $self->{_as} => { $function => $select } }
440 =head2 throw_exception
442 See L<DBIx::Class::Schema/throw_exception> for details.
446 sub throw_exception {
449 if (ref $self && $self->{_parent_resultset}) {
450 $self->{_parent_resultset}->throw_exception(@_);
453 DBIx::Class::Exception->throw(@_);
461 # Return Value: $resultset
463 # $year_col->_resultset->next
465 # Returns the underlying resultset. Creates it from the parent resultset if
471 return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
473 select => [$self->{_select}],
481 =head1 AUTHOR AND CONTRIBUTORS
483 See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
487 You may distribute this code under the same terms as Perl itself.