Throw if "foreign." and "self." are wrong way round in conditions for relationships
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSetColumn.pm
index 35f8fa4..f93cbdd 100644 (file)
@@ -16,7 +16,8 @@ use base 'DBIx::Class';
 
 =head1 DESCRIPTION
 
-A convenience class used to perform operations on a specific column of a resultset.
+A convenience class used to perform operations on a specific column of
+a resultset.
 
 =cut
 
@@ -26,19 +27,18 @@ A convenience class used to perform operations on a specific column of a results
 
   my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
 
-Creates a new resultset column object from the resultset and column passed as params
+Creates a new resultset column object from the resultset and column
+passed as params. Used internally by L<DBIx::Class::ResultSet/get_column>.
 
 =cut
 
 sub new {
   my ($class, $rs, $column) = @_;
   $class = ref $class if ref $class;
-
-  my $object_ref = { _column => $column,
-                    _parent_resultset => $rs };
-  
-  my $new = bless $object_ref, $class;
-  $new->throw_exception("column must be supplied") unless ($column);
+  my $new_parent_rs = $rs->search_rs; # we don't want to mess up the original, so clone it
+  $new_parent_rs->{attrs}->{prefetch} = undef; # prefetch causes additional columns to be fetched
+  my $new = bless { _column => $column, _parent_resultset => $new_parent_rs }, $class;
+  $new->throw_exception("column must be supplied") unless $column;
   return $new;
 }
 
@@ -52,15 +52,16 @@ sub new {
 
 =back
 
-Returns the next value of the column in the resultset (C<undef> is there is none).
+Returns the next value of the column in the resultset (or C<undef> if
+there is none).
 
-Much like $rs->next but just returning the one value
+Much like L<DBIx::Class::ResultSet/next> but just returning the 
+one value.
 
 =cut
 
 sub next {
   my $self = shift;
-    
   $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]}) unless ($self->{_resultset});
   my ($row) = $self->{_resultset}->cursor->next;
   return $row;
@@ -76,9 +77,11 @@ sub next {
 
 =back
 
-Returns all values of the column in the resultset (C<undef> is there are none).
+Returns all values of the column in the resultset (or C<undef> if
+there are none).
 
-Much like $rs->all but returns values rather than row objects
+Much like L<DBIx::Class::ResultSet/all> but returns values rather
+than row objects.
 
 =cut
 
@@ -97,13 +100,15 @@ sub all {
 
 =back
 
-Wrapper for ->func. Returns the lowest value of the column in the resultset (C<undef> is there are none).
+  my $first_year = $year_col->min();
+
+Wrapper for ->func. Returns the lowest value of the column in the
+resultset (or C<undef> if there are none).
 
 =cut
 
 sub min {
-  my $self = shift;
-  return $self->func('MIN');
+  return shift->func('MIN');
 }
 
 =head2 max
@@ -116,13 +121,15 @@ sub min {
 
 =back
 
-Wrapper for ->func. Returns the highest value of the column in the resultset (C<undef> is there are none).
+  my $last_year = $year_col->max();
+
+Wrapper for ->func. Returns the highest value of the column in the
+resultset (or C<undef> if there are none).
 
 =cut
 
 sub max {
-  my $self = shift;
-  return $self->func('MAX');
+  return shift->func('MAX');
 }
 
 =head2 sum
@@ -135,13 +142,15 @@ sub max {
 
 =back
 
-Wrapper for ->func. Returns the sum of all the values in the column of the resultset. Use on varchar-like columns at your own risk.
+  my $total = $prices_col->sum();
+
+Wrapper for ->func. Returns the sum of all the values in the column of
+the resultset. Use on varchar-like columns at your own risk.
 
 =cut
 
 sub sum {
-  my $self = shift;
-  return $self->func('SUM');
+  return shift->func('SUM');
 }
 
 =head2 func
@@ -154,19 +163,18 @@ sub sum {
 
 =back
 
-Runs a query using the function on the column and returns the value. For example 
   $rs = $schema->resultset("CD")->search({});
   $length = $rs->get_column('title')->func('LENGTH');
 
-Produces the following SQL
-  SELECT LENGTH( title ) from cd me
+Runs a query using the function on the column and returns the
+value. Produces the following SQL:
+
+  SELECT LENGTH( title ) FROM cd me
 
 =cut
 
 sub func {
-  my $self = shift;
-  my $function = shift;
-
+  my ($self,$function) = @_;
   my ($row) = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_column}}, as => [$self->{_column}]})->cursor->next;
   return $row;
 }
@@ -177,6 +185,8 @@ sub func {
 
 Luke Saunders <luke.saunders@gmail.com>
 
+Jess Robinson
+
 =head1 LICENSE
 
 You may distribute this code under the same terms as Perl itself.