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 $orig_attrs = $rs->_resolved_attrs;
43 my $new_parent_rs = $rs->search_rs;
45 # prefetch causes additional columns to be fetched, but we can not just make a new
46 # rs via the _resolved_attrs trick - we need to retain the separation between
47 # +select/+as and select/as. At the same time we want to preserve any joins that the
48 # prefetch would otherwise generate.
50 my $new_attrs = $new_parent_rs->{attrs} ||= {};
51 $new_attrs->{join} = $rs->_merge_attr( delete $new_attrs->{join}, delete $new_attrs->{prefetch} );
53 # If $column can be found in the 'as' list of the parent resultset, use the
54 # corresponding element of its 'select' list (to keep any custom column
55 # definition set up with 'select' or '+select' attrs), otherwise use $column
56 # (to create a new column definition on-the-fly).
58 my $as_list = $orig_attrs->{as} || [];
59 my $select_list = $orig_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 # {collapse} would mean a has_many join was injected, which in turn means
64 # we need to group IF WE CAN (only if the column in question is unique)
65 if (!$new_attrs->{group_by} && keys %{$orig_attrs->{collapse}}) {
67 # scan for a constraint that would contain our column only - that'd be proof
69 my $constraints = { $rs->result_source->unique_constraints };
70 for my $constraint_columns ( values %$constraints ) {
72 next unless @$constraint_columns == 1;
74 my $col = $constraint_columns->[0];
75 my $fqcol = join ('.', $new_attrs->{alias}, $col);
77 if ($col eq $select or $fqcol eq $select) {
78 $new_attrs->{group_by} = [ $select ];
84 my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
88 =head2 as_query (EXPERIMENTAL)
94 =item Return Value: \[ $sql, @bind ]
98 Returns the SQL query and bind vars associated with the invocant.
100 This is generally used as the RHS for a subquery.
102 B<NOTE>: This feature is still experimental.
106 sub as_query { return shift->_resultset->as_query(@_) }
112 =item Arguments: none
114 =item Return Value: $value
118 Returns the next value of the column in the resultset (or C<undef> if
121 Much like L<DBIx::Class::ResultSet/next> but just returning the
128 my ($row) = $self->_resultset->cursor->next;
136 =item Arguments: none
138 =item Return Value: @values
142 Returns all values of the column in the resultset (or C<undef> if
145 Much like L<DBIx::Class::ResultSet/all> but returns values rather
152 return map { $_->[0] } $self->_resultset->cursor->all;
159 =item Arguments: none
161 =item Return Value: $self
165 Resets the underlying resultset's cursor, so you can iterate through the
166 elements of the column again.
168 Much like L<DBIx::Class::ResultSet/reset>.
174 $self->_resultset->cursor->reset;
182 =item Arguments: none
184 =item Return Value: $value
188 Resets the underlying resultset and returns the next value of the column in the
189 resultset (or C<undef> if there is none).
191 Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
197 $self->_resultset->reset();
198 my ($row) = $self->_resultset->cursor->next;
206 =item Arguments: none
208 =item Return Value: $lowest_value
212 my $first_year = $year_col->min();
214 Wrapper for ->func. Returns the lowest value of the column in the
215 resultset (or C<undef> if there are none).
220 return shift->func('MIN');
227 =item Arguments: none
229 =item Return Value: $resultset
233 my $rs = $year_col->min_rs();
235 Wrapper for ->func_rs for function MIN().
239 sub min_rs { return shift->func_rs('MIN') }
245 =item Arguments: none
247 =item Return Value: $highest_value
251 my $last_year = $year_col->max();
253 Wrapper for ->func. Returns the highest value of the column in the
254 resultset (or C<undef> if there are none).
259 return shift->func('MAX');
266 =item Arguments: none
268 =item Return Value: $resultset
272 my $rs = $year_col->max_rs();
274 Wrapper for ->func_rs for function MAX().
278 sub max_rs { return shift->func_rs('MAX') }
284 =item Arguments: none
286 =item Return Value: $sum_of_values
290 my $total = $prices_col->sum();
292 Wrapper for ->func. Returns the sum of all the values in the column of
293 the resultset. Use on varchar-like columns at your own risk.
298 return shift->func('SUM');
305 =item Arguments: none
307 =item Return Value: $resultset
311 my $rs = $year_col->sum_rs();
313 Wrapper for ->func_rs for function SUM().
317 sub sum_rs { return shift->func_rs('SUM') }
323 =item Arguments: $function
325 =item Return Value: $function_return_value
329 $rs = $schema->resultset("CD")->search({});
330 $length = $rs->get_column('title')->func('LENGTH');
332 Runs a query using the function on the column and returns the
333 value. Produces the following SQL:
335 SELECT LENGTH( title ) FROM cd me
340 my ($self,$function) = @_;
341 my $cursor = $self->func_rs($function)->cursor;
344 return map { $_->[ 0 ] } $cursor->all;
347 return ( $cursor->next )[ 0 ];
354 =item Arguments: $function
356 =item Return Value: $resultset
360 Creates the resultset that C<func()> uses to run its query.
365 my ($self,$function) = @_;
366 return $self->{_parent_resultset}->search(
368 select => {$function => $self->{_select}},
369 as => [$self->{_as}],
374 =head2 throw_exception
376 See L<DBIx::Class::Schema/throw_exception> for details.
380 sub throw_exception {
382 if (ref $self && $self->{_parent_resultset}) {
383 $self->{_parent_resultset}->throw_exception(@_)
393 # Return Value: $resultset
395 # $year_col->_resultset->next
397 # Returns the underlying resultset. Creates it from the parent resultset if
403 return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
405 select => [$self->{_select}],
415 Luke Saunders <luke.saunders@gmail.com>
421 You may distribute this code under the same terms as Perl itself.