| 1 | package DBIO::DuckDB::Diff::View; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for DuckDB views | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 8 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use DBIO::DuckDB::DDL::Emit qw(quote_ident); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 8 | __PACKAGE__->mk_diff_accessors(qw/view_name view_info replaced/); | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub _comparable { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_comparable: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | my ($def) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | $def //= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$def //= ""
MC/DC0%0/1
$def //= ""
$def
Truth table0%0/2
A
A= $def
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | $def =~ s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | $def =~ s/[\s;]+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | return $def; | |||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # what is compared: the query of the statement, not the spelling of the name | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # it was created under (a database deployed before every name was quoted holds | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # CREATE VIEW v, the target CREATE VIEW "v") | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub _body { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_body: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my ($def) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | $def = _comparable($def); | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | $def =~ s/\A CREATE \s+ (?: OR \s+ REPLACE \s+ )? (?: (?:LOCAL|GLOBAL)? \s* TEMP(?:ORARY)? \s+ )? | |||||||||||||||||||||||||||||||||||||||||
| 31 | VIEW \s+ (?: IF \s+ NOT \s+ EXISTS \s+ )? | ||||||||||||||||||||||||||||||||||||||||||
| 32 | (?: "(?:[^"]|"")*" | [\w\$.]+ ) \s+ //xi; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | return $def; | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=10 SCAR=38.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | my ($class, $source, $target) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | $source //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source //= {}
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | $target //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target //= {}
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 42 | my @ops; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | for my $name (sort grep { !$seen{$_}++ } keys %$source, keys %$target) { | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my ($old, $new) = ($source->{$name}, $target->{$name}); | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | next if $old && $new | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | && _body($old->{view_definition}) eq _body($new->{view_definition}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$old and $new and _body($old->{"view_definition"}) eq _body($new->{"view_definition"})
$old and $new
MC/DC0%0/3
$old and $new and _body($old->{"view_definition"}) eq _body($new->{"view_definition"})
$old
$new
_body($old->{"view_definition"}) eq _body($new->{"view_definition"})
Truth table0%0/4
A and B and C
A= $oldB= $newC= _body($old->{"view_definition"}) eq _body($new->{"view_definition"})
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | push @ops, $class->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | action => 'drop', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | view_name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | view_info => $old, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | replaced => ($new ? 1 : 0), | ||||||||||||||||||||||||||||||||||||||||||
| 54 | ) if $old; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | push @ops, $class->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | action => 'create', | ||||||||||||||||||||||||||||||||||||||||||
| 58 | view_name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | view_info => $new, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | ) if $new; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | return @ops; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | return sprintf 'DROP VIEW %s;', quote_ident($self->view_name) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | if $self->action eq 'drop'; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | my $def = _comparable(($self->view_info // {})->{view_definition}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->view_info // {}
MC/DC0%0/1
$self->view_info // {}
$self->view_info
Truth table0%0/2
A
A= $self->view_info
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | die sprintf "Cannot create view %s: the target model holds no definition " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | . "for it, and a view cannot be written from its columns\n", $self->view_name | ||||||||||||||||||||||||||||||||||||||||||
| 75 | unless length $def; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | return "$def;"; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | return sprintf '%s view: %s', $self->summary_prefix, $self->view_name; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||||||||||||||||