Added remove_column(s) from ResultSource/ResultSourceProxy
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSource.pm
index 700d01e..53da27b 100644 (file)
@@ -17,7 +17,7 @@ __PACKAGE__->mk_group_accessors('simple' => qw/_ordered_columns
 __PACKAGE__->mk_group_accessors('component_class' => qw/resultset_class
   result_class/);
 
-=head1 NAME 
+=head1 NAME
 
 DBIx::Class::ResultSource - Result source object
 
@@ -63,7 +63,7 @@ keys are currently recognised/used by DBIx::Class:
 
 =over 4
 
-=item accessor 
+=item accessor
 
 Use this to set the name of the accessor for this column. If unset,
 the name of the column will be used.
@@ -83,7 +83,7 @@ whatever your database supports.
 =item size
 
 The length of your column, if it is a column type that can have a size
-restriction. This is currently not used by DBIx::Class. 
+restriction. This is currently not used by DBIx::Class.
 
 =item is_nullable
 
@@ -105,7 +105,7 @@ foreign table. This is currently not used by DBIx::Class.
 
 Set this to the default value which will be inserted into a column
 by the database. Can contain either a value or a function. This is
-currently not used by DBIx::Class. 
+currently not used by DBIx::Class.
 
 =item sequence
 
@@ -127,7 +127,7 @@ Convenience alias to add_columns.
 sub add_columns {
   my ($self, @cols) = @_;
   $self->_ordered_columns(\@cols) unless $self->_ordered_columns;
-  
+
   my @added;
   my $columns = $self->_columns;
   while (my $col = shift @cols) {
@@ -167,16 +167,16 @@ of add_column for information on the contents of the hashref.
 
 sub column_info {
   my ($self, $column) = @_;
-  $self->throw_exception("No such column $column") 
+  $self->throw_exception("No such column $column")
     unless exists $self->_columns->{$column};
   #warn $self->{_columns_info_loaded}, "\n";
-  if ( ! $self->_columns->{$column}{data_type} 
-       and ! $self->{_columns_info_loaded} 
+  if ( ! $self->_columns->{$column}{data_type}
+       and ! $self->{_columns_info_loaded}
        and $self->schema and $self->storage )
   {
     $self->{_columns_info_loaded}++;
     my $info;
-    # eval for the case of storage without table 
+    # eval for the case of storage without table
     eval { $info = $self->storage->columns_info_for($self->from) };
     unless ($@) {
       foreach my $col ( keys %{$self->_columns} ) {
@@ -205,6 +205,41 @@ sub columns {
   return @{$self->{_ordered_columns}||[]};
 }
 
+=head2 remove_columns
+
+  $table->remove_columns(qw/col1 col2 col3/);
+
+Removes columns from the result source.
+
+=head2 remove_column
+
+  $table->remove_column('col');
+
+Convenience alias to remove_columns.
+
+=cut
+
+sub remove_columns {
+  my ($self, @cols) = @_;
+
+  return unless $self->_ordered_columns;
+
+  my $columns = $self->_columns;
+  my @remaining;
+
+  foreach my $col (@{$self->_ordered_columns}) {
+    push @remaining, $col unless grep(/$col/, @cols);
+  }
+
+  foreach (@cols) {
+    undef $columns->{$_};
+  };
+
+  $self->_ordered_columns(\@remaining);
+}
+
+*remove_column = \&remove_columns;
+
 =head2 set_primary_key
 
 =over 4
@@ -219,7 +254,7 @@ called after C<add_columns>.
 Additionally, defines a unique constraint named C<primary>.
 
 The primary key columns are used by L<DBIx::Class::PK::Auto> to
-retrieve automatically created values from the database. 
+retrieve automatically created values from the database.
 
 =cut
 
@@ -292,7 +327,7 @@ clause contents.
 
 =head2 storage
 
-Returns the storage handle for the current schema. 
+Returns the storage handle for the current schema.
 
 See also: L<DBIx::Class::Storage>
 
@@ -339,11 +374,11 @@ the SQL command immediately before C<JOIN>.
 
 An arrayref containing a list of accessors in the foreign class to proxy in
 the main class. If, for example, you do the following:
-  
+
   CD->might_have(liner_notes => 'LinerNotes', undef, {
     proxy => [ qw/notes/ ],
   });
-  
+
 Then, assuming LinerNotes has an accessor named notes, you can do:
 
   my $cd = CD->find(1);
@@ -353,11 +388,11 @@ Then, assuming LinerNotes has an accessor named notes, you can do:
 =item accessor
 
 Specifies the type of accessor that should be created for the
-relationship. Valid values are C<single> (for when there is only a single 
-related object), C<multi> (when there can be many), and C<filter> (for 
-when there is a single related object, but you also want the relationship 
-accessor to double as a column accessor). For C<multi> accessors, an 
-add_to_* method is also created, which calls C<create_related> for the 
+relationship. Valid values are C<single> (for when there is only a single
+related object), C<multi> (when there can be many), and C<filter> (for
+when there is a single related object, but you also want the relationship
+accessor to double as a column accessor). For C<multi> accessors, an
+add_to_* method is also created, which calls C<create_related> for the
 relationship.
 
 =back
@@ -398,7 +433,7 @@ sub add_relationship {
   eval { $self->resolve_join($rel, 'me') };
 
   if ($@) { # If the resolve failed, back out and re-throw the error
-    delete $rels{$rel}; # 
+    delete $rels{$rel}; #
     $self->_relationships(\%rels);
     $self->throw_exception("Error creating relationship $rel: $@");
   }
@@ -431,7 +466,7 @@ name.
 sub relationship_info {
   my ($self, $rel) = @_;
   return $self->_relationships->{$rel};
-} 
+}
 
 =head2 has_relationship
 
@@ -511,9 +546,9 @@ sub resolve_condition {
     while (my ($k, $v) = each %{$cond}) {
       # XXX should probably check these are valid columns
       $k =~ s/^foreign\.// ||
-       $self->throw_exception("Invalid rel cond key ${k}");
+        $self->throw_exception("Invalid rel cond key ${k}");
       $v =~ s/^self\.// ||
-       $self->throw_exception("Invalid rel cond val ${v}");
+        $self->throw_exception("Invalid rel cond val ${v}");
       if (ref $for) { # Object
         #warn "$self $k $for $v";
         $ret{$k} = $for->get_column($v);
@@ -577,7 +612,7 @@ in the supplied relationships. Examples:
   #  'artist.name',
   #  'producer.producerid',
   #  'producer.name'
-  #)  
+  #)
 
 =cut
 
@@ -719,7 +754,7 @@ See L<DBIx::Class::Schema/"throw_exception">.
 
 sub throw_exception {
   my $self = shift;
-  if (defined $self->schema) { 
+  if (defined $self->schema) {
     $self->schema->throw_exception(@_);
   } else {
     croak(@_);