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