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 represents methods handling primary keys
20 and depending on them.
28 return join(" AND ", map { "$_ = ?" } keys %{$class->_primaries});
33 return (map { $self->{_column_data}{$_} } keys %{$self->_primaries});
36 =head2 set_primary_key <@cols>
38 define one or more columns as primary key for this class
43 my ($class, @cols) = @_;
44 # check if primary key columns are valid columns
46 $class->throw( "Column $_ can't be used as primary key because it isn't defined in $class" )
47 unless $class->has_column($_);
50 tie %pri, 'Tie::IxHash', map { $_ => {} } @cols;
51 $class->_primaries(\%pri);
56 Finds columns based on the primary key(s).
61 my ($class, @vals) = @_;
62 my $attrs = (@vals > 1 && ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
63 my @pk = keys %{$class->_primaries};
64 $class->throw( "Can't find unless primary columns are defined" )
67 if (ref $vals[0] eq 'HASH') {
69 } elsif (@pk == @vals) {
71 @{$query}{@pk} = @vals;
72 #my $ret = ($class->search_literal($class->_ident_cond, @vals, $attrs))[0];
73 #warn "$class: ".join(', ', %{$ret->{_column_data}});
78 $class->throw( "Can't find unless all primary keys are specified" )
79 unless (keys %$query >= @pk); # If we check 'em we run afoul of uc/lc
80 # column names etc. Not sure what to do yet
81 #return $class->search($query)->next;
82 my @cols = $class->_select_columns;
83 my @row = $class->storage->select_single($class->_table_name, \@cols, $query);
84 return (@row ? $class->_row_to_object(\@cols, \@row) : ());
87 =head2 discard_changes
89 Roll back changes that hasn't been comitted to the database.
95 delete $self->{_dirty_columns};
96 return unless $self->in_storage; # Don't reload if we aren't real!
97 my ($reload) = $self->find($self->id);
98 unless ($reload) { # If we got deleted in the mean-time
102 delete @{$self}{keys %$self};
103 @{$self}{keys %$reload} = values %$reload;
109 returns the primary key(s) for the current row. Can't be called as
116 $self->throw( "Can't call id() as a class method" ) unless ref $self;
117 my @pk = $self->_ident_values;
118 return (wantarray ? @pk : $pk[0]);
121 =head2 primary_columns
123 read-only accessor which returns a list of primary keys.
127 sub primary_columns {
128 return keys %{shift->_primaries};
133 $self->throw( "Can't call ID() as a class method" ) unless ref $self;
134 return undef unless $self->in_storage;
135 return $self->_create_ID(map { $_ => $self->{_column_data}{$_} } keys %{$self->_primaries});
139 my ($class,%vals) = @_;
140 return undef unless 0 == grep { !defined } values %vals;
141 $class = ref $class || $class;
142 return join '|', $class, map { $_ . '=' . $vals{$_} } sort keys %vals;
145 sub ident_condition {
148 $cond{$_} = $self->get_column($_) for $self->primary_columns;
156 Matt S. Trout <mst@shadowcatsystems.co.uk>
160 You may distribute this code under the same terms as Perl itself.