Informix/Diff/UniqueConstraint.pm

filter: Statementstmt 100% 68 / 68 Branchbran 71.4% 10 / 14 Conditioncond 54.1% 13 / 24 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 82.0% 114 / 139 CC 20 SCAR 16.2
CC
20
Cov
86%
diff
10.4
_explicit_name
5.5
as_sql
4.0
CRAP 5.1
« Informix/Introspect.pm ↑ Summary Informix/Introspect/ForeignKeys.pm »
1 package DBIO::Informix::Diff::UniqueConstraint;
2 # ABSTRACT: Diff operations for Informix unique constraints
3  
4 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 24 our $VERSION = '0.900007';
8  
9 24 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 24 ▶use DBIO::Informix::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 24 __PACKAGE__->mk_diff_accessors(qw(table_name constraint_name columns replaced));
14  
15  
16  
17 41 ▶sub _key { join ',', @{ $_[0]{columns} // [] } }
Subroutine _key: called CC=2 SCAR=7.2
Condition50%1/2
$_[0]{"columns"} // []
l!l
410
MC/DC0%0/1
$_[0]{"columns"} // []
$_[0]{"columns"}
Truth table50%1/2
A
A= $_[0]{"columns"}
Aresult
11
00
18  
19 150 ▶sub _keyed {
Subroutine _keyed: called CC=4 SCAR=13.9
20 150 my ($uniques) = @_;
21 150 my %out;
22 150 ▶ for my $table (keys %{ $uniques // {} }) {
Condition100%2/2
$uniques // {}
l!l
13218
MC/DC100%1/1
$uniques // {}
$uniques
Truth table100%2/2
A
A= $uniques
Aresult
11
00
23 38 ▶ $out{$table} = [ map { +{ %$_, _content_key => _key($_) } } @{ $uniques->{$table} // [] } ];
Condition50%1/2
$uniques->{$table} // []
l!l
380
MC/DC0%0/1
$uniques->{$table} // []
$uniques->{$table}
Truth table50%1/2
A
A= $uniques->{$table}
Aresult
11
00
24 }
25 150 return \%out;
26 }
27  
28 # the name written on ADD: one the model states, never one the server assigned
29 9 ▶sub _explicit_name {
Subroutine _explicit_name: called CC=4 SCAR=17.1
30 9 my ($name) = @_;
31 9 ▶ return undef if !defined $name || !length $name || $name =~ /\Au\d+_\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name && length $name or $name =~ /\Au\d+_\d+\z/ 0 9 9
Condition33%2/6
not defined $name && length $name or $name =~ /\Au\d+_\d+\z/
l!l&&r!l&&!r
009
defined $name && length $name
!ll&&!rl&&r
009
MC/DC0%0/3
not defined $name && length $name or $name =~ /\Au\d+_\d+\z/
defined $name length $name $name =~ /\Au\d+_\d+\z/
Truth table25%1/4
not(A && B) or C
A= defined $nameB= length $nameC= $name =~ /\Au\d+_\d+\z/
ABCresult
0XX1
10X1
1111
1100
32 9 return $name;
33 }
34  
35 9 ▶sub _add {
Subroutine _add: called CC=1 SCAR=0.0
36 9 my ($class, $table, $uc) = @_;
37 return $class->new(action => 'add', table_name => $table,
38 9 constraint_name => _explicit_name($uc->{constraint_name}), columns => $uc->{columns});
39 }
40  
41 14 ▶sub _drop {
Subroutine _drop: called CC=1 SCAR=0.0
42 14 my ($class, $table, $uc) = @_;
43 return $class->new(action => 'drop', table_name => $table,
44 14 constraint_name => $uc->{constraint_name}, columns => $uc->{columns});
45 }
46  
47  
48 50 ▶sub diff {
Subroutine diff: called CC=10 SCAR=23.4
Pod documented
49 50 my ($class, $source, $target, $source_tables, $target_tables) = @_;
50  
51 my @ops = $class->diff_nested(_keyed($source), _keyed($target),
52 index_by => '_content_key',
53 scope => 'both',
54 source_tables => $source_tables,
55 target_tables => $target_tables,
56 8 ▶ on_new => sub { $class->_add($_[0], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 14 ▶ on_gone => sub { $class->_drop($_[0], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 50 );
59  
60 # Tables only in the target: their constraints are added after the CREATE TABLEs.
61 50 my $keyed = _keyed($target);
62 50 for my $table (sort keys %$keyed) {
63 12 ▶ next if !$target_tables || !exists $target_tables->{$table};
Branch: 1 branch
BranchTrueFalseTotal
unless $target_tables and exists $target_tables->{$table} 0 12 12
Condition33%1/3
$target_tables and exists $target_tables->{$table}
!ll&&!rl&&r
0012
MC/DC0%0/2
$target_tables and exists $target_tables->{$table}
$target_tables exists $target_tables->{$table}
Truth table33%1/3
A and B
A= $target_tablesB= exists $target_tables->{$table}
ABresult
0X0
100
111
64 12 ▶ next if $source_tables && exists $source_tables->{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $source_tables and exists $source_tables->{$table} 11 1 12
Condition66%2/3
$source_tables and exists $source_tables->{$table}
!ll&&!rl&&r
0111
MC/DC50%1/2
$source_tables and exists $source_tables->{$table}
$source_tables exists $source_tables->{$table}
Truth table66%2/3
A and B
A= $source_tablesB= exists $source_tables->{$table}
ABresult
0X0
100
111
65 1 push @ops, map { $class->_add($table, $_) } @{ $keyed->{$table} };
66 }
67  
68 9 my %back = map { join("\0", $_->table_name, $_->constraint_name) => 1 }
69 50 ▶ grep { $_->action eq 'add' && defined $_->constraint_name } @ops;
Condition66%2/3
$_->action eq 'add' && defined $_->constraint_name
!ll&&!rl&&r
1409
MC/DC50%1/2
$_->action eq 'add' && defined $_->constraint_name
$_->action eq 'add' defined $_->constraint_name
Truth table66%2/3
A && B
A= $_->action eq 'add'B= defined $_->constraint_name
ABresult
0X0
100
111
70 50 for my $op (grep { $_->action eq 'drop' } @ops) {
71 14 my $name = $op->constraint_name;
72 14 ▶ $op->replaced(1) if defined $name && $back{ join "\0", $op->table_name, $name };
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and $back{join "\0", $op->table_name, $name} 5 9 14
Condition66%2/3
defined $name and $back{join "\0", $op->table_name, $name}
!ll&&!rl&&r
095
MC/DC50%1/2
defined $name and $back{join "\0", $op->table_name, $name}
defined $name $back{join "\0", $op->table_name, $name}
Truth table66%2/3
A and B
A= defined $nameB= $back{join "\0", $op->table_name, $name}
ABresult
0X0
100
111
73 }
74  
75 50 return @ops;
76 }
77  
78  
79 49 ▶sub dropped_keys {
Subroutine dropped_keys: called CC=1 SCAR=0.0
Pod documented
80 49 my ($class, @ops) = @_;
81 17 return { map { join("\0", $_->table_name, sort @{ $_->columns }) => 1 }
82 49 grep { $_->action eq 'drop' } @ops };
83 }
84  
85  
86 18 ▶sub as_sql {
Subroutine as_sql: called CC=4 SCAR=14.0
Pod documented
87 18 my ($self) = @_;
88 18 my $tbl = _quote_ident($self->table_name);
89  
90 18 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 10 8 18
91 10 my $name = $self->constraint_name;
92 10 ▶ die sprintf "Informix unique constraint on table %s cannot be dropped: the model holds no name for it\n",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 10 10
93 $self->table_name unless defined $name;
94 10 return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', $tbl, _quote_ident($name);
95 }
96  
97 return sprintf 'ALTER TABLE %s ADD CONSTRAINT UNIQUE (%s)%s;', $tbl,
98 8 ▶ join(', ', map { _quote_ident($_) } @{ $self->columns }),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->constraint_name ? : 8 0 8
99 (defined $self->constraint_name ? ' CONSTRAINT ' . _quote_ident($self->constraint_name) : '');
100 }
101  
102  
103 3 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
104 3 my ($self) = @_;
105 return sprintf '%s unique: (%s) on %s', $self->summary_prefix,
106 3 join(', ', @{ $self->columns }), $self->table_name;
107 }
108  
109 24 1;
110  
111 __END__
« Informix/Introspect.pm ↑ Summary Informix/Introspect/ForeignKeys.pm »