File Coverage

blib/lib/DBIx/Class/PK.pm
Criterion Covered Total %
statement 49 52 94.2
branch 14 22 63.6
condition 8 15 53.3
subroutine 13 13 100.0
pod 3 3 100.0
total 87 105 82.8


line stmt bran cond sub pod time code
1             package DBIx::Class::PK;
2              
3 312     312   96386 use strict;
  312         792  
  312         8837  
4 312     312   1677 use warnings;
  312         663  
  312         9057  
5              
6 312     312   1693 use base qw/DBIx::Class::Row/;
  312         695  
  312         29728  
7              
8 312     312   2148 use DBIx::Class::_Util 'fail_on_internal_call';
  312         702  
  312         14042  
9 312     312   1906 use namespace::clean;
  312         713  
  312         1914  
10              
11             =head1 NAME
12              
13             DBIx::Class::PK - Primary Key class
14              
15             =head1 SYNOPSIS
16              
17             =head1 DESCRIPTION
18              
19             This class contains methods for handling primary keys and methods
20             depending on them.
21              
22             =head1 METHODS
23              
24             =cut
25              
26             =head2 id
27              
28             Returns the primary key(s) for a row. Can't be called as
29             a class method.
30              
31             =cut
32              
33             sub id :DBIC_method_is_indirect_sugar {
34 344     344 1 6406 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call;
35              
36 344 50       1270 $_[0]->throw_exception( "Can't call id() as a class method" )
37             unless ref $_[0];
38              
39             wantarray
40 344 100       2023 ? $_[0]->_ident_values
41             : ($_[0]->_ident_values)[0] # FIXME - horrible horrible legacy crap
42             ;
43 312     312   86291 }
  312         767  
  312         2000  
44              
45             sub _ident_values {
46 1723     1723   5231 my ($self, $use_storage_state) = @_;
47              
48 1723         4088 my (@ids, @missing);
49              
50 1723         6405 for ($self->result_source->_pri_cols_or_die) {
51             push @ids, ($use_storage_state and exists $self->{_column_data_in_storage}{$_})
52 1795 100 100     13940 ? $self->{_column_data_in_storage}{$_}
53             : $self->get_column($_)
54             ;
55 1795 50 66     7068 push @missing, $_ if (! defined $ids[-1] and ! $self->has_column_loaded ($_) );
56             }
57              
58 1723 50 33     6398 if (@missing && $self->in_storage) {
59 0         0 $self->throw_exception (
60             'Unable to uniquely identify result object with missing PK columns: '
61             . join (', ', @missing )
62             );
63             }
64              
65 1723         6212 return @ids;
66             }
67              
68             =head2 ID
69              
70             Returns a unique id string identifying a result object by primary key.
71             Used by L and
72             L.
73              
74             =over
75              
76             =item WARNING
77              
78             The default C<_create_ID> method used by this function orders the returned
79             values by the alphabetical order of the primary column names, B
80             the L method, which follows the same order in which columns were fed
81             to L.
82              
83             =back
84              
85             =cut
86              
87             sub ID {
88 54     54 1 183 my ($self) = @_;
89 54 50       245 $self->throw_exception( "Can't call ID() as a class method" )
90             unless ref $self;
91 54 50       284 return undef unless $self->in_storage;
92 54         134 return $self->_create_ID(%{$self->ident_condition});
  54         318  
93             }
94              
95             sub _create_ID {
96 54     54   202 my ($self, %vals) = @_;
97 54 50       171 return undef if grep { !defined } values %vals;
  76         339  
98             return join '|', ref $self || $self, $self->result_source->name,
99 54   33     300 map { $_ . '=' . $vals{$_} } sort keys %vals;
  76         971  
100             }
101              
102             =head2 ident_condition
103              
104             my $cond = $result_source->ident_condition();
105              
106             my $cond = $result_source->ident_condition('alias');
107              
108             Produces a condition hash to locate a row based on the primary key(s).
109              
110             =cut
111              
112             sub ident_condition {
113 150     150 1 757 shift->_mk_ident_cond(@_);
114             }
115              
116             sub _storage_ident_condition {
117 1231     1231   18113 shift->_mk_ident_cond(shift, 1);
118             }
119              
120             sub _mk_ident_cond {
121 1381     1381   3822 my ($self, $alias, $use_storage_state) = @_;
122              
123 1381         5090 my @pks = $self->result_source->_pri_cols_or_die;
124 1379         6787 my @vals = $self->_ident_values($use_storage_state);
125              
126 1379         3154 my (%cond, @undef);
127 1379 100       4159 my $prefix = defined $alias ? $alias.'.' : '';
128 1379         3242 for my $col (@pks) {
129 1451 50       6953 if (! defined ($cond{$prefix.$col} = shift @vals) ) {
130 0         0 push @undef, $col;
131             }
132             }
133              
134 1379 50 33     4867 if (@undef && $self->in_storage) {
135 0         0 $self->throw_exception (
136             'Unable to construct result object identity condition due to NULL PK columns: '
137             . join (', ', @undef)
138             );
139             }
140              
141 1379         23059 return \%cond;
142             }
143              
144             =head1 FURTHER QUESTIONS?
145              
146             Check the list of L.
147              
148             =head1 COPYRIGHT AND LICENSE
149              
150             This module is free software L
151             by the L. You can
152             redistribute it and/or modify it under the same terms as the
153             L.
154              
155             =cut
156              
157             1;