1 package DBIx::Class::ResultSetColumn;
6 use base 'DBIx::Class';
8 use DBIx::Class::Exception;
13 DBIx::Class::ResultSetColumn - helpful methods for messing
14 with a single column of the resultset
18 $rs = $schema->resultset('CD')->search({ artist => 'Tool' });
19 $rs_column = $rs->get_column('year');
20 $max_year = $rs_column->max; #returns latest year
24 A convenience class used to perform operations on a specific column of
33 my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
35 Creates a new resultset column object from the resultset and column
36 passed as params. Used internally by L<DBIx::Class::ResultSet/get_column>.
41 my ($class, $rs, $column) = @_;
42 $class = ref $class if ref $class;
44 $rs->throw_exception("column must be supplied") unless $column;
46 my $orig_attrs = $rs->_resolved_attrs;
47 my $new_parent_rs = $rs->search_rs;
49 # prefetch causes additional columns to be fetched, but we can not just make a new
50 # rs via the _resolved_attrs trick - we need to retain the separation between
51 # +select/+as and select/as. At the same time we want to preserve any joins that the
52 # prefetch would otherwise generate.
54 my $new_attrs = $new_parent_rs->{attrs} ||= {};
55 $new_attrs->{join} = $rs->_merge_attr( delete $new_attrs->{join}, delete $new_attrs->{prefetch} );
57 # If $column can be found in the 'as' list of the parent resultset, use the
58 # corresponding element of its 'select' list (to keep any custom column
59 # definition set up with 'select' or '+select' attrs), otherwise use $column
60 # (to create a new column definition on-the-fly).
62 my $as_list = $orig_attrs->{as} || [];
63 my $select_list = $orig_attrs->{select} || [];
64 my $as_index = List::Util::first { ($as_list->[$_] || "") eq $column } 0..$#$as_list;
65 my $select = defined $as_index ? $select_list->[$as_index] : $column;
67 # {collapse} would mean a has_many join was injected, which in turn means
68 # we need to group IF WE CAN (only if the column in question is unique)
69 if (!$new_attrs->{group_by} && keys %{$orig_attrs->{collapse}}) {
71 # scan for a constraint that would contain our column only - that'd be proof
73 my $constraints = { $rs->result_source->unique_constraints };
74 for my $constraint_columns ( values %$constraints ) {
76 next unless @$constraint_columns == 1;
78 my $col = $constraint_columns->[0];
79 my $fqcol = join ('.', $new_attrs->{alias}, $col);
81 if ($col eq $select or $fqcol eq $select) {
82 $new_attrs->{group_by} = [ $select ];
88 my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
92 =head2 as_query (EXPERIMENTAL)
98 =item Return Value: \[ $sql, @bind ]
102 Returns the SQL query and bind vars associated with the invocant.
104 This is generally used as the RHS for a subquery.
106 B<NOTE>: This feature is still experimental.
110 sub as_query { return shift->_resultset->as_query(@_) }
116 =item Arguments: none
118 =item Return Value: $value
122 Returns the next value of the column in the resultset (or C<undef> if
125 Much like L<DBIx::Class::ResultSet/next> but just returning the
133 # using cursor so we don't inflate anything
134 my ($row) = $self->_resultset->cursor->next;
143 =item Arguments: none
145 =item Return Value: @values
149 Returns all values of the column in the resultset (or C<undef> if
152 Much like L<DBIx::Class::ResultSet/all> but returns values rather
160 # using cursor so we don't inflate anything
161 return map { $_->[0] } $self->_resultset->cursor->all;
168 =item Arguments: none
170 =item Return Value: $self
174 Resets the underlying resultset's cursor, so you can iterate through the
175 elements of the column again.
177 Much like L<DBIx::Class::ResultSet/reset>.
183 $self->_resultset->cursor->reset;
191 =item Arguments: none
193 =item Return Value: $value
197 Resets the underlying resultset and returns the next value of the column in the
198 resultset (or C<undef> if there is none).
200 Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
207 # using cursor so we don't inflate anything
208 $self->_resultset->cursor->reset;
209 my ($row) = $self->_resultset->cursor->next;
218 =item Arguments: none
220 =item Return Value: $value
224 Much like L<DBIx::Class::ResultSet/single> fetches one and only one column
225 value using the cursor directly. If additional rows are present a warning
226 is issued before discarding the cursor.
233 my $attrs = $self->_resultset->_resolved_attrs;
234 my ($row) = $self->_resultset->result_source->storage->select_single(
235 $attrs->{from}, $attrs->{select}, $attrs->{where}, $attrs
245 =item Arguments: none
247 =item Return Value: $lowest_value
251 my $first_year = $year_col->min();
253 Wrapper for ->func. Returns the lowest value of the column in the
254 resultset (or C<undef> if there are none).
259 return shift->func('MIN');
266 =item Arguments: none
268 =item Return Value: $resultset
272 my $rs = $year_col->min_rs();
274 Wrapper for ->func_rs for function MIN().
278 sub min_rs { return shift->func_rs('MIN') }
284 =item Arguments: none
286 =item Return Value: $highest_value
290 my $last_year = $year_col->max();
292 Wrapper for ->func. Returns the highest value of the column in the
293 resultset (or C<undef> if there are none).
298 return shift->func('MAX');
305 =item Arguments: none
307 =item Return Value: $resultset
311 my $rs = $year_col->max_rs();
313 Wrapper for ->func_rs for function MAX().
317 sub max_rs { return shift->func_rs('MAX') }
323 =item Arguments: none
325 =item Return Value: $sum_of_values
329 my $total = $prices_col->sum();
331 Wrapper for ->func. Returns the sum of all the values in the column of
332 the resultset. Use on varchar-like columns at your own risk.
337 return shift->func('SUM');
344 =item Arguments: none
346 =item Return Value: $resultset
350 my $rs = $year_col->sum_rs();
352 Wrapper for ->func_rs for function SUM().
356 sub sum_rs { return shift->func_rs('SUM') }
362 =item Arguments: $function
364 =item Return Value: $function_return_value
368 $rs = $schema->resultset("CD")->search({});
369 $length = $rs->get_column('title')->func('LENGTH');
371 Runs a query using the function on the column and returns the
372 value. Produces the following SQL:
374 SELECT LENGTH( title ) FROM cd me
379 my ($self,$function) = @_;
380 my $cursor = $self->func_rs($function)->cursor;
383 return map { $_->[ 0 ] } $cursor->all;
386 return ( $cursor->next )[ 0 ];
393 =item Arguments: $function
395 =item Return Value: $resultset
399 Creates the resultset that C<func()> uses to run its query.
404 my ($self,$function) = @_;
405 return $self->{_parent_resultset}->search(
407 select => {$function => $self->{_select}},
408 as => [$self->{_as}],
413 =head2 throw_exception
415 See L<DBIx::Class::Schema/throw_exception> for details.
419 sub throw_exception {
422 if (ref $self && $self->{_parent_resultset}) {
423 $self->{_parent_resultset}->throw_exception(@_);
426 DBIx::Class::Exception->throw(@_);
434 # Return Value: $resultset
436 # $year_col->_resultset->next
438 # Returns the underlying resultset. Creates it from the parent resultset if
444 return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
446 select => [$self->{_select}],
456 Luke Saunders <luke.saunders@gmail.com>
462 You may distribute this code under the same terms as Perl itself.