DB2/Diff/View.pm

filter: Statementstmt 100% 73 / 73 Branchbran 100% 30 / 30 Conditioncond 82.3% 28 / 34 MC/DCmcdc 66.6% 12 / 18 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 92.9% 157 / 169 CC 31 SCAR 27.4
CC
31
Cov
96%
diff
24.1
as_sql
5.1
is_view
3.0
CRAP 15.5
« DB2/Type.pm ↑ Summary DB2/Storage/Cursor.pm »
1 package DBIO::DB2::Diff::View;
2 # ABSTRACT: Diff operations for DB2 views
3  
4 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 45 our $VERSION = '0.900007';
8  
9 45 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 45 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 45 ▶use DBIO::DB2::DDL qw(_db2_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 45 __PACKAGE__->mk_diff_accessors(qw/view_name view_info replaced/);
15  
16  
17  
18  
19 691 ▶sub is_view { return ((($_[0] // {})->{kind}) // '') eq 'view' }
Subroutine is_view: called CC=3 SCAR=11.0
Pod documented
Condition100%4/4
+($_[0] // {})->{"kind"} // ""
l!l
59398
$_[0] // {}
l!l
62962
MC/DC100%1/1
+($_[0] // {})->{"kind"} // ""
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
20  
21 52 ▶sub _comparable {
Subroutine _comparable: called CC=2 SCAR=6.9
22 52 my ($def) = @_;
23 52 ▶ return undef unless defined $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def 3 49 52
24 49 $def =~ s/\s+/ /g;
25 49 $def =~ s/\A //;
26 49 $def =~ s/ \z//;
27 49 return $def;
28 }
29  
30  
31 167 ▶sub diff {
Subroutine diff: called CC=24 SCAR=31.8
Pod documented
32 167 my ($class, $source, $target, $schema) = @_;
33 167 ▶ $source //= {};
Condition50%1/2
$source //= {}
l!l
1670
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
34 167 ▶ $target //= {};
Condition50%1/2
$target //= {}
l!l
1670
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
35  
36 # The schema the operations run in is the live one, the source model's: its
37 # entries carry it (Introspect::Tables). A qualified read in a definition is
38 # judged against it (karr k55); a model without an entry has no schema to give.
39 167 ▶ ($schema) = grep { defined } map { $_->{schema} } map { $source->{$_} } sort keys %$source
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema 166 1 167
40 unless defined $schema;
41  
42 167 my %seen;
43 167 my (%drop, %create, %replaced);
44 167 for my $name (sort grep { !$seen{$_}++ } keys %$source, keys %$target) {
45 225 ▶ my $old = is_view($source->{$name}) ? $source->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
is_view($source->{$name}) ? : 31 194 225
46 225 ▶ my $new = is_view($target->{$name}) ? $target->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
is_view($target->{$name}) ? : 42 183 225
47 225 ▶ next unless $old || $new;
Branch: 1 branch
BranchTrueFalseTotal
unless $old or $new 178 47 225
Condition100%3/3
$old or $new
l!l&&r!l&&!r
3116178
MC/DC100%2/2
$old or $new
$old $new
Truth table100%3/3
A or B
A= $oldB= $new
ABresult
1X1
011
000
48  
49 47 ▶ if ($old && $new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old and $new) 26 21 47
Condition100%3/3
$old and $new
!ll&&!rl&&r
16526
MC/DC100%2/2
$old and $new
$old $new
Truth table100%3/3
A and B
A= $oldB= $new
ABresult
0X0
100
111
50 26 my ($was, $will) = map { _comparable($_->{view_definition}) } $old, $new;
51 26 ▶ next if !defined $was || !defined $will || $was eq $will;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $was && defined $will or $was eq $will 20 6 26
Condition83%5/6
not defined $was && defined $will or $was eq $will
l!l&&r!l&&!r
2186
defined $was && defined $will
!ll&&!rl&&r
2024
MC/DC66%2/3
not defined $was && defined $will or $was eq $will
defined $was defined $will $was eq $will
Truth table75%3/4
not(A && B) or C
A= defined $wasB= defined $willC= $was eq $will
ABCresult
0XX1
10X1
1111
1100
52 }
53  
54 27 ▶ $drop{$name} = $old if $old;
Branch: 1 branch
BranchTrueFalseTotal
if $old 11 16 27
55 27 ▶ $create{$name} = $new if $new;
Branch: 1 branch
BranchTrueFalseTotal
if $new 22 5 27
56 27 ▶ $replaced{$name} = 1 if $old && $new;
Branch: 1 branch
BranchTrueFalseTotal
if $old and $new 6 21 27
Condition100%3/3
$old and $new
!ll&&!rl&&r
1656
MC/DC100%2/2
$old and $new
$old $new
Truth table100%3/3
A and B
A= $oldB= $new
ABresult
0X0
100
111
57 }
58  
59 # A view that reads a view being replaced is replaced with it (karr k54),
60 # transitively, although its own definition is the same. An unchanged view
61 # has the same reads in both models.
62 20 my %stay = map { $_ => $target->{$_} } grep {
63 167 ▶ is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_}
Condition83%5/6
is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_}
!ll&&!rl&&r
189620
is_view($source->{$_}) and is_view($target->{$_})
!ll&&!rl&&r
189026
MC/DC66%2/3
is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_}
is_view($source->{$_}) is_view($target->{$_}) !$replaced{$_}
Truth table75%3/4
A and B && not(C)
A= is_view($source->{$_})B= is_view($target->{$_})C= !$replaced{$_}
ABCresult
0XX0
10X0
1100
1111
64 } keys %$target;
65 167 my @grow = keys %replaced;
66 167 ▶ while (defined(my $changed = shift @grow)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $changed = shift @grow)) 13 167 180
67 13 for my $name (sort keys %stay) {
68 10 next unless grep { $_ eq $changed } DBIO::DB2::DDL::view_dependencies(
69 22 ▶ [ keys %stay, keys %replaced ], $name, $stay{$name}{view_definition}, $schema);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $changed;} DBIO::DB2::DDL::view_dependencies([keys %stay, keys %replaced], $name, $stay{$name}{"view_definition"}, $schema) 15 7 22
70 7 $drop{$name} = $source->{$name};
71 7 $create{$name} = $target->{$name};
72 7 $replaced{$name} = 1;
73 7 delete $stay{$name};
74 7 push @grow, $name;
75 }
76 }
77  
78 # the order is worked out for the whole set before an op is made: a cycle
79 # throws and leaves nothing half done
80 167 my @drop_order = reverse DBIO::DB2::DDL::order_views({ map { $_ => $drop{$_}{view_definition} } keys %drop }, $schema);
81 167 my @create_order = DBIO::DB2::DDL::order_views({ map { $_ => $create{$_}{view_definition} } keys %create }, $schema);
82  
83 return (
84 (map { $class->new(
85 action => 'drop',
86 view_name => $_,
87 view_info => $drop{$_},
88 18 ▶ replaced => ($replaced{$_} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$replaced{$_} ? : 13 5 18
89 ) } @drop_order),
90 164 (map { $class->new(
91 action => 'create',
92 view_name => $_,
93 23 view_info => $create{$_},
94 ) } @create_order),
95 );
96 }
97  
98  
99 30 ▶sub as_sql {
Subroutine as_sql: called CC=5 SCAR=16.2
Pod documented
100 30 my ($self) = @_;
101  
102 30 ▶ return sprintf 'DROP VIEW %s;', _db2_quote_ident($self->view_name)
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 12 18 30
103 if $self->action eq 'drop';
104  
105 18 ▶ my $def = ($self->view_info // {})->{view_definition};
Condition50%1/2
$self->view_info // {}
l!l
180
MC/DC0%0/1
$self->view_info // {}
$self->view_info
Truth table50%1/2
A
A= $self->view_info
Aresult
11
00
106 18 ▶ Carp::croak(sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 1 17 18
Condition66%2/3
defined $def and length $def
!ll&&!rl&&r
1017
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
107 'Cannot create view %s: the target model holds no view definition for it, '
108 . 'and a view cannot be written from its columns', $self->view_name)
109 unless defined $def && length $def;
110  
111 17 return sprintf 'CREATE VIEW %s %s;', _db2_quote_ident($self->view_name), $def;
112 }
113  
114  
115 11 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
116 11 my ($self) = @_;
117 11 return sprintf '%s view: %s', $self->summary_prefix, $self->view_name;
118 }
119  
120 45 1;
121  
122 __END__
« DB2/Type.pm ↑ Summary DB2/Storage/Cursor.pm »