| 1 | package DBIO::Informix::Diff::Column; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for Informix columns | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 26 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 26 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 26 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 26 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 26 | ▶ | use DBIO::Diff::Compare qw(changed_column_fields column_change_lossy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 26 | ▶ | use DBIO::Informix::Util qw(_quote_ident); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 26 | ▶ | use DBIO::Informix::ColumnDef qw(column_def_sql); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 26 | __PACKAGE__->mk_diff_accessors(qw(table_name column_info from_info)); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 61 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 61 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 61 | my @ops; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 61 | for my $table (sort keys %$target) { | |||||||||||||||||||||||||||
| 25 | 97 | ▶ | next unless $target_tables->{$table} && $source_tables->{$table}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$target_tables->{$table} and $source_tables->{$table}
MC/DC50%1/2
$target_tables->{$table} and $source_tables->{$table}
$target_tables->{$table}
$source_tables->{$table}
Truth table66%2/3
A and B
A= $target_tables->{$table}B= $source_tables->{$table}
| |||||||||||||||||||||||||||||
| 26 | 80 | ▶ | next if _is_view($target_tables->{$table}) || _is_view($source_tables->{$table}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
_is_view($target_tables->{$table}) or _is_view($source_tables->{$table})
MC/DC100%2/2
_is_view($target_tables->{$table}) or _is_view($source_tables->{$table})
_is_view($target_tables->{$table})
_is_view($source_tables->{$table})
Truth table100%3/3
A or B
A= _is_view($target_tables->{$table})B= _is_view($source_tables->{$table})
| |||||||||||||||||||||||||||||
| 27 | 76 | ▶ | my %src_cols = map { $_->{column_name} => $_ } @{ $source->{$table} // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$source->{$table} // []
MC/DC0%0/1
$source->{$table} // []
$source->{$table}
Truth table50%1/2
A
A= $source->{$table}
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 76 | for my $col (@{ $target->{$table} }) { | |||||||||||||||||||||||||||
| 30 | 118 | ▶ | if (my $src = $src_cols{$col->{column_name}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 112 | ▶ | if (changed_column_fields(_compare_view($src), _compare_view($col))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 8 | push @ops, $class->new( | |||||||||||||||||||||||||||
| 33 | action => 'alter', | ||||||||||||||||||||||||||||
| 34 | table_name => $table, | ||||||||||||||||||||||||||||
| 35 | column_info => $col, | ||||||||||||||||||||||||||||
| 36 | from_info => $src, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } else { | ||||||||||||||||||||||||||||
| 40 | 6 | push @ops, $class->new( | |||||||||||||||||||||||||||
| 41 | action => 'add', | ||||||||||||||||||||||||||||
| 42 | table_name => $table, | ||||||||||||||||||||||||||||
| 43 | column_info => $col, | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 61 | for my $table (sort keys %$source) { | |||||||||||||||||||||||||||
| 50 | 92 | ▶ | next unless $source_tables->{$table} && $target_tables->{$table}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$source_tables->{$table} and $target_tables->{$table}
MC/DC100%2/2
$source_tables->{$table} and $target_tables->{$table}
$source_tables->{$table}
$target_tables->{$table}
Truth table100%3/3
A and B
A= $source_tables->{$table}B= $target_tables->{$table}
| |||||||||||||||||||||||||||||
| 51 | 80 | ▶ | next if _is_view($target_tables->{$table}) || _is_view($source_tables->{$table}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
_is_view($target_tables->{$table}) or _is_view($source_tables->{$table})
MC/DC100%2/2
_is_view($target_tables->{$table}) or _is_view($source_tables->{$table})
_is_view($target_tables->{$table})
_is_view($source_tables->{$table})
Truth table100%3/3
A or B
A= _is_view($target_tables->{$table})B= _is_view($source_tables->{$table})
| |||||||||||||||||||||||||||||
| 52 | 76 | ▶ | my %tgt_cols = map { $_->{column_name} => $_ } @{ $target->{$table} // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$target->{$table} // []
MC/DC0%0/1
$target->{$table} // []
$target->{$table}
Truth table50%1/2
A
A= $target->{$table}
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 76 | for my $col (@{ $source->{$table} }) { | |||||||||||||||||||||||||||
| 55 | 116 | ▶ | unless (exists $tgt_cols{$col->{column_name}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 4 | push @ops, $class->new( | |||||||||||||||||||||||||||
| 57 | action => 'drop', | ||||||||||||||||||||||||||||
| 58 | table_name => $table, | ||||||||||||||||||||||||||||
| 59 | column_info => $col, | ||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 61 | return @ops; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # A view is created whole or dropped whole: its columns are not altered. | ||||||||||||||||||||||||||||
| 69 | 314 | ▶ | sub _is_view { ref $_[0] eq 'HASH' && ($_[0]{kind} // '') eq 'view' } | ||||||||||||||||||||||||||
|
Subroutine
_is_view: called CC=3 SCAR=11.0
Branch: 1 branch
Condition100%2/2
$_[0]{"kind"} // ""
MC/DC100%1/1
$_[0]{"kind"} // ""
$_[0]{"kind"}
Truth table100%2/2
A
A= $_[0]{"kind"}
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Build a comparison-only view of a column. Informix carries C<default_value> | ||||||||||||||||||||||||||||
| 72 | # as a SCALAR ref to signal "literal SQL, do not quote" (see | ||||||||||||||||||||||||||||
| 73 | # L<DBIO::Informix::ColumnDef>); dereference it here so L</changed_column_fields>'s | ||||||||||||||||||||||||||||
| 74 | # string compare sees the same string on both sides. The original column_info | ||||||||||||||||||||||||||||
| 75 | # (and its SCALAR ref) is left untouched for C<as_sql>. | ||||||||||||||||||||||||||||
| 76 | 224 | ▶ | sub _compare_view { | ||||||||||||||||||||||||||
|
Subroutine
_compare_view: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 224 | my ($col) = @_; | |||||||||||||||||||||||||||
| 78 | 224 | my $view = { %$col }; | |||||||||||||||||||||||||||
| 79 | 224 | ▶ | if (ref $col->{default_value} eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | $view->{default_value} = ${ $col->{default_value} }; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 224 | return $view; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 18 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 18 | my ($self) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 18 | my $col = $self->column_info; | |||||||||||||||||||||||||||
| 90 | 18 | my $table = _quote_ident($self->table_name); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 18 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | return sprintf 'ALTER TABLE %s DROP (%s);', | ||||||||||||||||||||||||||||
| 94 | 4 | $table, _quote_ident($col->{column_name}); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | my $def = column_def_sql( | ||||||||||||||||||||||||||||
| 98 | name => $col->{column_name}, | ||||||||||||||||||||||||||||
| 99 | type => $col->{data_type}, | ||||||||||||||||||||||||||||
| 100 | size => $col->{size}, | ||||||||||||||||||||||||||||
| 101 | not_null => $col->{not_null}, | ||||||||||||||||||||||||||||
| 102 | default_value => $col->{default_value}, | ||||||||||||||||||||||||||||
| 103 | is_auto_increment => $col->{is_auto_increment}, | ||||||||||||||||||||||||||||
| 104 | 14 | ); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 14 | ▶ | return sprintf 'ALTER TABLE %s MODIFY (%s);', $table, $def | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | if $self->action eq 'alter'; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 6 | return sprintf 'ALTER TABLE %s ADD (%s);', $table, $def; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 26 | my %LOSSLESS_PAIRS = map { ($_ => 1) } ( | |||||||||||||||||||||||||||
| 114 | 'smallint->integer', 'smallint->int8', 'smallint->bigint', | ||||||||||||||||||||||||||||
| 115 | 'integer->int8', 'integer->bigint', | ||||||||||||||||||||||||||||
| 116 | 'int8->bigint', 'bigint->int8', | ||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 23 | ▶ | sub lossy { | ||||||||||||||||||||||||||
|
Subroutine
lossy: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 23 | my ($self) = @_; | |||||||||||||||||||||||||||
| 121 | 23 | ▶ | return 0 unless $self->action eq 'alter'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 20 | my ($old, $new) = ($self->from_info, $self->column_info); | |||||||||||||||||||||||||||
| 123 | 20 | ▶ | return column_change_lossy($old, $new, \%LOSSLESS_PAIRS) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 4 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 129 | return sprintf '%s column: %s.%s', | ||||||||||||||||||||||||||||
| 130 | 4 | $self->summary_prefix, $self->table_name, $self->column_info->{column_name}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 26 | 1; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||