| 1 | package DBIO::Firebird::Diff::View; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for Firebird views | ||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | 11 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use DBIO::Firebird::DDL qw(_firebird_quote_ident); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | 11 | __PACKAGE__->mk_diff_accessors(qw/view_name view_source old_source replaced/); | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 6 | ▶ | sub normalize { | ||||||||||||||||||||||||||||||||
|
Subroutine
normalize: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 19 | 6 | my ($class, $source) = @_; | |||||||||||||||||||||||||||||||||
| 20 | 6 | ▶ | return '' unless defined $source; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 21 | 6 | $source =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||
| 22 | 6 | $source =~ s/^ //; | |||||||||||||||||||||||||||||||||
| 23 | 6 | $source =~ s/ \z//; | |||||||||||||||||||||||||||||||||
| 24 | 6 | $source =~ s/\s*;\z//; | |||||||||||||||||||||||||||||||||
| 25 | 6 | return $source; | |||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | 359 | ▶ | sub _is_view { ($_[0] // {})->{kind} && $_[0]{kind} eq 'view' } | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_view: called CC=3 SCAR=11.2
Branch: 1 branch
Condition50%1/2
$_[0] // {}
MC/DC0%0/1
$_[0] // {}
$_[0]
Truth table50%1/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | # The views $name reads (by quoted name; an unquoted reference reads the upper | ||||||||||||||||||||||||||||||||||
| 31 | # case name), for ordering only. | ||||||||||||||||||||||||||||||||||
| 32 | 15 | ▶ | sub _reads { | ||||||||||||||||||||||||||||||||
|
Subroutine
_reads: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||||||||
| 33 | 15 | my ($source, @others) = @_; | |||||||||||||||||||||||||||||||||
| 34 | return [ grep { | ||||||||||||||||||||||||||||||||||
| 35 | 15 | my $o = $_; | |||||||||||||||||||||||||||||||||
| 36 | 29 | ▶ | $source =~ /"\Q$o\E"/ || ($o eq uc($o) && $source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i) | ||||||||||||||||||||||||||||||||
|
Condition50%3/6
$source =~ /"\Q$o\E"/ || $o eq uc $o && $source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i
$o eq uc $o and $source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i
MC/DC33%1/3
$source =~ /"\Q$o\E"/ || $o eq uc $o && $source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i
$source =~ /"\Q$o\E"/
$o eq uc $o
$source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i
Truth table50%2/4
A || B and C
A= $source =~ /"\Q$o\E"/B= $o eq uc $oC= $source =~ /(?<![\w\$])\Q$o\E(?![\w\$])/i
| |||||||||||||||||||||||||||||||||||
| 37 | } @others ]; | ||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | 146 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 42 | 146 | my ($class, $source, $target) = @_; | |||||||||||||||||||||||||||||||||
| 43 | 146 | ▶ | $source ||= {}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source ||= {}
MC/DC0%0/1
$source ||= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||
| 44 | 146 | ▶ | $target ||= {}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target ||= {}
MC/DC0%0/1
$target ||= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 146 | my (%old, %new); | |||||||||||||||||||||||||||||||||
| 47 | 146 | for my $name (keys %$source) { | |||||||||||||||||||||||||||||||||
| 48 | 176 | ▶ | next unless _is_view($source->{$name}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 49 | 8 | ▶ | next if exists $target->{$name} && !_is_view($target->{$name}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $target->{$name} and not _is_view($target->{$name})
MC/DC100%2/2
exists $target->{$name} and not _is_view($target->{$name})
exists $target->{$name}
not _is_view($target->{$name})
Truth table100%3/3
A and not(B)
A= exists $target->{$name}B= not _is_view($target->{$name})
| |||||||||||||||||||||||||||||||||||
| 50 | 7 | $old{$name} = $source->{$name}{view_source}; | |||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||
| 52 | 146 | for my $name (keys %$target) { | |||||||||||||||||||||||||||||||||
| 53 | 175 | ▶ | next unless _is_view($target->{$name}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 54 | 11 | ▶ | next if exists $source->{$name} && !_is_view($source->{$name}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $source->{$name} and not _is_view($source->{$name})
MC/DC100%2/2
exists $source->{$name} and not _is_view($source->{$name})
exists $source->{$name}
not _is_view($source->{$name})
Truth table100%3/3
A and not(B)
A= exists $source->{$name}B= not _is_view($source->{$name})
| |||||||||||||||||||||||||||||||||||
| 55 | 10 | $new{$name} = $target->{$name}{view_source}; | |||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | 146 | my @gone = grep { !exists $new{$_} } keys %old; | |||||||||||||||||||||||||||||||||
| 59 | 146 | ▶ | my @make = grep { !exists $old{$_} || $class->normalize($old{$_}) ne $class->normalize($new{$_}) } | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
!exists $old{$_} || $class->normalize($old{$_}) ne $class->normalize($new{$_})
MC/DC100%2/2
!exists $old{$_} || $class->normalize($old{$_}) ne $class->normalize($new{$_})
!exists $old{$_}
$class->normalize($old{$_}) ne $class->normalize($new{$_})
Truth table100%3/3
not(A) || B
A= !exists $old{$_}B= $class->normalize($old{$_}) ne $class->normalize($new{$_})
| |||||||||||||||||||||||||||||||||||
| 60 | keys %new; | ||||||||||||||||||||||||||||||||||
| 61 | 146 | my %replaced = map { $_ => 1 } grep { exists $old{$_} } @make; | |||||||||||||||||||||||||||||||||
| 62 | 146 | my @unmake = (@gone, grep { $replaced{$_} } @make); | |||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | my @drops = reverse DBIO::Diff::Op::_order_by_dependency( | ||||||||||||||||||||||||||||||||||
| 65 | 146 | ▶ | sub { _reads($old{ $_[0] } // '', @unmake) }, @unmake); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$old{$_[0]} // ""
MC/DC0%0/1
$old{$_[0]} // ""
$old{$_[0]}
Truth table50%1/2
A
A= $old{$_[0]}
| |||||||||||||||||||||||||||||||||||
| 66 | my @creates = DBIO::Diff::Op::_order_by_dependency( | ||||||||||||||||||||||||||||||||||
| 67 | 146 | ▶ | sub { _reads($new{ $_[0] } // '', @make) }, @make); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$new{$_[0]} // ""
MC/DC100%1/1
$new{$_[0]} // ""
$new{$_[0]}
Truth table100%2/2
A
A= $new{$_[0]}
| |||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | return ( | ||||||||||||||||||||||||||||||||||
| 70 | drops => [ map { $class->new( | ||||||||||||||||||||||||||||||||||
| 71 | action => 'drop', view_name => $_, view_source => $old{$_}, | ||||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | replaced => ($replaced{$_} ? 1 : 0), | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 73 | ) } @drops ], | ||||||||||||||||||||||||||||||||||
| 74 | 146 | creates => [ map { $class->new( | |||||||||||||||||||||||||||||||||
| 75 | action => (exists $old{$_} ? 'change' : 'create'), | ||||||||||||||||||||||||||||||||||
| 76 | view_name => $_, | ||||||||||||||||||||||||||||||||||
| 77 | view_source => $new{$_}, | ||||||||||||||||||||||||||||||||||
| 78 | 9 | ▶ | old_source => $old{$_}, | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | ) } @creates ], | ||||||||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | 9 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 85 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 86 | 9 | my $name = _firebird_quote_ident($self->view_name); | |||||||||||||||||||||||||||||||||
| 87 | 9 | ▶ | return sprintf 'DROP VIEW %s;', $name if $self->action eq 'drop'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 88 | 5 | my $def = $self->view_source; | |||||||||||||||||||||||||||||||||
| 89 | 5 | ▶ | DBIO::Exception->throw(sprintf 'view %s: the model holds no definition (view_source) to write it from', $name) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $def and length $def
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
| |||||||||||||||||||||||||||||||||||
| 90 | unless defined $def && length $def; | ||||||||||||||||||||||||||||||||||
| 91 | 4 | $def =~ s/\s*;\s*\z//; | |||||||||||||||||||||||||||||||||
| 92 | 4 | return sprintf 'CREATE VIEW %s AS %s;', $name, $def; | |||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | 6 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 97 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 98 | return sprintf '~ view: %s (dropped, created again after the tables)', $self->view_name | ||||||||||||||||||||||||||||||||||
| 99 | 6 | ▶ | if $self->action eq 'drop' && $self->{replaced}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->action eq "drop" and $self->{"replaced"}
MC/DC100%2/2
$self->action eq "drop" and $self->{"replaced"}
$self->action eq "drop"
$self->{"replaced"}
Truth table100%3/3
A and B
A= $self->action eq "drop"B= $self->{"replaced"}
| |||||||||||||||||||||||||||||||||||
| 100 | 4 | return sprintf '%s view: %s', $self->summary_prefix, $self->view_name; | |||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | 11 | 1; | |||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||||||||