| 1 | package DBIO::Extension::List; | ||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: The declared list of database extensions of a schema, resolved to classes | ||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 6 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use DBIO::Exception; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use DBIO::Util (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use DBIO::Extension; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||
| 15 | 44 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=25 SCAR=32.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 16 | 44 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||
| 17 | 44 | ▶ | my $declaration = $args{declaration} || []; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'declaration'} || []
MC/DC0%0/1
$args{'declaration'} || []
$args{'declaration'}
Truth table50%1/2
A
A= $args{'declaration'}
| |||||||||||||||||||||||||||||||||||||
| 18 | 44 | my $table = $args{class_table}; | |||||||||||||||||||||||||||||||||||
| 19 | 44 | my $plain = $args{plain_class}; | |||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||
| 21 | 44 | ▶ | DBIO::Exception->throw('DBIO::Extension::List: declaration must be an array reference') | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 22 | unless ref $declaration eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||
| 24 | 44 | my %entry; | |||||||||||||||||||||||||||||||||||
| 25 | 44 | my @decl = @$declaration; | |||||||||||||||||||||||||||||||||||
| 26 | 44 | ▶ | while (@decl) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 27 | 61 | my $name = shift @decl; | |||||||||||||||||||||||||||||||||||
| 28 | 61 | ▶ | DBIO::Exception->throw( | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
not defined $name or ref $name
MC/DC50%1/2
not defined $name or ref $name
not defined $name
ref $name
Truth table66%2/3
not(A) or B
A= not defined $nameB= ref $name
| |||||||||||||||||||||||||||||||||||||
| 29 | 'Extension declaration: expected an extension name, got ' | ||||||||||||||||||||||||||||||||||||
| 30 | . (ref $name ? 'a ' . ref($name) . ' reference' : defined $name ? "'$name'" : 'undef') | ||||||||||||||||||||||||||||||||||||
| 31 | . ' (options follow the name they belong to)' | ||||||||||||||||||||||||||||||||||||
| 32 | ) if !defined $name || ref $name; | ||||||||||||||||||||||||||||||||||||
| 33 | 60 | DBIO::Util::assert_identifier_text('Extension declaration: an extension name', $name); | |||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||
| 35 | 56 | my %options; | |||||||||||||||||||||||||||||||||||
| 36 | 56 | ▶ | if (@decl && ref $decl[0]) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@decl and ref $decl[0]
MC/DC100%2/2
@decl and ref $decl[0]
@decl
ref $decl[0]
Truth table100%3/3
A and B
A= @declB= ref $decl[0]
| |||||||||||||||||||||||||||||||||||||
| 37 | 29 | my $options = shift @decl; | |||||||||||||||||||||||||||||||||||
| 38 | 29 | ▶ | DBIO::Exception->throw("Extension '$name': the options must be a hash reference") | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 39 | unless ref $options eq 'HASH'; | ||||||||||||||||||||||||||||||||||||
| 40 | 29 | %options = %$options; | |||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||
| 43 | DBIO::Util::assert_identifier_text("Extension '$name': option 'schema'", $options{schema}) | ||||||||||||||||||||||||||||||||||||
| 44 | 56 | ▶ | if exists $options{schema}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||
| 46 | 52 | my $explicit = delete $options{class}; | |||||||||||||||||||||||||||||||||||
| 47 | 52 | ▶ | my ($resolved, $distribution) = defined $explicit ? ($explicit) | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 48 | : $table ? $table->($name) : (); | ||||||||||||||||||||||||||||||||||||
| 49 | 52 | ▶ | $resolved //= $plain; | ||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$resolved //= $plain
MC/DC100%2/2
$resolved //= $plain
$resolved
$plain
Truth table100%3/3
A //= B
A= $resolvedB= $plain
| |||||||||||||||||||||||||||||||||||||
| 50 | 52 | ▶ | DBIO::Exception->throw( | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 51 | "Extension '$name' is not known to the driver and no class is named for it " | ||||||||||||||||||||||||||||||||||||
| 52 | . "(declare it as $name => { class => 'My::Extension::Class' })" | ||||||||||||||||||||||||||||||||||||
| 53 | ) unless defined $resolved; | ||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||
| 55 | 51 | ▶ | if (my $have = $entry{$name}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 56 | DBIO::Exception->throw( | ||||||||||||||||||||||||||||||||||||
| 57 | "Extension '$name' is declared with two classes: $have->{class} and $resolved" | ||||||||||||||||||||||||||||||||||||
| 58 | 11 | ▶ | ) if $have->{class} ne $resolved; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 59 | 10 | for my $key (sort keys %options) { | |||||||||||||||||||||||||||||||||||
| 60 | 9 | ▶ | next unless exists $have->{options}{$key}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 61 | DBIO::Exception->throw( | ||||||||||||||||||||||||||||||||||||
| 62 | "Extension '$name' is declared with two values for option '$key'" | ||||||||||||||||||||||||||||||||||||
| 63 | 8 | ▶ | ) if DBIO::Util::canonical_text($have->{options}{$key}) ne DBIO::Util::canonical_text($options{$key}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||
| 65 | 6 | %{ $have->{options} } = (%{ $have->{options} }, %options); | |||||||||||||||||||||||||||||||||||
| 66 | 6 | next; | |||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||
| 69 | 40 | ▶ | $entry{$name} = { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 70 | name => $name, | ||||||||||||||||||||||||||||||||||||
| 71 | class => $resolved, | ||||||||||||||||||||||||||||||||||||
| 72 | options => \%options, | ||||||||||||||||||||||||||||||||||||
| 73 | (defined $distribution ? (_distribution => $distribution) : ()), | ||||||||||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | 29 | for my $name (sort keys %entry) { | |||||||||||||||||||||||||||||||||||
| 78 | 35 | my $entry = $entry{$name}; | |||||||||||||||||||||||||||||||||||
| 79 | 35 | _load_class($name, $entry->{class}, delete $entry->{_distribution}); | |||||||||||||||||||||||||||||||||||
| 80 | 33 | DBIO::Extension->check_interface($entry->{class}); | |||||||||||||||||||||||||||||||||||
| 81 | 32 | my $states = $entry->{class}->name; | |||||||||||||||||||||||||||||||||||
| 82 | 32 | ▶ | DBIO::Exception->throw( | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $states and $states ne $name
MC/DC100%2/2
defined $states and $states ne $name
defined $states
$states ne $name
Truth table100%3/3
A and B
A= defined $statesB= $states ne $name
| |||||||||||||||||||||||||||||||||||||
| 83 | "Extension '$name' is declared with class $entry->{class}, which is the extension '$states'" | ||||||||||||||||||||||||||||||||||||
| 84 | ) if defined $states && $states ne $name; | ||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||
| 87 | 25 | return bless { entries => \%entry }, $class; | |||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||
| 90 | 35 | ▶ | sub _load_class { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_load_class: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||
| 91 | 35 | my ($name, $class, $distribution) = @_; | |||||||||||||||||||||||||||||||||||
| 92 | 35 | DBIO::Util::assert_class_name($class); | |||||||||||||||||||||||||||||||||||
| 93 | 35 | ▶ | return if $class->can('extension_interface'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||
| 95 | 9 | (my $file = "$class.pm") =~ s{::}{/}g; | |||||||||||||||||||||||||||||||||||
| 96 | 9 | ▶ | return if $INC{$file}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 97 | 9 | ▶ | return if eval { require $file; 1 }; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||
| 99 | 2 | my $error = $@; | |||||||||||||||||||||||||||||||||||
| 100 | 2 | ▶ | if ($error =~ /^Can't locate \Q$file\E in \@INC/) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 101 | 2 | ▶ | DBIO::Exception->throw( | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 102 | "Extension '$name': class $class is not installed" | ||||||||||||||||||||||||||||||||||||
| 103 | . (defined $distribution ? " -- install the distribution $distribution" : '') | ||||||||||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||
| 106 | 0 | DBIO::Exception->throw("Extension '$name': class $class does not load: $error"); | |||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | 18 | ▶ | sub entries { | ||||||||||||||||||||||||||||||||||
|
Subroutine
entries: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 111 | 18 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||
| 112 | 26 | return map { { %$_, options => { %{ $_->{options} } } } } | |||||||||||||||||||||||||||||||||||
| 113 | 18 | map { $self->{entries}{$_} } sort keys %{ $self->{entries} }; | |||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub names { sort keys %{ $_[0]{entries} } } | ||||||||||||||||||||||||||||||||||
|
Subroutine
names: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||
| 120 | 2 | ▶ | sub set_installed { | ||||||||||||||||||||||||||||||||||
|
Subroutine
set_installed: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 121 | 2 | my ($self, $state) = @_; | |||||||||||||||||||||||||||||||||||
| 122 | 2 | for my $name (sort keys %$state) { | |||||||||||||||||||||||||||||||||||
| 123 | 3 | ▶ | my $entry = $self->{entries}{$name} | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 124 | or DBIO::Exception->throw("Extension '$name' is not in the declared list; its installed state is not attached"); | ||||||||||||||||||||||||||||||||||||
| 125 | 2 | for my $key (qw(installed version schema)) { | |||||||||||||||||||||||||||||||||||
| 126 | 6 | ▶ | $entry->{$key} = $state->{$name}{$key} if exists $state->{$name}{$key}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||
| 129 | 1 | return $self; | |||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | 3 | ▶ | sub check_driver { | ||||||||||||||||||||||||||||||||||
|
Subroutine
check_driver: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 134 | 3 | my ($self, $resolved) = @_; | |||||||||||||||||||||||||||||||||||
| 135 | 3 | for my $name (sort keys %{ $self->{entries} }) { | |||||||||||||||||||||||||||||||||||
| 136 | 5 | my $class = $self->{entries}{$name}{class}; | |||||||||||||||||||||||||||||||||||
| 137 | 5 | my $asserted = $class->driver; | |||||||||||||||||||||||||||||||||||
| 138 | 5 | ▶ | next unless defined $asserted; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 139 | 4 | ▶ | next if $asserted eq $resolved; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | next if $resolved->can('isa') && $resolved->isa($asserted); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$resolved->can("isa") and $resolved->isa($asserted)
MC/DC0%0/2
$resolved->can("isa") and $resolved->isa($asserted)
$resolved->can("isa")
$resolved->isa($asserted)
Truth table33%1/3
A and B
A= $resolved->can("isa")B= $resolved->isa($asserted)
| |||||||||||||||||||||||||||||||||||||
| 141 | 1 | DBIO::Exception->throw( | |||||||||||||||||||||||||||||||||||
| 142 | "Extension '$name' ($class) belongs to the driver $asserted, " | ||||||||||||||||||||||||||||||||||||
| 143 | . "but the schema's driver is $resolved" | ||||||||||||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||
| 146 | 2 | return 1; | |||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||
| 149 | 6 | 1; | |||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||||||||||