blib/lib/DBIO/SQLite/Diff/View.pm

filter: Statementstmt 100% 48 / 48 Branchbran 93.7% 15 / 16 Conditioncond 80.0% 16 / 20 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 91.5% 98 / 107 CC 19 SCAR 20.9
CC
19
Cov
94%
diff
13.0
statements
7.2
BEGIN
1.0
CRAP 8.1
« blib/lib/DBIO/SQLite/Diff/Index.pm ↑ Summary blib/lib/DBIO/SQLite/Introspect/Indexes.pm »
1 package DBIO::SQLite::Diff::View;
2 # ABSTRACT: Diff operations for SQLite views
3  
4 190 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 190 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 190 our $VERSION = '0.900007';
8  
9 190 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 190 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 190 ▶use DBIO::SQLite::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 comparable_statement create_name_index quote_identifier sql_tokens
14 190 );
15 190 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 190 __PACKAGE__->mk_diff_accessors(qw/view_name view_info triggers replaced/);
19  
20  
21  
22 394 ▶sub diff {
Subroutine diff: called CC=13 SCAR=25.7
Pod documented
23 394 my ($class, $source, $target, $source_triggers) = @_;
24 394 ▶ $source //= {};
Condition50%1/2
$source //= {}
l!l
3940
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
25 394 ▶ $target //= {};
Condition50%1/2
$target //= {}
l!l
3940
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
26 394 ▶ $source_triggers //= {};
Condition100%2/2
$source_triggers //= {}
l!l
35044
MC/DC100%1/1
$source_triggers //= {}
$source_triggers
Truth table100%2/2
A
A= $source_triggers
Aresult
11
00
27  
28 394 my %seen;
29 my @ops;
30 394 for my $name (sort grep { !$seen{$_}++ } keys %$source, keys %$target) {
31 68 my ($old, $new) = ($source->{$name}, $target->{$name});
32  
33 next
34 if $old && $new
35 && comparable_statement($old->{sql}, 'VIEW')
36 68 ▶ eq comparable_statement($new->{sql}, 'VIEW');
Branch: 1 branch
BranchTrueFalseTotal
if $old and $new and comparable_statement($old->{"sql"}, "VIEW") eq comparable_statement($new->{"sql"}, "VIEW") 36 32 68
Condition100%6/6
$old and $new and comparable_statement($old->{"sql"}, "VIEW") eq comparable_statement($new->{"sql"}, "VIEW")
!ll&&!rl&&r
92336
$old and $new
!ll&&!rl&&r
4559
MC/DC100%3/3
$old and $new and comparable_statement($old->{"sql"}, "VIEW") eq comparable_statement($new->{"sql"}, "VIEW")
$old $new comparable_statement($old->{"sql"}, "VIEW") eq comparable_statement($new->{"sql"}, "VIEW")
Truth table100%4/4
A and B and C
A= $oldB= $newC= comparable_statement($old->{"sql"}, "VIEW") eq comparable_statement($new->{"sql"}, "VIEW")
ABCresult
0XX0
10X0
1100
1111
37  
38 32 ▶ push @ops, $class->new(
Branch: 2 branches
BranchTrueFalseTotal
if $old 28 4 32
$new ? : 23 5 28
39 action => 'drop',
40 view_name => $name,
41 view_info => $old,
42 replaced => ($new ? 1 : 0),
43 ) if $old;
44  
45 32 ▶ next unless $new;
Branch: 1 branch
BranchTrueFalseTotal
unless $new 5 27 32
46  
47 # A replaced view gets back the triggers its DROP took away.
48 27 ▶ my $triggers = $old ? $source_triggers->{$name} // {} : {};
Branch: 1 branch
BranchTrueFalseTotal
$old ? : 23 4 27
Condition100%2/2
$source_triggers->{$name} // {}
l!l
122
MC/DC100%1/1
$source_triggers->{$name} // {}
$source_triggers->{$name}
Truth table100%2/2
A
A= $source_triggers->{$name}
Aresult
11
00
49 push @ops, $class->new(
50 action => 'create',
51 view_name => $name,
52 view_info => $new,
53 27 triggers => [ map { $triggers->{$_}{sql} } sort keys %$triggers ],
54 );
55 }
56 394 return @ops;
57 }
58  
59  
60 56 ▶sub statements {
Subroutine statements: called CC=7 SCAR=19.7
Pod documented
61 56 my ($self) = @_;
62  
63 56 ▶ return sprintf 'DROP VIEW %s;', quote_identifier($self->view_name)
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 29 27 56
64 if $self->action eq 'drop';
65  
66 # Without the statement there is nothing to run, and an empty statement is
67 # dropped by the DDL run without a word (karr k73).
68 27 ▶ my $sql = ($self->view_info // {})->{sql} // '';
Condition75%3/4
+($self->view_info // {})->{"sql"} // ""
l!l
243
$self->view_info // {}
l!l
270
MC/DC0%0/1
+($self->view_info // {})->{"sql"} // ""
$self->view_info
Truth table50%1/2
A
A= $self->view_info
Aresult
11
00
69 27 my @tokens = sql_tokens($sql);
70 27 ▶ Carp::croak(sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless defined create_name_index(\@tokens, "VIEW") 3 24 27
71 'Cannot create view %s: the target model holds no CREATE VIEW statement '
72 . 'for it, and a view cannot be written from its columns', $self->view_name)
73 unless defined create_name_index(\@tokens, 'VIEW');
74  
75 24 ▶ return map { /;\s*\z/ ? $_ : "$_;" } $sql, @{ $self->triggers // [] };
Branch: 1 branch
BranchTrueFalseTotal
/;\s*\z/ ? : 0 26 26
Condition50%1/2
$self->triggers // []
l!l
240
MC/DC0%0/1
$self->triggers // []
$self->triggers
Truth table50%1/2
A
A= $self->triggers
Aresult
11
00
76 }
77  
78  
79 8 ▶sub as_sql {
Subroutine as_sql: called CC=1 SCAR=0.0
Pod documented
80 8 my ($self) = @_;
81 8 return join "\n", $self->statements;
82 }
83  
84  
85 15 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
86 15 my ($self) = @_;
87 15 return sprintf '%s view: %s', $self->summary_prefix, $self->view_name;
88 }
89  
90 190 1;
91  
92 __END__
« blib/lib/DBIO/SQLite/Diff/Index.pm ↑ Summary blib/lib/DBIO/SQLite/Introspect/Indexes.pm »