| 1 | package DBIO::DB2::Diff::View; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for DB2 views | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 45 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 45 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 45 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 45 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 45 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 45 | ▶ | use DBIO::DB2::DDL qw(_db2_quote_ident); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 45 | __PACKAGE__->mk_diff_accessors(qw/view_name view_info replaced/); | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 691 | ▶ | sub is_view { return ((($_[0] // {})->{kind}) // '') eq 'view' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_view: called CC=3 SCAR=11.0
Pod
documented
Condition100%4/4
+($_[0] // {})->{"kind"} // ""
$_[0] // {}
MC/DC100%1/1
+($_[0] // {})->{"kind"} // ""
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 52 | ▶ | sub _comparable { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_comparable: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 52 | my ($def) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 52 | ▶ | return undef unless defined $def; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 49 | $def =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 49 | $def =~ s/\A //; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 49 | $def =~ s/ \z//; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 49 | return $def; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 167 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=24 SCAR=31.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 167 | my ($class, $source, $target, $schema) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 167 | ▶ | $source //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source //= {}
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 167 | ▶ | $target //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target //= {}
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # The schema the operations run in is the live one, the source model's: its | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # entries carry it (Introspect::Tables). A qualified read in a definition is | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # judged against it (karr k55); a model without an entry has no schema to give. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 167 | ▶ | ($schema) = grep { defined } map { $_->{schema} } map { $source->{$_} } sort keys %$source | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | unless defined $schema; | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 167 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 167 | my (%drop, %create, %replaced); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 167 | for my $name (sort grep { !$seen{$_}++ } keys %$source, keys %$target) { | |||||||||||||||||||||||||||||||||||||||||
| 45 | 225 | ▶ | my $old = is_view($source->{$name}) ? $source->{$name} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 225 | ▶ | my $new = is_view($target->{$name}) ? $target->{$name} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 225 | ▶ | next unless $old || $new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$old or $new
MC/DC100%2/2
$old or $new
$old
$new
Truth table100%3/3
A or B
A= $oldB= $new
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 47 | ▶ | if ($old && $new) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$old and $new
MC/DC100%2/2
$old and $new
$old
$new
Truth table100%3/3
A and B
A= $oldB= $new
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 26 | my ($was, $will) = map { _comparable($_->{view_definition}) } $old, $new; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 26 | ▶ | next if !defined $was || !defined $will || $was eq $will; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not defined $was && defined $will or $was eq $will
defined $was && defined $will
MC/DC66%2/3
not defined $was && defined $will or $was eq $will
defined $was
defined $will
$was eq $will
Truth table75%3/4
not(A && B) or C
A= defined $wasB= defined $willC= $was eq $will
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 27 | ▶ | $drop{$name} = $old if $old; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 27 | ▶ | $create{$name} = $new if $new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 27 | ▶ | $replaced{$name} = 1 if $old && $new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$old and $new
MC/DC100%2/2
$old and $new
$old
$new
Truth table100%3/3
A and B
A= $oldB= $new
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # A view that reads a view being replaced is replaced with it (karr k54), | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # transitively, although its own definition is the same. An unchanged view | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # has the same reads in both models. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 20 | my %stay = map { $_ => $target->{$_} } grep { | |||||||||||||||||||||||||||||||||||||||||
| 63 | 167 | ▶ | is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_} | ||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_}
is_view($source->{$_}) and is_view($target->{$_})
MC/DC66%2/3
is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_}
is_view($source->{$_})
is_view($target->{$_})
!$replaced{$_}
Truth table75%3/4
A and B && not(C)
A= is_view($source->{$_})B= is_view($target->{$_})C= !$replaced{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | } keys %$target; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 167 | my @grow = keys %replaced; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 167 | ▶ | while (defined(my $changed = shift @grow)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 13 | for my $name (sort keys %stay) { | |||||||||||||||||||||||||||||||||||||||||
| 68 | 10 | next unless grep { $_ eq $changed } DBIO::DB2::DDL::view_dependencies( | |||||||||||||||||||||||||||||||||||||||||
| 69 | 22 | ▶ | [ keys %stay, keys %replaced ], $name, $stay{$name}{view_definition}, $schema); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | $drop{$name} = $source->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 7 | $create{$name} = $target->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 7 | $replaced{$name} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 7 | delete $stay{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 7 | push @grow, $name; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | # the order is worked out for the whole set before an op is made: a cycle | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # throws and leaves nothing half done | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 167 | my @drop_order = reverse DBIO::DB2::DDL::order_views({ map { $_ => $drop{$_}{view_definition} } keys %drop }, $schema); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 167 | my @create_order = DBIO::DB2::DDL::order_views({ map { $_ => $create{$_}{view_definition} } keys %create }, $schema); | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 84 | (map { $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 85 | action => 'drop', | ||||||||||||||||||||||||||||||||||||||||||
| 86 | view_name => $_, | ||||||||||||||||||||||||||||||||||||||||||
| 87 | view_info => $drop{$_}, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 18 | ▶ | replaced => ($replaced{$_} ? 1 : 0), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | ) } @drop_order), | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 164 | (map { $class->new( | |||||||||||||||||||||||||||||||||||||||||
| 91 | action => 'create', | ||||||||||||||||||||||||||||||||||||||||||
| 92 | view_name => $_, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 23 | view_info => $create{$_}, | |||||||||||||||||||||||||||||||||||||||||
| 94 | ) } @create_order), | ||||||||||||||||||||||||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 30 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 30 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 30 | ▶ | return sprintf 'DROP VIEW %s;', _db2_quote_ident($self->view_name) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | if $self->action eq 'drop'; | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 18 | ▶ | my $def = ($self->view_info // {})->{view_definition}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->view_info // {}
MC/DC0%0/1
$self->view_info // {}
$self->view_info
Truth table50%1/2
A
A= $self->view_info
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 18 | ▶ | Carp::croak(sprintf | ||||||||||||||||||||||||||||||||||||||||
|
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
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 'Cannot create view %s: the target model holds no view definition for it, ' | ||||||||||||||||||||||||||||||||||||||||||
| 108 | . 'and a view cannot be written from its columns', $self->view_name) | ||||||||||||||||||||||||||||||||||||||||||
| 109 | unless defined $def && length $def; | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 17 | return sprintf 'CREATE VIEW %s %s;', _db2_quote_ident($self->view_name), $def; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 11 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 11 | return sprintf '%s view: %s', $self->summary_prefix, $self->view_name; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 45 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||||||||||||||||