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
47 for my $attr (qw/prefetch collapse/) {
48 for (qw/attrs _attrs/) {
49 delete $new_parent_rs->{$_}{$attr} if ref $new_parent_rs->{$_};
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).
57 my $attrs = $new_parent_rs->_resolved_attrs;
59 my $as_list = $attrs->{as} || [];
60 my $select_list = $attrs->{select} || [];
61 my $as_index = List::Util::first { ($as_list->[$_] || "") eq $column } 0..$#$as_list;
62 my $select = defined $as_index ? $select_list->[$as_index] : $column;
64 my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
68 =head2 as_query (EXPERIMENTAL)
74 =item Return Value: \[ $sql, @bind ]
78 Returns the SQL query and bind vars associated with the invocant.
80 This is generally used as the RHS for a subquery.
82 B<NOTE>: This feature is still experimental.
86 sub as_query { return shift->_resultset->as_query(@_) }
94 =item Return Value: $value
98 Returns the next value of the column in the resultset (or C<undef> if
101 Much like L<DBIx::Class::ResultSet/next> but just returning the
108 my ($row) = $self->_resultset->cursor->next;
116 =item Arguments: none
118 =item Return Value: @values
122 Returns all values of the column in the resultset (or C<undef> if
125 Much like L<DBIx::Class::ResultSet/all> but returns values rather
132 return map { $_->[0] } $self->_resultset->cursor->all;
139 =item Arguments: none
141 =item Return Value: $self
145 Resets the underlying resultset's cursor, so you can iterate through the
146 elements of the column again.
148 Much like L<DBIx::Class::ResultSet/reset>.
154 $self->_resultset->cursor->reset;
162 =item Arguments: none
164 =item Return Value: $value
168 Resets the underlying resultset and returns the next value of the column in the
169 resultset (or C<undef> if there is none).
171 Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
177 my ($row) = $self->_resultset->cursor->reset->next;
185 =item Arguments: none
187 =item Return Value: $lowest_value
191 my $first_year = $year_col->min();
193 Wrapper for ->func. Returns the lowest value of the column in the
194 resultset (or C<undef> if there are none).
199 return shift->func('MIN');
206 =item Arguments: none
208 =item Return Value: $resultset
212 my $rs = $year_col->min_rs();
214 Wrapper for ->func_rs for function MIN().
218 sub min_rs { return shift->func_rs('MIN') }
224 =item Arguments: none
226 =item Return Value: $highest_value
230 my $last_year = $year_col->max();
232 Wrapper for ->func. Returns the highest value of the column in the
233 resultset (or C<undef> if there are none).
238 return shift->func('MAX');
245 =item Arguments: none
247 =item Return Value: $resultset
251 my $rs = $year_col->max_rs();
253 Wrapper for ->func_rs for function MAX().
257 sub max_rs { return shift->func_rs('MAX') }
263 =item Arguments: none
265 =item Return Value: $sum_of_values
269 my $total = $prices_col->sum();
271 Wrapper for ->func. Returns the sum of all the values in the column of
272 the resultset. Use on varchar-like columns at your own risk.
277 return shift->func('SUM');
284 =item Arguments: none
286 =item Return Value: $resultset
290 my $rs = $year_col->sum_rs();
292 Wrapper for ->func_rs for function SUM().
296 sub sum_rs { return shift->func_rs('SUM') }
302 =item Arguments: $function
304 =item Return Value: $function_return_value
308 $rs = $schema->resultset("CD")->search({});
309 $length = $rs->get_column('title')->func('LENGTH');
311 Runs a query using the function on the column and returns the
312 value. Produces the following SQL:
314 SELECT LENGTH( title ) FROM cd me
319 my ($self,$function) = @_;
320 my $cursor = $self->func_rs($function)->cursor;
323 return map { $_->[ 0 ] } $cursor->all;
326 return ( $cursor->next )[ 0 ];
333 =item Arguments: $function
335 =item Return Value: $resultset
339 Creates the resultset that C<func()> uses to run its query.
344 my ($self,$function) = @_;
345 return $self->{_parent_resultset}->search(
347 select => {$function => $self->{_select}},
348 as => [$self->{_as}],
353 =head2 throw_exception
355 See L<DBIx::Class::Schema/throw_exception> for details.
359 sub throw_exception {
361 if (ref $self && $self->{_parent_resultset}) {
362 $self->{_parent_resultset}->throw_exception(@_)
372 # Return Value: $resultset
374 # $year_col->_resultset->next
376 # Returns the underlying resultset. Creates it from the parent resultset if
382 return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
384 select => [$self->{_select}],
394 Luke Saunders <luke.saunders@gmail.com>
400 You may distribute this code under the same terms as Perl itself.