Sybase/Diff/View.pm

filter: Statementstmt 100% 82 / 82 Branchbran 100% 10 / 10 Conditioncond 82.3% 14 / 17 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 95.5% 130 / 136 CC 18 SCAR 13.6
CC
18
Cov
97%
diff
8.0
__ANON__
6.0
__ANON__
3.0
CRAP 3.9
« Sybase/Introspect/Tables.pm ↑ Summary Sybase/SQLMaker.pm »
1 package DBIO::Sybase::Diff::View;
2 # ABSTRACT: Diff Sybase ASE views
3  
4 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 our $VERSION = '0.900007';
8  
9 31 ▶use DBIO::Diff::Op ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use DBIO::Sybase::Quote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 62 ▶sub diff {
Subroutine diff: called CC=8 SCAR=20.8
Pod documented
15 62 my ($class, $source, $target) = @_;
16 62 ▶ $source //= {};
Condition100%2/2
$source //= {}
l!l
1448
MC/DC100%1/1
$source //= {}
$source
Truth table100%2/2
A
A= $source
Aresult
11
00
17 62 ▶ $target //= {};
Condition100%2/2
$target //= {}
l!l
1448
MC/DC100%1/1
$target //= {}
$target
Truth table100%2/2
A
A= $target
Aresult
11
00
18  
19 62 my (%drop, %create);
20 62 for my $name (keys %$source) {
21 18 ▶ $drop{$name} = 1 unless exists $target->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $target->{$name} 9 9 18
22 }
23 62 for my $name (keys %$target) {
24 18 ▶ if (!exists $source->{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $source->{$name}) { } 9 9 18
elsif (_normalize($source->{$name}{'definition'}) ne _normalize($target->{$name}{'definition'})) { } 4 5 9
25 9 $create{$name} = 1;
26 }
27 elsif (_normalize($source->{$name}{definition}) ne _normalize($target->{$name}{definition})) {
28 4 $drop{$name} = $create{$name} = 1;
29 }
30 }
31  
32 62 my @ops;
33 # A dropped view is dropped before the views it reads from.
34 push @ops, map {
35 11 DBIO::Sybase::Diff::View::Drop->new(action => 'drop', view => $source->{$_})
36 13 ▶ } _order(\%drop, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
37 13 my ($n) = @_;
38 13 ▶ return grep { $_ ne $n && grep { $_ eq $n } @{ $source->{$_}{depends_on} // [] } }
Condition80%4/5
$_ ne $n && grep({$_ eq $n;} @{$source->{$_}{'depends_on'} // [];})
!ll&&!rl&&r
13124
$source->{$_}{"depends_on"} // []
l!l
160
MC/DC100%2/2
$_ ne $n && grep({$_ eq $n;} @{$source->{$_}{'depends_on'} // [];})
$_ ne $n grep({$_ eq $n;} @{$source->{$_}{'depends_on'} // [];})
MC/DC0%0/1
$source->{$_}{"depends_on"} // []
$source->{$_}{"depends_on"}
Truth table100%3/3
A && B
A= $_ ne $nB= grep({$_ eq $n;} @{$source->{$_}{'depends_on'} // [];})
ABresult
0X0
100
111
Truth table50%1/2
A
A= $source->{$_}{"depends_on"}
Aresult
11
00
39 keys %drop;
40 62 }, 'dropped');
41 # A created view is created after the views it reads from.
42 push @ops, map {
43 11 DBIO::Sybase::Diff::View::Create->new(action => 'create', view => $target->{$_})
44 13 ▶ } _order(\%create, sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
45 13 my ($n) = @_;
46 13 ▶ return grep { $create{$_} } @{ $target->{$n}{depends_on} // [] };
Condition50%1/2
$target->{$n}{"depends_on"} // []
l!l
130
MC/DC0%0/1
$target->{$n}{"depends_on"} // []
$target->{$n}{"depends_on"}
Truth table50%1/2
A
A= $target->{$n}{"depends_on"}
Aresult
11
00
47 61 }, 'created');
48  
49 60 return @ops;
50 }
51  
52 18 ▶sub _normalize {
Subroutine _normalize: called CC=2 SCAR=7.0
53 18 my ($sql) = @_;
54 18 ▶ $sql //= '';
Condition50%1/2
$sql //= ""
l!l
180
MC/DC0%0/1
$sql //= ""
$sql
Truth table50%1/2
A
A= $sql
Aresult
11
00
55 18 $sql =~ s/\s+/ /g;
56 18 $sql =~ s/\A //;
57 18 $sql =~ s/ \z//;
58 18 return $sql;
59 }
60  
61 # The names of %$set in an order where every name comes after the names
62 # $before->($name) returns; name order otherwise. A cycle throws.
63 123 ▶sub _order {
Subroutine _order: called CC=2 SCAR=6.9
64 123 my ($set, $before, $what) = @_;
65 123 my (@out, %state);
66 123 ▶ my $visit; $visit = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
67 32 my ($n, @path) = @_;
68 32 ▶ return if ($state{$n} // '') eq 'done';
Branch: 1 branch
BranchTrueFalseTotal
if ($state{$n} // "") eq "done" 4 28 32
Condition100%2/2
$state{$n} // ""
l!l
626
MC/DC100%1/1
$state{$n} // ""
$state{$n}
Truth table100%2/2
A
A= $state{$n}
Aresult
11
00
69 28 ▶ if (($state{$n} // '') eq 'open') {
Branch: 1 branch
BranchTrueFalseTotal
if (($state{$n} // "") eq "open") 2 26 28
Condition100%2/2
$state{$n} // ""
l!l
226
MC/DC100%1/1
$state{$n} // ""
$state{$n}
Truth table100%2/2
A
A= $state{$n}
Aresult
11
00
70 2 my @cycle = (@path[ (grep { $path[$_] eq $n } 0 .. $#path)[0] .. $#path ], $n);
71 2 DBIO::Exception->throw(
72 'DBIO::Sybase: the views to be ' . $what . ' depend on each other in a '
73 . 'cycle (' . join(' -> ', @cycle) . '), no order creates or drops them');
74 }
75 26 $state{$n} = 'open';
76 26 $visit->($_, @path, $n) for sort $before->($n);
77 22 $state{$n} = 'done';
78 22 push @out, $n;
79 123 };
80 123 $visit->($_) for sort keys %$set;
81 121 undef $visit;
82 121 return @out;
83 }
84  
85 package DBIO::Sybase::Diff::View::Create;
86 31 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 31 __PACKAGE__->mk_diff_accessors(qw(view));
89  
90 5 ▶sub as_sql {
Subroutine as_sql: called CC=1 SCAR=0.0
91 5 my $self = shift;
92 5 my $view = $self->view;
93 sprintf 'CREATE VIEW %s AS %s;',
94 5 DBIO::Sybase::Quote::quote_ident($view->{view_name}), $view->{definition};
95 }
96 1 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
97 1 my $self = shift;
98 1 'CREATE VIEW ' . $self->view->{view_name};
99 }
100  
101 package DBIO::Sybase::Diff::View::Drop;
102 31 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 31 __PACKAGE__->mk_diff_accessors(qw(view));
105  
106 5 ▶sub as_sql {
Subroutine as_sql: called CC=1 SCAR=0.0
107 5 my $self = shift;
108 5 'DROP VIEW ' . DBIO::Sybase::Quote::quote_ident($self->view->{view_name}) . ';';
109 }
110 1 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
111 1 my $self = shift;
112 1 'DROP VIEW ' . $self->view->{view_name};
113 }
114  
115 31 1;
116  
117 __END__
« Sybase/Introspect/Tables.pm ↑ Summary Sybase/SQLMaker.pm »