1 package DBIx::Class::PK;
7 use base qw/DBIx::Class::Row/;
9 __PACKAGE__->mk_classdata('_primaries' => {});
13 DBIx::Class::PK - Primary Key class
19 This class contains methods for handling primary keys and methods
28 return join(" AND ", map { "$_ = ?" } keys %{$class->_primaries});
33 return (map { $self->{_column_data}{$_} } keys %{$self->_primaries});
36 =head2 set_primary_key(@cols)
38 Defines one or more columns as primary key for this class. Should be
39 called after C<columns>.
44 my ($class, @cols) = @_;
45 # check if primary key columns are valid columns
47 $class->throw( "Column $_ can't be used as primary key because it isn't defined in $class" )
48 unless $class->has_column($_);
51 tie %pri, 'Tie::IxHash', map { $_ => {} } @cols;
52 $class->_primaries(\%pri);
55 =head2 find(@colvalues), find(\%cols)
57 Finds a row based on its primary key(s).
62 my ($class, @vals) = @_;
63 my $attrs = (@vals > 1 && ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
64 my @pk = keys %{$class->_primaries};
65 $class->throw( "Can't find unless primary columns are defined" )
68 if (ref $vals[0] eq 'HASH') {
70 } elsif (@pk == @vals) {
72 @{$query}{@pk} = @vals;
73 #my $ret = ($class->search_literal($class->_ident_cond, @vals, $attrs))[0];
74 #warn "$class: ".join(', ', %{$ret->{_column_data}});
79 $class->throw( "Can't find unless all primary keys are specified" )
80 unless (keys %$query >= @pk); # If we check 'em we run afoul of uc/lc
81 # column names etc. Not sure what to do yet
82 return $class->search($query)->next;
83 #my @cols = $class->_select_columns;
84 #my @row = $class->storage->select_single($class->_table_name, \@cols, $query);
85 #return (@row ? $class->_row_to_object(\@cols, \@row) : ());
88 =head2 discard_changes
90 Re-selects the row from the database, losing any changes that had
97 delete $self->{_dirty_columns};
98 return unless $self->in_storage; # Don't reload if we aren't real!
99 my ($reload) = $self->find($self->id);
100 unless ($reload) { # If we got deleted in the mean-time
101 $self->in_storage(0);
104 delete @{$self}{keys %$self};
105 @{$self}{keys %$reload} = values %$reload;
111 Returns the primary key(s) for a row. Can't be called as
118 $self->throw( "Can't call id() as a class method" ) unless ref $self;
119 my @pk = $self->_ident_values;
120 return (wantarray ? @pk : $pk[0]);
123 =head2 primary_columns
125 Read-only accessor which returns the list of primary keys for a class
126 (in scalar context, only returns the first primary key).
130 sub primary_columns {
131 return keys %{shift->_primaries};
136 Returns a unique id string identifying a row object by primary key.
137 Used by L<DBIx::Class::CDBICompat::LiveObjectIndex> and
138 L<DBIx::Class::ObjectCache>.
144 $self->throw( "Can't call ID() as a class method" ) unless ref $self;
145 return undef unless $self->in_storage;
146 return $self->_create_ID(map { $_ => $self->{_column_data}{$_} } keys %{$self->_primaries});
150 my ($class,%vals) = @_;
151 return undef unless 0 == grep { !defined } values %vals;
152 $class = ref $class || $class;
153 return join '|', $class, map { $_ . '=' . $vals{$_} } sort keys %vals;
156 sub ident_condition {
159 $cond{$_} = $self->get_column($_) for $self->primary_columns;
167 Matt S. Trout <mst@shadowcatsystems.co.uk>
171 You may distribute this code under the same terms as Perl itself.