MSSQL/Diff/View.pm

filter: Statementstmt 100% 90 / 90 Branchbran 87.5% 35 / 40 Conditioncond 73.6% 14 / 19 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 92.0% 162 / 176 CC 33 SCAR 19.5
CC
33
Cov
93%
diff
10.2
body
10.1
__ANON__
9.0
CRAP 7.1
« MSSQL/Deploy.pm ↑ Summary MSSQL/Introspect/ForeignKeys.pm »
1 package DBIO::MSSQL::Diff::View;
2 # ABSTRACT: Diff operations for MSSQL views
3  
4 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 36 our $VERSION = '0.900007';
8  
9 36 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 36 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 36 ▶use DBIO::MSSQL::DDL qw(_quote_table);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 36 __PACKAGE__->mk_diff_accessors(qw/view_name definition/);
16  
17  
18 36 ▶sub body {
Subroutine body: called CC=10 SCAR=23.1
Pod documented
19 36 my ($class, $def) = @_;
20 36 ▶ return undef unless defined $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def 1 35 36
21 35 my $text = $def;
22 35 pos($text) = 0;
23 # comments before the header are not part of it
24 35 ▶ 1 while $text =~ /\G(?:\s+|--[^\n]*\n|\/\*.*?\*\/)/gcs;
Branch: 1 branch
BranchTrueFalseTotal
$text =~ m[\G(?:\s+|--[^\n]*\n|/\*.*?\*/)]cgs and do { '???' } 1 35 36
25 35 ▶ if ($text =~ /\GCREATE\s+(?:OR\s+ALTER\s+)?VIEW\s+/gci) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\GCREATE\s+(?:OR\s+ALTER\s+)?VIEW\s+/cig) 34 1 35
26 # the name: up to three parts, each [bracketed], "quoted" or plain
27 34 my $part = qr/(?:\[(?:[^\]]|\]\])*\]|"[^"]*"|[^\s.(\[\]"]+)/;
28 34 ▶ if ($text =~ /\G$part(?:\.$part){0,2}/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G$part(?:\.$part){0,2}/cg) 34 0 34
29 34 ▶ while (pos($text) < length $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $text < length $text) 81 0 81
30 81 ▶ if ($text =~ /\GAS\b/gci) { last }
Branch: 4 branches
BranchTrueFalseTotal
if ($text =~ /\GAS\b/cig) { } 34 47 81
elsif ($text =~ /\G\[(?:[^\]]|\]\])*\]/cg) { } 1 46 47
elsif ($text =~ /\G"[^"]*"/cg) { } 0 46 46
elsif ($text =~ /\G\w+/cg) { } 3 43 46
31 elsif ($text =~ /\G\[(?:[^\]]|\]\])*\]/gc) { }
32 elsif ($text =~ /\G"[^"]*"/gc) { }
33 elsif ($text =~ /\G\w+/gc) { }
34 43 else { $text =~ /\G./gcs }
35 }
36 34 $def = substr $text, pos($text);
37 }
38 }
39 35 $def =~ s/\A\s+//;
40 35 $def =~ s/[\s;]+\z//;
41 35 return $def;
42 }
43  
44  
45 22 ▶sub normalize {
Subroutine normalize: called CC=2 SCAR=6.9
Pod documented
46 22 my ($class, $def) = @_;
47 22 my $body = $class->body($def);
48 22 ▶ return undef unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 1 21 22
49 21 $body =~ s/\s+/ /g;
50 21 return $body;
51 }
52  
53  
54 94 ▶sub diff {
Subroutine diff: called CC=10 SCAR=23.2
Pod documented
55 94 my ($class, $source, $target) = @_;
56 188 ▶ my $views = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
57 188 my ($tables) = @_;
58 25 return +{ map { $_ => $tables->{$_} } grep {
59 274 ▶ ref $tables->{$_} eq 'HASH' && ($tables->{$_}{kind} // '') eq 'view'
Condition80%4/5
ref $tables->{$_} eq 'HASH' && ($tables->{$_}{'kind'} // '') eq 'view'
!ll&&!rl&&r
024925
$tables->{$_}{"kind"} // ""
l!l
25915
MC/DC50%1/2
ref $tables->{$_} eq 'HASH' && ($tables->{$_}{'kind'} // '') eq 'view'
ref $tables->{$_} eq 'HASH' $tables->{$_}{"kind"}
Truth table66%2/3
A && B
A= ref $tables->{$_} eq 'HASH'B= $tables->{$_}{"kind"}
ABresult
0X0
100
111
60 188 ▶ } keys %{ $tables // {} } };
Condition50%1/2
$tables // {}
l!l
1880
MC/DC0%0/1
$tables // {}
$tables
Truth table50%1/2
A
A= $tables
Aresult
11
00
61 94 };
62 94 my ($src, $tgt) = ($views->($source), $views->($target));
63  
64 94 my (@ops, @drops);
65 94 for my $name (sort keys %$tgt) {
66 14 my $def = $tgt->{$name}{definition};
67 14 my $new = $class->normalize($def);
68 14 my $action;
69 14 ▶ if (!exists $src->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $src->{$name}) { } 9 5 14
70 9 $action = 'create';
71 }
72 else {
73 5 my $old = $class->normalize($src->{$name}{definition});
74 5 ▶ next if defined $new && defined $old && $new eq $old;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new and defined $old and $new eq $old 2 3 5
Condition50%3/6
defined $new and defined $old and $new eq $old
!ll&&!rl&&r
032
defined $new and defined $old
!ll&&!rl&&r
005
MC/DC33%1/3
defined $new and defined $old and $new eq $old
defined $new defined $old $new eq $old
Truth table50%2/4
A and B and C
A= defined $newB= defined $oldC= $new eq $old
ABCresult
0XX0
10X0
1100
1111
75 3 ▶ next unless defined $new;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new 0 3 3
76 3 $action = 'alter';
77 }
78 12 ▶ DBIO::Exception->throw("DBIO::MSSQL: the view '$name' is declared without a "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new 1 11 12
79 . 'definition, so there is nothing to CREATE VIEW') unless defined $new;
80 11 push @ops, $class->new(action => $action, view_name => $name, definition => $def);
81 }
82 93 for my $name (sort keys %$src) {
83 11 ▶ next if exists $tgt->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tgt->{$name} 5 6 11
84 push @drops, $class->new(action => 'drop', view_name => $name,
85 6 definition => $src->{$name}{definition});
86 }
87  
88 # a view that reads another view comes after it when written, before it
89 # when dropped (karr k82)
90 93 @ops = $class->_in_dependency_order(\@ops, $tgt);
91 91 @drops = reverse $class->_in_dependency_order(\@drops, $src);
92 91 return (@ops, @drops);
93 }
94  
95 # The ops put in the order of the views they read, from the depends_on of
96 # the model entries (the views a view reads, by model key). Only the views the
97 # ops are about are ordered among themselves; a view that is read and not part
98 # of the ops is there already (or stays). A view read that the model does not
99 # know, and a cycle, are refused before any statement.
100 184 ▶sub _in_dependency_order {
Subroutine _in_dependency_order: called CC=2 SCAR=6.9
101 184 my ($class, $ops, $views) = @_;
102 184 my %op = map { $_->view_name => $_ } @$ops;
103 184 my (@out, %state);
104 184 ▶ my $visit; $visit = sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
105 21 my ($name, @path) = @_;
106 21 ▶ return if ($state{$name} // 0) == 2;
Branch: 1 branch
BranchTrueFalseTotal
if ($state{$name} // 0) == 2 3 18 21
Condition100%2/2
$state{$name} // 0
l!l
417
MC/DC100%1/1
$state{$name} // 0
$state{$name}
Truth table100%2/2
A
A= $state{$name}
Aresult
11
00
107 DBIO::Exception->throw(sprintf
108 'DBIO::MSSQL: the views %s read each other in a cycle; the order to '
109 . 'write them in cannot be found, refused', join ' -> ', @path, $name)
110 18 ▶ if ($state{$name} // 0) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if ($state{$name} // 0) == 1 1 17 18
Condition100%2/2
$state{$name} // 0
l!l
117
MC/DC100%1/1
$state{$name} // 0
$state{$name}
Truth table100%2/2
A
A= $state{$name}
Aresult
11
00
111 17 $state{$name} = 1;
112 17 ▶ for my $dep (sort @{ $views->{$name}{depends_on} // [] }) {
Condition100%2/2
$views->{$name}{"depends_on"} // []
l!l
611
MC/DC100%1/1
$views->{$name}{"depends_on"} // []
$views->{$name}{"depends_on"}
Truth table100%2/2
A
A= $views->{$name}{"depends_on"}
Aresult
11
00
113 DBIO::Exception->throw(sprintf
114 "DBIO::MSSQL: the view '%s' reads the view '%s', which the model does "
115 . 'not know (a schema that was not introspected?); the order cannot be '
116 . 'found, refused', $name, $dep)
117 6 ▶ unless exists $views->{$dep};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $views->{$dep} 1 5 6
118 5 ▶ $visit->($dep, @path, $name) if $op{$dep};
Branch: 1 branch
BranchTrueFalseTotal
if $op{$dep} 5 0 5
119 }
120 14 $state{$name} = 2;
121 14 push @out, $op{$name};
122 184 };
123 184 $visit->($_) for sort keys %op;
124 182 return @out;
125 }
126  
127  
128 13 ▶sub as_sql {
Subroutine as_sql: called CC=2 SCAR=6.9
Pod documented
129 13 my ($self) = @_;
130 13 my $name = _quote_table($self->view_name);
131 13 ▶ return "DROP VIEW $name;" if $self->action eq 'drop';
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 5 8 13
132 8 return sprintf "CREATE OR ALTER VIEW %s AS\n%s;", $name,
133 $self->body($self->definition);
134 }
135  
136  
137 1 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
138 1 my ($self) = @_;
139 1 return sprintf '%s view: %s', $self->summary_prefix, $self->view_name;
140 }
141  
142 36 1;
143  
144 __END__
« MSSQL/Deploy.pm ↑ Summary MSSQL/Introspect/ForeignKeys.pm »