X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FTable.pm;h=d824b0adb4c173d9f80101008563c1adffc8fd41;hb=d7156e507aaffa832df977118f015e0833bc87ff;hp=01771ee707e993766a4611bfca7ebc90b028e6f6;hpb=fa58d4bbe57c3324316e08b5c8e5c6be6899c75e;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Table.pm b/lib/DBIx/Class/Table.pm index 01771ee..d824b0a 100644 --- a/lib/DBIx/Class/Table.pm +++ b/lib/DBIx/Class/Table.pm @@ -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 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. + +=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