Simplified column handling code, moved primary key defs to Table.pm
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Table.pm
index 01771ee..d824b0a 100644 (file)
@@ -11,7 +11,7 @@ use base qw/DBIx::Class/;
 __PACKAGE__->load_components(qw/AccessorGroup/);
 
 __PACKAGE__->mk_group_accessors('simple' =>
-  qw/_columns name resultset_class result_class storage/);
+  qw/_columns _primaries name resultset_class result_class storage/);
 
 =head1 NAME 
 
@@ -31,7 +31,7 @@ L<DBIx::Class> classes.
 sub new {
   my ($class, $attrs) = @_;
   $class = ref $class if ref $class;
-  my $new = bless($attrs || {}, $class);
+  my $new = bless({ %{$attrs || {}} }, $class);
   $new->{resultset_class} ||= 'DBIx::Class::ResultSet';
   $new->{_columns} ||= {};
   $new->{name} ||= "!!NAME NOT SET!!";
@@ -41,14 +41,11 @@ sub new {
 sub add_columns {
   my ($self, @cols) = @_;
   while (my $col = shift @cols) {
-    $self->add_column($col => (ref $cols[0] ? shift : {}));
+    $self->_columns->{$col} = (ref $cols[0] ? shift : {});
   }
 }
 
-sub add_column {
-  my ($self, $col, $info) = @_;
-  $self->_columns->{$col} = $info || {};
-}
+*add_column = \&add_columns;
 
 =head2 add_columns
 
@@ -59,6 +56,12 @@ sub add_column {
 Adds columns to the table object. If supplied key => hashref pairs uses
 the hashref as the column_info for that column.
 
+=head2 add_column
+
+  $table->add_column('col' => \%info?);
+
+Convenience alias to add_columns
+
 =cut
 
 sub resultset {
@@ -95,8 +98,8 @@ sub column_info {
   return $self->_columns->{$column};
 }
 
-=head2 columns                                                                   
-                                                                                
+=head2 columns
+
   my @column_names = $obj->columns;                                             
                                                                                 
 =cut                                                                            
@@ -106,6 +109,34 @@ sub columns {
   return keys %{shift->_columns};
 }
 
+=head2 set_primary_key(@cols)                                                   
+                                                                                
+Defines one or more columns as primary key for this table. Should be            
+called after C<add_columns>.
+                                                                                
+=cut                                                                            
+
+sub set_primary_key {
+  my ($self, @cols) = @_;
+  # check if primary key columns are valid columns
+  for (@cols) {
+    $self->throw("No such column $_ on table ".$self->name)
+      unless $self->has_column($_);
+  }
+  $self->_primaries(\@cols);
+}
+
+=head2 primary_columns                                                          
+                                                                                
+Read-only accessor which returns the list of primary keys.
+                                                                                
+=cut                                                                            
+
+sub primary_columns {
+  return @{shift->_primaries||[]};
+}
+
+
 1;
 
 =head1 AUTHORS