Expand/fortify the handling of attributes
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / PK.pm
CommitLineData
dbd7896f 1package DBIx::Class::PK;
2
3use strict;
4use warnings;
5
1edd1722 6use base qw/DBIx::Class::Row/;
dbd7896f 7
75d07914 8=head1 NAME
34d52be2 9
10DBIx::Class::PK - Primary Key class
11
12=head1 SYNOPSIS
13
14=head1 DESCRIPTION
15
75d07914 16This class contains methods for handling primary keys and methods
8091aa91 17depending on them.
34d52be2 18
19=head1 METHODS
20
34d52be2 21=cut
22
8091aa91 23=head2 id
076652e8 24
8091aa91 25Returns the primary key(s) for a row. Can't be called as
076652e8 26a class method.
27
28=cut
29
604d9f38 30sub id {
31 my ($self) = @_;
bc0c9800 32 $self->throw_exception( "Can't call id() as a class method" )
33 unless ref $self;
b946a283 34 my @id_vals = $self->_ident_values;
35 return (wantarray ? @id_vals : $id_vals[0]);
604d9f38 36}
37
bbd107cf 38sub _ident_values {
867f1b28 39 my ($self, $use_storage_state) = @_;
90f250bc 40
b946a283 41 my (@ids, @missing);
42
56ad42bb 43 for ($self->result_source->_pri_cols_or_die) {
867f1b28 44 push @ids, ($use_storage_state and exists $self->{_column_data_in_storage}{$_})
5ef76b8b 45 ? $self->{_column_data_in_storage}{$_}
46 : $self->get_column($_)
47 ;
b946a283 48 push @missing, $_ if (! defined $ids[-1] and ! $self->has_column_loaded ($_) );
49 }
50
51 if (@missing && $self->in_storage) {
52 $self->throw_exception (
fb13a49f 53 'Unable to uniquely identify result object with missing PK columns: '
b946a283 54 . join (', ', @missing )
55 );
56 }
57
58 return @ids;
bbd107cf 59}
60
8091aa91 61=head2 ID
62
fb13a49f 63Returns a unique id string identifying a result object by primary key.
75d07914 64Used by L<DBIx::Class::CDBICompat::LiveObjectIndex> and
8091aa91 65L<DBIx::Class::ObjectCache>.
66
bbd107cf 67=over
68
69=item WARNING
70
71The default C<_create_ID> method used by this function orders the returned
72values by the alphabetical order of the primary column names, B<unlike>
73the L</id> method, which follows the same order in which columns were fed
74to L<DBIx::Class::ResultSource/set_primary_key>.
75
76=back
77
8091aa91 78=cut
79
48700d09 80sub ID {
81 my ($self) = @_;
bc0c9800 82 $self->throw_exception( "Can't call ID() as a class method" )
83 unless ref $self;
48700d09 84 return undef unless $self->in_storage;
b946a283 85 return $self->_create_ID(%{$self->ident_condition});
48700d09 86}
87
88sub _create_ID {
b946a283 89 my ($self, %vals) = @_;
2053211a 90 return undef if grep { !defined } values %vals;
bc0c9800 91 return join '|', ref $self || $self, $self->result_source->name,
75d07914 92 map { $_ . '=' . $vals{$_} } sort keys %vals;
48700d09 93}
94
9b83fccd 95=head2 ident_condition
96
97 my $cond = $result_source->ident_condition();
98
99 my $cond = $result_source->ident_condition('alias');
100
101Produces a condition hash to locate a row based on the primary key(s).
102
103=cut
104
103647d5 105sub ident_condition {
867f1b28 106 shift->_mk_ident_cond(@_);
107}
108
109sub _storage_ident_condition {
110 shift->_mk_ident_cond(shift, 1);
111}
112
113sub _mk_ident_cond {
114 my ($self, $alias, $use_storage_state) = @_;
b946a283 115
56ad42bb 116 my @pks = $self->result_source->_pri_cols_or_die;
867f1b28 117 my @vals = $self->_ident_values($use_storage_state);
b946a283 118
119 my (%cond, @undef);
e04df8ec 120 my $prefix = defined $alias ? $alias.'.' : '';
b946a283 121 for my $col (@pks) {
122 if (! defined ($cond{$prefix.$col} = shift @vals) ) {
123 push @undef, $col;
124 }
125 }
126
127 if (@undef && $self->in_storage) {
128 $self->throw_exception (
fb13a49f 129 'Unable to construct result object identity condition due to NULL PK columns: '
b946a283 130 . join (', ', @undef)
131 );
132 }
133
103647d5 134 return \%cond;
135}
136
a2bd3796 137=head1 FURTHER QUESTIONS?
34d52be2 138
a2bd3796 139Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
34d52be2 140
a2bd3796 141=head1 COPYRIGHT AND LICENSE
34d52be2 142
a2bd3796 143This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
144by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
145redistribute it and/or modify it under the same terms as the
146L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
34d52be2 147
148=cut
149
a2bd3796 1501;