Oracle/Diff/View.pm

filter: Statementstmt 98.7% 78 / 79 Branchbran 96.4% 27 / 28 Conditioncond 91.1% 31 / 34 MC/DCmcdc 84.2% 16 / 19 Subroutinesub 92.3% 12 / 13 Podpod 100% 4 / 4 totaltotal 94.9% 168 / 177 CC 34 SCAR 22.8
CC
34
Cov
96%
diff
18.0
__ANON__
9.0
as_sql
4.0
CRAP 9.8
« Oracle/Introspect/Views.pm ↑ Summary Oracle/Diff/Table.pm »
1 package DBIO::Oracle::Diff::View;
2 # ABSTRACT: Diff operations for Oracle views
3  
4 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 30 our $VERSION = '0.900007';
8  
9 30 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 30 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use DBIO::Oracle::Identifier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 30 __PACKAGE__->mk_diff_accessors(qw(view_name definition columns));
15  
16  
17  
18 0 ▶sub table_name { $_[0]->view_name }
Subroutine table_name: not called CC=1 SCAR=6.9
Pod documented
19  
20  
21 116 ▶sub diff {
Subroutine diff: called CC=18 SCAR=28.9
Pod documented
22 116 my ($class, $source_tables, $target_tables, $source_views, $target_views) = @_;
23 116 ▶ $_ //= {} for $source_tables, $target_tables, $source_views, $target_views;
Condition50%1/2
$_ //= {}
l!l
4640
MC/DC0%0/1
$_ //= {}
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
24  
25 116 ▶ my $is_view = sub { ( ref $_[0] eq 'HASH' && ( $_[0]{kind} // '' ) eq 'view' ) };
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "HASH" and ($_[0]{"kind"} // "") eq "view" 353 0 353
Condition100%2/2
$_[0]{"kind"} // ""
l!l
200153
MC/DC100%1/1
$_[0]{"kind"} // ""
$_[0]{"kind"}
Truth table100%2/2
A
A= $_[0]{"kind"}
Aresult
11
00
26  
27 116 my (@create, @alter, @drop);
28 116 for my $name (sort keys %$target_tables) {
29 177 ▶ next unless $is_view->($target_tables->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless &$is_view($target_tables->{$name}) 144 33 177
30 33 my $def = $target_views->{$name}{definition};
31 33 my $cols = $target_views->{$name}{columns};
32 33 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 5 28 33
Condition100%3/3
defined $def and length $def
!ll&&!rl&&r
4128
MC/DC100%2/2
defined $def and length $def
defined $def length $def
Truth table100%3/3
A and B
A= defined $defB= length $def
ABresult
0X0
100
111
33 "DBIO::Oracle::Diff refuses to create view $name: the model carries no "
34 . 'view definition to create it from')
35 unless defined $def && length $def;
36  
37 28 ▶ if (!exists $source_tables->{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $source_tables->{$name}) { } 13 15 28
elsif (&$is_view($source_tables->{$name})) { } 14 1 15
38 13 push @create, $class->new(action => 'create', view_name => $name, definition => $def, columns => $cols);
39 }
40 elsif ($is_view->($source_tables->{$name})) {
41 14 my $was = $source_views->{$name}{definition};
42 14 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $was and length $was 3 11 14
Condition100%3/3
defined $was and length $was
!ll&&!rl&&r
2111
MC/DC100%2/2
defined $was and length $was
defined $was length $was
Truth table100%3/3
A and B
A= defined $wasB= length $was
ABresult
0X0
100
111
43 "DBIO::Oracle::Diff refuses to compare view $name: the source model "
44 . 'carries no view definition to compare it with')
45 unless defined $was && length $was;
46 11 my $was_cols = $source_views->{$name}{columns};
47 11 ▶ push @alter, $class->new(action => 'alter', view_name => $name, definition => $def, columns => $cols)
Branch: 1 branch
BranchTrueFalseTotal
if _normalize($was) ne _normalize($def) or $was_cols and $cols and join("\0", @$was_cols) ne join("\0", @$cols) 7 4 11
Condition100%9/9
_normalize($was) ne _normalize($def) or $was_cols and $cols and join("\0", @$was_cols) ne join("\0", @$cols)
l!l&&r!l&&!r
524
$was_cols and $cols and join("\0", @$was_cols) ne join("\0", @$cols)
!ll&&!rl&&r
312
$was_cols and $cols
!ll&&!rl&&r
213
MC/DC100%4/4
_normalize($was) ne _normalize($def) or $was_cols and $cols and join("\0", @$was_cols) ne join("\0", @$cols)
_normalize($was) ne _normalize($def) $was_cols $cols join("\0", @$was_cols) ne join("\0", @$cols)
Truth table100%5/5
A or B and C and D
A= _normalize($was) ne _normalize($def)B= $was_colsC= $colsD= join("\0", @$was_cols) ne join("\0", @$cols)
ABCDresult
1XXX1
01111
00XX0
010X0
01100
48 if _normalize($was) ne _normalize($def)
49 || ( $was_cols && $cols && join("\0", @$was_cols) ne join("\0", @$cols) );
50 }
51 # a table that became a view: DBIO::Oracle::Diff::Table refuses it
52 }
53  
54 108 for my $name (sort keys %$source_tables) {
55 161 ▶ next unless $is_view->($source_tables->{$name}) && !exists $target_tables->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless &$is_view($source_tables->{$name}) and not exists $target_tables->{$name} 150 11 161
Condition100%3/3
&$is_view($source_tables->{$name}) and not exists $target_tables->{$name}
!ll&&!rl&&r
1391111
MC/DC100%2/2
&$is_view($source_tables->{$name}) and not exists $target_tables->{$name}
&$is_view($source_tables->{$name}) not exists $target_tables->{$name}
Truth table100%3/3
A and not(B)
A= &$is_view($source_tables->{$name})B= not exists $target_tables->{$name}
ABresult
0X0
100
111
56 11 push @drop, $class->new(action => 'drop', view_name => $name);
57 }
58  
59 # A view that rests on another view is written after it, dropped before it
60 # (karr k65): the target's dependencies order what is written, the source's
61 # what is dropped. Only the views of the run count; a view it rests on that
62 # is not written needs no edge.
63 108 my @write = _dependency_order(
64 [ @create, @alter ], $target_views, 'create or replace',
65 );
66 107 @drop = reverse _dependency_order(\@drop, $source_views, 'drop', 1);
67  
68 106 return (@drop, @write);
69 }
70  
71 # The ops, the ones a view rests on first; ties in the order of the names (the
72 # reverse of it when $descending, for a list that is reversed afterwards).
73 # A cycle is refused with the names of its views.
74 215 ▶sub _dependency_order {
Subroutine _dependency_order: called CC=3 SCAR=11.0
75 215 my ($ops, $views, $what, $descending) = @_;
76 215 my %op = map { $_->view_name => $_ } @$ops;
77  
78 215 my (@order, %state);
79 215 ▶ my $visit; $visit = sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
80 38 my ($name, @path) = @_;
81 38 ▶ return if ( $state{$name} // '' ) eq 'done';
Branch: 1 branch
BranchTrueFalseTotal
if ($state{$name} // "") eq "done" 5 33 38
Condition100%2/2
$state{$name} // ""
l!l
731
MC/DC100%1/1
$state{$name} // ""
$state{$name}
Truth table100%2/2
A
A= $state{$name}
Aresult
11
00
82 33 ▶ if ( ( $state{$name} // '' ) eq 'open' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($state{$name} // "") eq "open") 2 31 33
Condition100%2/2
$state{$name} // ""
l!l
231
MC/DC100%1/1
$state{$name} // ""
$state{$name}
Truth table100%2/2
A
A= $state{$name}
Aresult
11
00
83 2 my @cycle = ( @path[ ( grep { $path[$_] eq $name } 0 .. $#path )[0] .. $#path ], $name );
84 2 DBIO::Exception->throw(
85 "DBIO::Oracle::Diff refuses to $what views that rest on each other (cycle): "
86 . join(' -> ', @cycle));
87 }
88 31 $state{$name} = 'open';
89 31 ▶ my @deps = grep { exists $op{$_} && $_ ne $name } @{ $views->{$name}{depends_on} // [] };
Condition80%4/5
exists $op{$_} && $_ ne $name
!ll&&!rl&&r
1011
$views->{$name}{"depends_on"} // []
l!l
1615
MC/DC50%1/2
exists $op{$_} && $_ ne $name
exists $op{$_} $_ ne $name
MC/DC100%1/1
$views->{$name}{"depends_on"} // []
$views->{$name}{"depends_on"}
Truth table66%2/3
A && B
A= exists $op{$_}B= $_ ne $name
ABresult
0X0
100
111
Truth table100%2/2
A
A= $views->{$name}{"depends_on"}
Aresult
11
00
90 $visit->($_, @path, $name)
91 31 ▶ for $descending ? reverse sort(@deps) : sort(@deps);
Branch: 1 branch
BranchTrueFalseTotal
$descending ? : 11 20 31
92 25 $state{$name} = 'done';
93 25 push @order, $op{$name};
94 215 };
95 215 ▶ $visit->($_) for $descending ? reverse sort(keys %op) : sort(keys %op);
Branch: 1 branch
BranchTrueFalseTotal
$descending ? : 107 108 215
96 213 return @order;
97 }
98  
99 # Whitespace and a trailing semicolon are not a difference; a string literal and
100 # a quoted identifier are kept as they are.
101 22 ▶sub _normalize {
Subroutine _normalize: called CC=2 SCAR=6.9
102 22 my ($sql) = @_;
103 22 $sql =~ s/\A\s+//;
104 22 $sql =~ s/(?:\s*;)*\s*\z//;
105 22 my @parts = split /('(?:[^']|'')*'|"(?:[^"]|"")*")/, $sql, -1;
106 22 for my $i (grep { !($_ % 2) } 0 .. $#parts) {
107 79 $parts[$i] =~ s/\s+/ /g;
108 }
109 22 return join '', @parts;
110 }
111  
112  
113 12 ▶sub as_sql {
Subroutine as_sql: called CC=4 SCAR=13.9
Pod documented
114 12 my ($self) = @_;
115 12 my $name = DBIO::Oracle::Identifier::quote_table($self->view_name);
116 12 ▶ return sprintf 'DROP VIEW %s;', $name if $self->action eq 'drop';
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 5 7 12
117 7 my $cols = $self->columns;
118 my $list = $cols && @$cols
119 7 ▶ ? ' (' . join(', ', map { DBIO::Oracle::Identifier::quote($_) } @$cols) . ')'
Branch: 1 branch
BranchTrueFalseTotal
$cols && @$cols ? : 2 5 7
Condition66%2/3
$cols and @$cols
!ll&&!rl&&r
502
MC/DC50%1/2
$cols and @$cols
$cols @$cols
Truth table66%2/3
A and B
A= $colsB= @$cols
ABresult
0X0
100
111
120 : '';
121 7 return sprintf 'CREATE OR REPLACE VIEW %s%s AS %s;', $name, $list, $self->definition;
122 }
123  
124  
125 4 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
126 4 my ($self) = @_;
127 4 return sprintf '%s view: %s', $self->summary_prefix, $self->view_name;
128 }
129  
130 30 1;
131  
132 __END__
« Oracle/Introspect/Views.pm ↑ Summary Oracle/Diff/Table.pm »