| 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 //= {}
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 394 | ▶ | $target //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target //= {}
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 394 | ▶ | $source_triggers //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$source_triggers //= {}
MC/DC100%1/1
$source_triggers //= {}
$source_triggers
Truth table100%2/2
A
A= $source_triggers
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$old and $new and comparable_statement($old->{"sql"}, "VIEW") eq comparable_statement($new->{"sql"}, "VIEW")
$old and $new
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 32 | ▶ | push @ops, $class->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | # A replaced view gets back the triggers its DROP took away. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 27 | ▶ | my $triggers = $old ? $source_triggers->{$name} // {} : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$source_triggers->{$name} // {}
MC/DC100%1/1
$source_triggers->{$name} // {}
$source_triggers->{$name}
Truth table100%2/2
A
A= $source_triggers->{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // ""
$self->view_info // {}
MC/DC0%0/1
+($self->view_info // {})->{"sql"} // ""
$self->view_info
Truth table50%1/2
A
A= $self->view_info
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 27 | my @tokens = sql_tokens($sql); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 27 | ▶ | Carp::croak(sprintf | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$self->triggers // []
MC/DC0%0/1
$self->triggers // []
$self->triggers
Truth table50%1/2
A
A= $self->triggers
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||