1 package DBIx::Class::PK;
7 use base qw/Class::Data::Inheritable/;
9 __PACKAGE__->mk_classdata('_primaries' => {});
13 DBIx::Class::PK - Primary Key class
19 This class represents methods handling primary keys
20 and depending on them.
31 return join(" AND ", map { "$_ = ?" } keys %{$class->_primaries});
36 return (map { $self->{_column_data}{$_} } keys %{$self->_primaries});
40 my ($class, @cols) = @_;
42 tie %pri, 'Tie::IxHash';
43 %pri = map { $_ => {} } @cols;
44 $class->_primaries(\%pri);
48 my ($class, @vals) = @_;
49 my $attrs = (@vals > 1 && ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
50 my @pk = keys %{$class->_primaries};
51 $class->throw( "Can't find unless primary columns are defined" )
54 if (ref $vals[0] eq 'HASH') {
56 } elsif (@pk == @vals) {
57 my $ret = ($class->search_literal($class->_ident_cond, @vals, $attrs))[0];
58 #warn "$class: ".join(', ', %{$ret->{_column_data}});
63 $class->throw( "Can't find unless all primary keys are specified" )
64 unless (keys %$query >= @pk); # If we check 'em we run afoul of uc/lc
65 # column names etc. Not sure what to do yet
66 my $ret = ($class->search($query))[0];
67 #warn "$class: ".join(', ', %{$ret->{_column_data}});
73 delete $self->{_dirty_columns};
74 return unless $self->in_storage; # Don't reload if we aren't real!
75 my ($reload) = $self->find($self->id);
76 unless ($reload) { # If we got deleted in the mean-time
80 delete @{$self}{keys %$self};
81 @{$self}{keys %$reload} = values %$reload;
82 #$self->store_column($_ => $reload->get_column($_))
83 # foreach keys %{$self->_columns};
89 $self->throw( "Can't call id() as a class method" ) unless ref $self;
90 my @pk = $self->_ident_values;
91 return (wantarray ? @pk : $pk[0]);
95 return keys %{shift->_primaries};
104 Matt S. Trout <perl-stuff@trout.me.uk>
108 You may distribute this code under the same terms as Perl itself.