Firebird/Diff/View.pm

filter: Statementstmt 100% 68 / 68 Branchbran 95.4% 21 / 22 Conditioncond 74.1% 23 / 31 MC/DCmcdc 61.1% 11 / 18 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 89.7% 140 / 156 CC 26 SCAR 19.1
CC
26
Cov
93%
diff
14.0
as_sql
4.0
_reads
3.2
CRAP 6.8
« Firebird/Diff/Index.pm ↑ Summary Firebird/Diff.pm »
1 package DBIO::Firebird::Diff::View;
2 # ABSTRACT: Diff operations for Firebird views
3  
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 our $VERSION = '0.900007';
8  
9 11 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use DBIO::Firebird::DDL qw(_firebird_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 11 __PACKAGE__->mk_diff_accessors(qw/view_name view_source old_source replaced/);
16  
17  
18 6 ▶sub normalize {
Subroutine normalize: called CC=2 SCAR=7.0
Pod documented
19 6 my ($class, $source) = @_;
20 6 ▶ return '' unless defined $source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 0 6 6
21 6 $source =~ s/\s+/ /g;
22 6 $source =~ s/^ //;
23 6 $source =~ s/ \z//;
24 6 $source =~ s/\s*;\z//;
25 6 return $source;
26 }
27  
28 359 ▶sub _is_view { ($_[0] // {})->{kind} && $_[0]{kind} eq 'view' }
Subroutine _is_view: called CC=3 SCAR=11.2
Branch: 1 branch
BranchTrueFalseTotal
+($_[0] // {})->{"kind"} and $_[0]{"kind"} eq "view" 351 8 359
Condition50%1/2
$_[0] // {}
l!l
3590
MC/DC0%0/1
$_[0] // {}
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
29  
30 # The views $name reads (by quoted name; an unquoted reference reads the upper
31 # case name), for ordering only.
32 15 ▶sub _reads {
Subroutine _reads: called CC=3 SCAR=11.8
33 15 my ($source, @others) = @_;
34 return [ grep {
35 15 my $o = $_;
36 29 ▶ $source =~ /"\Q$o\E"/ || ($o eq uc($o) && $source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i)
Condition50%3/6
$source =~ /"\Q$o\E"/ || $o eq uc $o && $source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i
l!l&&r!l&&!r
3026
$o eq uc $o and $source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i
!ll&&!rl&&r
2600
MC/DC33%1/3
$source =~ /"\Q$o\E"/ || $o eq uc $o && $source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i
$source =~ /"\Q$o\E"/ $o eq uc $o $source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i
Truth table50%2/4
A || B and C
A= $source =~ /"\Q$o\E"/B= $o eq uc $oC= $source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i
ABCresult
1XX1
0111
00X0
0100
37 } @others ];
38 }
39  
40  
41 146 ▶sub diff {
Subroutine diff: called CC=14 SCAR=26.4
Pod documented
42 146 my ($class, $source, $target) = @_;
43 146 ▶ $source ||= {};
Condition50%1/2
$source ||= {}
l!l
1460
MC/DC0%0/1
$source ||= {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
44 146 ▶ $target ||= {};
Condition50%1/2
$target ||= {}
l!l
1460
MC/DC0%0/1
$target ||= {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
45  
46 146 my (%old, %new);
47 146 for my $name (keys %$source) {
48 176 ▶ next unless _is_view($source->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_view($source->{$name}) 168 8 176
49 8 ▶ next if exists $target->{$name} && !_is_view($target->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if exists $target->{$name} and not _is_view($target->{$name}) 1 7 8
Condition100%3/3
exists $target->{$name} and not _is_view($target->{$name})
!ll&&!rl&&r
431
MC/DC100%2/2
exists $target->{$name} and not _is_view($target->{$name})
exists $target->{$name} not _is_view($target->{$name})
Truth table100%3/3
A and not(B)
A= exists $target->{$name}B= not _is_view($target->{$name})
ABresult
0X0
100
111
50 7 $old{$name} = $source->{$name}{view_source};
51 }
52 146 for my $name (keys %$target) {
53 175 ▶ next unless _is_view($target->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_view($target->{$name}) 164 11 175
54 11 ▶ next if exists $source->{$name} && !_is_view($source->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if exists $source->{$name} and not _is_view($source->{$name}) 1 10 11
Condition100%3/3
exists $source->{$name} and not _is_view($source->{$name})
!ll&&!rl&&r
731
MC/DC100%2/2
exists $source->{$name} and not _is_view($source->{$name})
exists $source->{$name} not _is_view($source->{$name})
Truth table100%3/3
A and not(B)
A= exists $source->{$name}B= not _is_view($source->{$name})
ABresult
0X0
100
111
55 10 $new{$name} = $target->{$name}{view_source};
56 }
57  
58 146 my @gone = grep { !exists $new{$_} } keys %old;
59 146 ▶ my @make = grep { !exists $old{$_} || $class->normalize($old{$_}) ne $class->normalize($new{$_}) }
Condition100%3/3
!exists $old{$_} || $class->normalize($old{$_}) ne $class->normalize($new{$_})
l!l&&r!l&&!r
721
MC/DC100%2/2
!exists $old{$_} || $class->normalize($old{$_}) ne $class->normalize($new{$_})
!exists $old{$_} $class->normalize($old{$_}) ne $class->normalize($new{$_})
Truth table100%3/3
not(A) || B
A= !exists $old{$_}B= $class->normalize($old{$_}) ne $class->normalize($new{$_})
ABresult
1X1
011
000
60 keys %new;
61 146 my %replaced = map { $_ => 1 } grep { exists $old{$_} } @make;
62 146 my @unmake = (@gone, grep { $replaced{$_} } @make);
63  
64 my @drops = reverse DBIO::Diff::Op::_order_by_dependency(
65 146 ▶ sub { _reads($old{ $_[0] } // '', @unmake) }, @unmake);
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$old{$_[0]} // ""
l!l
60
MC/DC0%0/1
$old{$_[0]} // ""
$old{$_[0]}
Truth table50%1/2
A
A= $old{$_[0]}
Aresult
11
00
66 my @creates = DBIO::Diff::Op::_order_by_dependency(
67 146 ▶ sub { _reads($new{ $_[0] } // '', @make) }, @make);
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$new{$_[0]} // ""
l!l
81
MC/DC100%1/1
$new{$_[0]} // ""
$new{$_[0]}
Truth table100%2/2
A
A= $new{$_[0]}
Aresult
11
00
68  
69 return (
70 drops => [ map { $class->new(
71 action => 'drop', view_name => $_, view_source => $old{$_},
72 6 ▶ replaced => ($replaced{$_} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$replaced{$_} ? : 2 4 6
73 ) } @drops ],
74 146 creates => [ map { $class->new(
75 action => (exists $old{$_} ? 'change' : 'create'),
76 view_name => $_,
77 view_source => $new{$_},
78 9 ▶ old_source => $old{$_},
Branch: 1 branch
BranchTrueFalseTotal
exists $old{$_} ? : 2 7 9
79 ) } @creates ],
80 );
81 }
82  
83  
84 9 ▶sub as_sql {
Subroutine as_sql: called CC=4 SCAR=13.9
Pod documented
85 9 my ($self) = @_;
86 9 my $name = _firebird_quote_ident($self->view_name);
87 9 ▶ return sprintf 'DROP VIEW %s;', $name if $self->action eq 'drop';
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 4 5 9
88 5 my $def = $self->view_source;
89 5 ▶ DBIO::Exception->throw(sprintf 'view %s: the model holds no definition (view_source) to write it from', $name)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 1 4 5
Condition66%2/3
defined $def and length $def
!ll&&!rl&&r
104
MC/DC50%1/2
defined $def and length $def
defined $def length $def
Truth table66%2/3
A and B
A= defined $defB= length $def
ABresult
0X0
100
111
90 unless defined $def && length $def;
91 4 $def =~ s/\s*;\s*\z//;
92 4 return sprintf 'CREATE VIEW %s AS %s;', $name, $def;
93 }
94  
95  
96 6 ▶sub summary {
Subroutine summary: called CC=3 SCAR=11.0
Pod documented
97 6 my ($self) = @_;
98 return sprintf '~ view: %s (dropped, created again after the tables)', $self->view_name
99 6 ▶ if $self->action eq 'drop' && $self->{replaced};
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" and $self->{"replaced"} 2 4 6
Condition100%3/3
$self->action eq "drop" and $self->{"replaced"}
!ll&&!rl&&r
312
MC/DC100%2/2
$self->action eq "drop" and $self->{"replaced"}
$self->action eq "drop" $self->{"replaced"}
Truth table100%3/3
A and B
A= $self->action eq "drop"B= $self->{"replaced"}
ABresult
0X0
100
111
100 4 return sprintf '%s view: %s', $self->summary_prefix, $self->view_name;
101 }
102  
103 11 1;
104  
105 __END__
« Firebird/Diff/Index.pm ↑ Summary Firebird/Diff.pm »