| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 35 | ▶ | if ($text =~ /\GCREATE\s+(?:OR\s+ALTER\s+)?VIEW\s+/gci) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 34 | ▶ | while (pos($text) < length $text) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 81 | ▶ | if ($text =~ /\GAS\b/gci) { last } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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'
$tables->{$_}{"kind"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 188 | ▶ | } keys %{ $tables // {} } }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$tables // {}
MC/DC0%0/1
$tables // {}
$tables
Truth table50%1/2
A
A= $tables
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
defined $new and defined $old and $new eq $old
defined $new and defined $old
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
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | ▶ | next unless defined $new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | $action = 'alter'; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 12 | ▶ | DBIO::Exception->throw("DBIO::MSSQL: the view '$name' is declared without a " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$state{$name} // 0
MC/DC100%1/1
$state{$name} // 0
$state{$name}
Truth table100%2/2
A
A= $state{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$state{$name} // 0
MC/DC100%1/1
$state{$name} // 0
$state{$name}
Truth table100%2/2
A
A= $state{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 17 | $state{$name} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 17 | ▶ | for my $dep (sort @{ $views->{$name}{depends_on} // [] }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$views->{$name}{"depends_on"} // []
MC/DC100%1/1
$views->{$name}{"depends_on"} // []
$views->{$name}{"depends_on"}
Truth table100%2/2
A
A= $views->{$name}{"depends_on"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | ▶ | $visit->($dep, @path, $name) if $op{$dep}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||