1 package DBIx::Class::ResultSetColumn;
6 use base 'DBIx::Class';
8 use DBIx::Class::Exception;
10 # not importing first() as it will clash with our own method
15 DBIx::Class::ResultSetColumn - helpful methods for messing
16 with a single column of the resultset
20 $rs = $schema->resultset('CD')->search({ artist => 'Tool' });
21 $rs_column = $rs->get_column('year');
22 $max_year = $rs_column->max; #returns latest year
26 A convenience class used to perform operations on a specific column of
35 my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
37 Creates a new resultset column object from the resultset and column
38 passed as params. Used internally by L<DBIx::Class::ResultSet/get_column>.
43 my ($class, $rs, $column) = @_;
44 $class = ref $class if ref $class;
46 $rs->throw_exception('column must be supplied') unless $column;
48 my $orig_attrs = $rs->_resolved_attrs;
49 my $alias = $rs->current_source_alias;
50 my $rsrc = $rs->result_source;
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 $as_list = $orig_attrs->{as} || [];
57 my $select_list = $orig_attrs->{select} || [];
58 my $as_index = List::Util::first { ($as_list->[$_] || "") eq $column } 0..$#$as_list;
59 my $select = defined $as_index ? $select_list->[$as_index] : $column;
61 my ($new_parent_rs, $colmap);
62 for ($rsrc->columns, $column) {
63 if ($_ =~ /^ \Q$alias\E \. ([^\.]+) $ /x) {
67 $colmap->{"$alias.$_"} = $_;
72 # analyze the order_by, and see if it is done over a function/nonexistentcolumn
73 # if this is the case we will need to wrap a subquery since the result of RSC
74 # *must* be a single column select
77 { ! exists $colmap->{$_->[0]} }
78 ( $rsrc->schema->storage->_extract_order_criteria ($orig_attrs->{order_by} ) )
80 # nuke the prefetch before collapsing to sql
81 my $subq_rs = $rs->search;
82 $subq_rs->{attrs}{join} = $subq_rs->_merge_joinpref_attr( $subq_rs->{attrs}{join}, delete $subq_rs->{attrs}{prefetch} );
83 $new_parent_rs = $subq_rs->as_subselect_rs;
86 $new_parent_rs ||= $rs->search_rs;
87 my $new_attrs = $new_parent_rs->{attrs} ||= {};
89 # prefetch causes additional columns to be fetched, but we can not just make a new
90 # rs via the _resolved_attrs trick - we need to retain the separation between
91 # +select/+as and select/as. At the same time we want to preserve any joins that the
92 # prefetch would otherwise generate.
93 $new_attrs->{join} = $rs->_merge_joinpref_attr( $new_attrs->{join}, delete $new_attrs->{prefetch} );
95 # {collapse} would mean a has_many join was injected, which in turn means
96 # we need to group *IF WE CAN* (only if the column in question is unique)
97 if (!$orig_attrs->{group_by} && keys %{$orig_attrs->{collapse}}) {
99 if ($colmap->{$select} and $rsrc->_identifying_column_set([$colmap->{$select}])) {
100 $new_attrs->{group_by} = [ $select ];
101 delete $new_attrs->{distinct}; # it is ignored when group_by is present
105 "Attempting to retrieve non-unique column '$column' on a resultset containing "
106 . 'one-to-many joins will return duplicate results.'
111 my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
119 =item Arguments: none
121 =item Return Value: \[ $sql, @bind ]
125 Returns the SQL query and bind vars associated with the invocant.
127 This is generally used as the RHS for a subquery.
131 sub as_query { return shift->_resultset->as_query(@_) }
137 =item Arguments: none
139 =item Return Value: $value
143 Returns the next value of the column in the resultset (or C<undef> if
146 Much like L<DBIx::Class::ResultSet/next> but just returning the
154 # using cursor so we don't inflate anything
155 my ($row) = $self->_resultset->cursor->next;
164 =item Arguments: none
166 =item Return Value: @values
170 Returns all values of the column in the resultset (or C<undef> if
173 Much like L<DBIx::Class::ResultSet/all> but returns values rather
181 # using cursor so we don't inflate anything
182 return map { $_->[0] } $self->_resultset->cursor->all;
189 =item Arguments: none
191 =item Return Value: $self
195 Resets the underlying resultset's cursor, so you can iterate through the
196 elements of the column again.
198 Much like L<DBIx::Class::ResultSet/reset>.
204 $self->_resultset->cursor->reset;
212 =item Arguments: none
214 =item Return Value: $value
218 Resets the underlying resultset and returns the next value of the column in the
219 resultset (or C<undef> if there is none).
221 Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
228 # using cursor so we don't inflate anything
229 $self->_resultset->cursor->reset;
230 my ($row) = $self->_resultset->cursor->next;
239 =item Arguments: none
241 =item Return Value: $value
245 Much like L<DBIx::Class::ResultSet/single> fetches one and only one column
246 value using the cursor directly. If additional rows are present a warning
247 is issued before discarding the cursor.
254 my $attrs = $self->_resultset->_resolved_attrs;
255 my ($row) = $self->_resultset->result_source->storage->select_single(
256 $attrs->{from}, $attrs->{select}, $attrs->{where}, $attrs
266 =item Arguments: none
268 =item Return Value: $lowest_value
272 my $first_year = $year_col->min();
274 Wrapper for ->func. Returns the lowest value of the column in the
275 resultset (or C<undef> if there are none).
280 return shift->func('MIN');
287 =item Arguments: none
289 =item Return Value: L<$resultset|DBIx::Class::ResultSet>
293 my $rs = $year_col->min_rs();
295 Wrapper for ->func_rs for function MIN().
299 sub min_rs { return shift->func_rs('MIN') }
305 =item Arguments: none
307 =item Return Value: $highest_value
311 my $last_year = $year_col->max();
313 Wrapper for ->func. Returns the highest value of the column in the
314 resultset (or C<undef> if there are none).
319 return shift->func('MAX');
326 =item Arguments: none
328 =item Return Value: L<$resultset|DBIx::Class::ResultSet>
332 my $rs = $year_col->max_rs();
334 Wrapper for ->func_rs for function MAX().
338 sub max_rs { return shift->func_rs('MAX') }
344 =item Arguments: none
346 =item Return Value: $sum_of_values
350 my $total = $prices_col->sum();
352 Wrapper for ->func. Returns the sum of all the values in the column of
353 the resultset. Use on varchar-like columns at your own risk.
358 return shift->func('SUM');
365 =item Arguments: none
367 =item Return Value: L<$resultset|DBIx::Class::ResultSet>
371 my $rs = $year_col->sum_rs();
373 Wrapper for ->func_rs for function SUM().
377 sub sum_rs { return shift->func_rs('SUM') }
383 =item Arguments: $function
385 =item Return Value: $function_return_value
389 $rs = $schema->resultset("CD")->search({});
390 $length = $rs->get_column('title')->func('LENGTH');
392 Runs a query using the function on the column and returns the
393 value. Produces the following SQL:
395 SELECT LENGTH( title ) FROM cd me
400 my ($self,$function) = @_;
401 my $cursor = $self->func_rs($function)->cursor;
404 return map { $_->[ 0 ] } $cursor->all;
407 return ( $cursor->next )[ 0 ];
414 =item Arguments: $function
416 =item Return Value: L<$resultset|DBIx::Class::ResultSet>
420 Creates the resultset that C<func()> uses to run its query.
425 my ($self,$function) = @_;
426 return $self->{_parent_resultset}->search(
428 select => {$function => $self->{_select}},
429 as => [$self->{_as}],
434 =head2 throw_exception
436 See L<DBIx::Class::Schema/throw_exception> for details.
440 sub throw_exception {
443 if (ref $self && $self->{_parent_resultset}) {
444 $self->{_parent_resultset}->throw_exception(@_);
447 DBIx::Class::Exception->throw(@_);
455 # Return Value: $resultset
457 # $year_col->_resultset->next
459 # Returns the underlying resultset. Creates it from the parent resultset if
465 return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
467 select => [$self->{_select}],
475 =head1 AUTHOR AND CONTRIBUTORS
477 See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
481 You may distribute this code under the same terms as Perl itself.