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