| 1 | package DBIO::SQLite::Diff::Column; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for SQLite columns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 192 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 192 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 192 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 192 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 192 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 192 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 192 | ▶ | use DBIO::Diff::Compare qw(changed_column_fields column_change_lossy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 192 | ▶ | use DBIO::SQLite::Util qw( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | create_table_name_index default_value_sql quote_identifier sql_tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | token_identifier type_affinity type_with_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 192 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 192 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # The groups of type names that share one affinity. Each name is the lower | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # case data_type; a pair is lossless when both names are in one group | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # (t/74-diff-column-lossy.t checks the groups against type_affinity and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # pairs against the rows SQLite really copies). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 192 | my %AFFINITY_GROUP = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | INTEGER => [ qw(integer int bigint smallint tinyint mediumint int2 int8) ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | TEXT => [ qw(text varchar char character nchar nvarchar clob) ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | REAL => [ 'real', 'double', 'double precision', 'float' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | NUMERIC => [ qw(numeric decimal boolean date datetime) ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | BLOB => [ 'blob', '' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 192 | my %LOSSLESS_PAIRS; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 192 | for my $names (values %AFFINITY_GROUP) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 960 | for my $from (@$names) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 4992 | for my $to (@$names) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 30336 | ▶ | $LOSSLESS_PAIRS{"$from->$to"} = 1 unless $from eq $to; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | sub lossless_type_pairs { return { %LOSSLESS_PAIRS } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lossless_type_pairs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 91 | ▶ | sub lossy { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lossy: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 91 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 91 | ▶ | return 0 unless $self->action eq 'alter'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 56 | ▶ | return column_change_lossy($self->old_info, $self->new_info, \%LOSSLESS_PAIRS) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 192 | __PACKAGE__->mk_diff_accessors(qw/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | table_name column_name old_info new_info definition needs_rebuild | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | target_described | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | /); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # What a CREATE TABLE statement declares, as far as an added column needs it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # and as far as no PRAGMA reports it: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # columns => { $name => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # sql => 'TYPE and constraints', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # keyed => 0|1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # checks => [ 'qty >= 0', ... ], # its CHECK clauses, sorted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # collate => 'nocase' | undef, # its COLLATE, lower case | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # generated => 'a + b VIRTUAL' | undef, # its generated clause | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # constrained => { lc $name => 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # checks => [ 'low <= high', ... ] # the table's CHECK constraints | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # keyed says the column's own definition carries PRIMARY KEY or UNIQUE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # constrained holds every name a table constraint mentions, short of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # column list a FOREIGN KEY points at. Returns nothing for a statement it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # cannot follow -- the caller rebuilds the table then, it does not guess. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # checks, collate and generated are what DBIO::SQLite::Diff compares on a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # table both sides have (karr k72). An expression is its tokens joined by one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # space, without whitespace and comments and without the parentheses around | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # it: what is written is compared, not what it means. The keywords that frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # a clause -- CHECK, COLLATE, GENERATED ALWAYS AS, STORED, VIRTUAL -- are read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # in any letter case, and so is the name of a collation, which SQLite does not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # tell apart by case. A constraint's name is not kept. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # The statement is read with the driver's one lexer (DBIO::SQLite::Util's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # sql_tokens), which tells a quoted name from a word. So it does not matter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # how the statement quotes its names: bare, as a database deployed by an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # earlier version has them, or all in double quotes, as DBIO::SQLite::DDL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # writes them now -- a column named "unique" or "check" included. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # The parenthesised group that begins at or behind $tokens->[$from], as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # ($text, $index_of_its_closing_parenthesis). An opening parenthesis carries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # the depth of what is inside it and the closing one the depth outside, so the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # group ends at the first ")" one level up. Nothing when there is no group. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 88 | ▶ | sub _group { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_group: called CC=11 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 88 | my ($tokens, $from) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 88 | ▶ | $from++ while $tokens->[$from] && $tokens->[$from][0] eq 'skip'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tokens->[$from] and $tokens->[$from][0] eq "skip"
MC/DC50%1/2
$tokens->[$from] and $tokens->[$from][0] eq "skip"
$tokens->[$from]
$tokens->[$from][0] eq "skip"
Truth table66%2/3
A and B
A= $tokens->[$from]B= $tokens->[$from][0] eq "skip"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 88 | my $open = $tokens->[$from]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 88 | ▶ | return unless $open && $open->[0] eq 'punct' && $open->[1] eq '('; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$open and $open->[0] eq "punct" and $open->[1] eq "("
$open and $open->[0] eq "punct"
MC/DC0%0/3
$open and $open->[0] eq "punct" and $open->[1] eq "("
$open
$open->[0] eq "punct"
$open->[1] eq "("
Truth table25%1/4
A and B and C
A= $openB= $open->[0] eq "punct"C= $open->[1] eq "("
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 88 | my @inner; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 88 | for my $at ($from + 1 .. $#$tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 543 | my ($kind, $text, $depth) = @{ $tokens->[$at] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 543 | ▶ | return (join(' ', @inner), $at) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$kind eq "punct" and $text eq ")" and $depth == $open->[2] - 1
$kind eq "punct" and $text eq ")"
MC/DC100%3/3
$kind eq "punct" and $text eq ")" and $depth == $open->[2] - 1
$kind eq "punct"
$text eq ")"
$depth == $open->[2] - 1
Truth table100%4/4
A and B and C
A= $kind eq "punct"B= $text eq ")"C= $depth == $open->[2] - 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | if $kind eq 'punct' && $text eq ')' && $depth == $open->[2] - 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 455 | ▶ | push @inner, $text unless $kind eq 'skip'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # The clauses of one column definition that no PRAGMA reports: the tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # behind the column's name, read at the level of the definition itself -- a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # COLLATE inside a CHECK is part of that CHECK. Returns nothing for a clause | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # it cannot follow. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 2738 | ▶ | sub _column_clauses { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_column_clauses: called CC=18 SCAR=29.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 2738 | my ($tokens) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 2738 | my (%checks, $collate, $generated, $nonconstant); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 2738 | for my $at (0 .. $#$tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 16386 | my ($kind, $text, $depth) = @{ $tokens->[$at] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 16386 | ▶ | next unless $kind eq 'word' && $depth == 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$kind eq "word" and $depth == 1
MC/DC100%2/2
$kind eq "word" and $depth == 1
$kind eq "word"
$depth == 1
Truth table100%3/3
A and B
A= $kind eq "word"B= $depth == 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 7185 | my $word = uc $text; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 7185 | ▶ | if ($word eq 'DEFAULT') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 164 | ▶ | $nonconstant = 1 if _nonconstant_default($tokens, $at + 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 7185 | ▶ | if ($word eq 'CHECK') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 38 | my ($expr) = _group($tokens, $at + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 38 | ▶ | return unless defined $expr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 38 | $checks{$expr} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | elsif ($word eq 'COLLATE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 18 | my ($name) = grep { $_->[0] ne 'skip' } @$tokens[ $at + 1 .. $#$tokens ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 18 | ▶ | return unless $name && ($name->[0] eq 'word' || $name->[0] eq 'quoted'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$name and $name->[0] eq "word" || $name->[0] eq "quoted"
$name->[0] eq "word" or $name->[0] eq "quoted"
MC/DC0%0/3
$name and $name->[0] eq "word" || $name->[0] eq "quoted"
$name
$name->[0] eq "word"
$name->[0] eq "quoted"
Truth table25%1/4
A and B or C
A= $nameB= $name->[0] eq "word"C= $name->[0] eq "quoted"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 18 | $collate = lc token_identifier(@$name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | elsif ($word eq 'AS') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 20 | my ($expr, $end) = _group($tokens, $at + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 20 | ▶ | return unless defined $expr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 20 | my ($next) = grep { $_->[0] ne 'skip' } @$tokens[ $end + 1 .. $#$tokens ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 20 | ▶ | my $stored = $next && $next->[0] eq 'word' && uc $next->[1] eq 'STORED'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
$next && $next->[0] eq 'word' && uc $next->[1] eq 'STORED'
$next and $next->[0] eq "word"
MC/DC33%1/3
$next && $next->[0] eq 'word' && uc $next->[1] eq 'STORED'
$next
$next->[0] eq "word"
uc $next->[1] eq 'STORED'
Truth table50%2/4
A and B && C
A= $nextB= $next->[0] eq "word"C= uc $next->[1] eq 'STORED'
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 20 | ▶ | $generated = $expr . ($stored ? ' STORED' : ' VIRTUAL'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 2738 | ▶ | checks => [ sort keys %checks ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | collate => $collate, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | generated => $generated, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | nonconstant => $nonconstant ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # Whether the DEFAULT whose value begins at or behind $tokens->[$from] is one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # SQLite refuses in an ADD COLUMN for a table that holds rows (karr k65): any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # parenthesised expression, and CURRENT_TIME, CURRENT_DATE, CURRENT_TIMESTAMP. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # A literal is not -- a number, a string, NULL, TRUE, FALSE, a signed number -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # and neither is such a literal in parentheses, which SQLite takes as constant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # (`DEFAULT (NULL)`). Anything else in the parentheses counts as an expression. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 164 | ▶ | sub _nonconstant_default { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_nonconstant_default: called CC=16 SCAR=28.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 164 | my ($tokens, $from) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 164 | my @rest = grep { $_->[0] ne 'skip' } @$tokens[ $from .. $#$tokens ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 164 | ▶ | my $first = $rest[0] or return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 164 | ▶ | return 1 if $first->[0] eq 'word' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$first->[0] eq "word" and $first->[1] =~ /\ACURRENT_(?:TIME|DATE|TIMESTAMP)\z/i
MC/DC100%2/2
$first->[0] eq "word" and $first->[1] =~ /\ACURRENT_(?:TIME|DATE|TIMESTAMP)\z/i
$first->[0] eq "word"
$first->[1] =~ /\ACURRENT_(?:TIME|DATE|TIMESTAMP)\z/i
Truth table100%3/3
A and B
A= $first->[0] eq "word"B= $first->[1] =~ /\ACURRENT_(?:TIME|DATE|TIMESTAMP)\z/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | && $first->[1] =~ /\ACURRENT_(?:TIME|DATE|TIMESTAMP)\z/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 141 | ▶ | return 0 unless $first->[0] eq 'punct' && $first->[1] eq '('; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$first->[0] eq "punct" and $first->[1] eq "("
MC/DC50%1/2
$first->[0] eq "punct" and $first->[1] eq "("
$first->[0] eq "punct"
$first->[1] eq "("
Truth table66%2/3
A and B
A= $first->[0] eq "punct"B= $first->[1] eq "("
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 17 | my ($expr, $end) = _group($tokens, $from); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 17 | ▶ | return 1 unless defined $expr; # cannot follow it: treat as an expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 17 | my $open = $from; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 17 | ▶ | $open++ while $tokens->[$open][0] eq 'skip'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 17 | my @inner = grep { $_->[0] ne 'skip' } @$tokens[ $open + 1 .. $end - 1 ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 17 | ▶ | shift @inner if @inner == 2 && $inner[0][0] eq 'word' && $inner[0][1] =~ /\A[-+]\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
@inner == 2 and $inner[0][0] eq "word" and $inner[0][1] =~ /\A[-+]\z/
@inner == 2 and $inner[0][0] eq "word"
MC/DC33%1/3
@inner == 2 and $inner[0][0] eq "word" and $inner[0][1] =~ /\A[-+]\z/
@inner == 2
$inner[0][0] eq "word"
$inner[0][1] =~ /\A[-+]\z/
Truth table50%2/4
A and B and C
A= @inner == 2B= $inner[0][0] eq "word"C= $inner[0][1] =~ /\A[-+]\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 17 | ▶ | return 1 unless @inner == 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 6 | my ($kind, $text) = @{ $inner[0] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 6 | ▶ | return 0 if $kind eq 'quoted' && $text =~ /\A'/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$kind eq "quoted" and $text =~ /\A'/
MC/DC50%1/2
$kind eq "quoted" and $text =~ /\A'/
$kind eq "quoted"
$text =~ /\A'/
Truth table66%2/3
A and B
A= $kind eq "quoted"B= $text =~ /\A'/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 4 | ▶ | return 0 if $kind eq 'word' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$kind eq "word" and $text =~ /\A(?:NULL|TRUE|FALSE|[0-9][0-9a-z_.]*|\.[0-9][0-9a-z_.]*)\z/i
MC/DC0%0/2
$kind eq "word" and $text =~ /\A(?:NULL|TRUE|FALSE|[0-9][0-9a-z_.]*|\.[0-9][0-9a-z_.]*)\z/i
$kind eq "word"
$text =~ /\A(?:NULL|TRUE|FALSE|[0-9][0-9a-z_.]*|\.[0-9][0-9a-z_.]*)\z/i
Truth table33%1/3
A and B
A= $kind eq "word"B= $text =~ /\A(?:NULL|TRUE|FALSE|[0-9][0-9a-z_.]*|\.[0-9][0-9a-z_.]*)\z/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | && $text =~ /\A(?:NULL|TRUE|FALSE|[0-9][0-9a-z_.]*|\.[0-9][0-9a-z_.]*)\z/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 1074 | ▶ | sub _declarations { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_declarations: called CC=38 SCAR=36.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 1074 | my ($sql) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # CREATE TABLE name ( -- the list starts behind the name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 1074 | my @tokens = sql_tokens($sql); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 1074 | my $name_at = create_table_name_index(\@tokens); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 1074 | ▶ | return unless defined $name_at; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 1074 | splice @tokens, 0, $name_at + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 1074 | ▶ | shift @tokens while @tokens && $tokens[0][0] eq 'skip'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@tokens and $tokens[0][0] eq "skip"
MC/DC50%1/2
@tokens and $tokens[0][0] eq "skip"
@tokens
$tokens[0][0] eq "skip"
Truth table66%2/3
A and B
A= @tokensB= $tokens[0][0] eq "skip"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 1074 | my $open = shift @tokens; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 1074 | ▶ | return unless $open && $open->[0] eq 'punct' && $open->[1] eq '('; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$open and $open->[0] eq "punct" and $open->[1] eq "("
$open and $open->[0] eq "punct"
MC/DC0%0/3
$open and $open->[0] eq "punct" and $open->[1] eq "("
$open
$open->[0] eq "punct"
$open->[1] eq "("
Truth table25%1/4
A and B and C
A= $openB= $open->[0] eq "punct"C= $open->[1] eq "("
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # The items of the parenthesised list, split at its own commas. A token is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # [ kind, text, depth ]; depth 1 is the item itself, deeper is inside one of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # its parentheses. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 1074 | my (@items, @item); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 1074 | my $depth = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 1074 | ▶ | while ($depth && @tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$depth and @tokens
MC/DC50%1/2
$depth and @tokens
$depth
@tokens
Truth table66%2/3
A and B
A= $depthB= @tokens
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 30914 | my ($kind, $text) = @{ shift @tokens }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 30914 | ▶ | if ($kind eq 'punct') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 5526 | ▶ | $depth++ if $text eq '('; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 5526 | ▶ | $depth-- if $text eq ')'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 5526 | ▶ | if ($depth == 0 || ($depth == 1 && $text eq ',')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$depth == 0 or $depth == 1 and $text eq ","
$depth == 1 and $text eq ","
MC/DC100%3/3
$depth == 0 or $depth == 1 and $text eq ","
$depth == 0
$depth == 1
$text eq ","
Truth table100%4/4
A or B and C
A= $depth == 0B= $depth == 1C= $text eq ","
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 3231 | push @items, [@item]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 3231 | @item = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 3231 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 27683 | push @item, [ $kind, $text, $depth ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 1074 | ▶ | return if $depth; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 1074 | my (%columns, %constrained, %checks); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 1074 | for my $item (@items) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 3231 | my @tokens = @$item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 3231 | ▶ | shift @tokens while @tokens && $tokens[0][0] eq 'skip'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@tokens and $tokens[0][0] eq "skip"
MC/DC50%1/2
@tokens and $tokens[0][0] eq "skip"
@tokens
$tokens[0][0] eq "skip"
Truth table66%2/3
A and B
A= @tokensB= $tokens[0][0] eq "skip"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 3231 | ▶ | return unless @tokens; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 3231 | my ($kind, $text) = @{ shift @tokens }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # None of these five can be a column name unless it is quoted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 3231 | ▶ | if ($kind eq 'word' && $text =~ /\A(?:CONSTRAINT|PRIMARY|UNIQUE|CHECK|FOREIGN)\z/i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$kind eq "word" and $text =~ /\A(?:CONSTRAINT|PRIMARY|UNIQUE|CHECK|FOREIGN)\z/i
MC/DC100%2/2
$kind eq "word" and $text =~ /\A(?:CONSTRAINT|PRIMARY|UNIQUE|CHECK|FOREIGN)\z/i
$kind eq "word"
$text =~ /\A(?:CONSTRAINT|PRIMARY|UNIQUE|CHECK|FOREIGN)\z/i
Truth table100%3/3
A and B
A= $kind eq "word"B= $text =~ /\A(?:CONSTRAINT|PRIMARY|UNIQUE|CHECK|FOREIGN)\z/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # A table CHECK, on its own or behind CONSTRAINT name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 493 | my @whole = ([ $kind, $text, 1 ], @tokens); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 493 | for my $at (0 .. $#whole) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 5763 | my ($k, $t, $d) = @{ $whole[$at] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 5763 | ▶ | next unless $k eq 'word' && $d == 1 && uc $t eq 'CHECK'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$k eq "word" and $d == 1 and uc $t eq "CHECK"
$k eq "word" and $d == 1
MC/DC100%3/3
$k eq "word" and $d == 1 and uc $t eq "CHECK"
$k eq "word"
$d == 1
uc $t eq "CHECK"
Truth table100%4/4
A and B and C
A= $k eq "word"B= $d == 1C= uc $t eq "CHECK"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 13 | my ($expr) = _group(\@whole, $at + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 13 | ▶ | return unless defined $expr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 13 | $checks{$expr} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 493 | for my $token (@tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 3556 | my ($k, $t, $d) = @$token; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 3556 | ▶ | last if $k eq 'word' && $d == 1 && uc $t eq 'REFERENCES'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$k eq "word" and $d == 1 and uc $t eq "REFERENCES"
$k eq "word" and $d == 1
MC/DC100%3/3
$k eq "word" and $d == 1 and uc $t eq "REFERENCES"
$k eq "word"
$d == 1
uc $t eq "REFERENCES"
Truth table100%4/4
A and B and C
A= $k eq "word"B= $d == 1C= uc $t eq "REFERENCES"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 3344 | ▶ | $constrained{ lc token_identifier($k, $t) } = 1 if $k eq 'word' || $k eq 'quoted'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$k eq "word" or $k eq "quoted"
MC/DC100%2/2
$k eq "word" or $k eq "quoted"
$k eq "word"
$k eq "quoted"
Truth table100%3/3
A or B
A= $k eq "word"B= $k eq "quoted"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 493 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 2738 | my $name = token_identifier($kind, $text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 2738 | ▶ | return if exists $columns{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 2738 | ▶ | my $definition = join '', map { $_->[0] eq 'skip' ? ' ' : $_->[1] } @tokens; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 2738 | $definition =~ s/\A | \z//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 2738 | ▶ | my $clauses = _column_clauses(\@tokens) or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | $columns{$name} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | sql => $definition, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | keyed => scalar(grep { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 2738 | ▶ | $_->[0] eq 'word' && $_->[2] == 1 && $_->[1] =~ /\A(?:PRIMARY|UNIQUE)\z/i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$_->[0] eq 'word' && $_->[2] == 1 && $_->[1] =~ /\A(?:PRIMARY|UNIQUE)\z/i
$_->[0] eq "word" and $_->[2] == 1
MC/DC100%3/3
$_->[0] eq 'word' && $_->[2] == 1 && $_->[1] =~ /\A(?:PRIMARY|UNIQUE)\z/i
$_->[0] eq "word"
$_->[2] == 1
$_->[1] =~ /\A(?:PRIMARY|UNIQUE)\z/i
Truth table100%4/4
A and B && C
A= $_->[0] eq "word"B= $_->[2] == 1C= $_->[1] =~ /\A(?:PRIMARY|UNIQUE)\z/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | } @tokens), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | %$clauses, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 1074 | columns => \%columns, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | constrained => \%constrained, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | checks => [ sort keys %checks ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | # The fields of a column that changed: changed_column_fields, less a default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | # that is the same one spelled in the other way -- see "What counts as a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | # changed column". Every place that asks whether or how a column changed asks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | # here, so that the op and what it says of itself cannot disagree. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | # Measured against SQLite 3.51: in a TEXT column a bare integer and the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | # digits in apostrophes store the same text as long as the integer fits into | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | # 64 bit. Past that it is read as a REAL (9223372036854775808 is stored as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | # 9.22337203685478e+18). 18 digits always fit; the 19-digit values that fit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | # too are given up for a rule that needs no arithmetic. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 192 | my $STRING_AS_INTEGER = qr/\A-?(?:0|[1-9]\d{0,17})\z/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 110 | ▶ | sub _same_text_default { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_same_text_default: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 110 | my ($old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 110 | ▶ | return 0 if grep { type_affinity($_->{data_type}) ne 'TEXT' } $old, $new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 84 | my @defaults = ($old->{default_value}, $new->{default_value}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 84 | ▶ | return 0 if grep { !defined } @defaults; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 78 | for my $pair ([@defaults], [ reverse @defaults ]) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 139 | my ($bare, $quoted) = @$pair; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 139 | ▶ | return 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$bare =~ /$STRING_AS_INTEGER/ and $bare ne "-0" and $quoted eq "'${bare}'"
$bare =~ /$STRING_AS_INTEGER/ and $bare ne "-0"
MC/DC100%3/3
$bare =~ /$STRING_AS_INTEGER/ and $bare ne "-0" and $quoted eq "'${bare}'"
$bare =~ /$STRING_AS_INTEGER/
$bare ne "-0"
$quoted eq "'${bare}'"
Truth table100%4/4
A and B and C
A= $bare =~ /$STRING_AS_INTEGER/B= $bare ne "-0"C= $quoted eq "'${bare}'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | if $bare =~ $STRING_AS_INTEGER && $bare ne '-0' && $quoted eq "'$bare'"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 54 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 1475 | ▶ | sub _changed_fields { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_changed_fields: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 1475 | my ($old, $new, $described) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 1475 | my @changed = changed_column_fields($old, $new); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | # The core leaves a field the target does not state alone. A target that is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | # the introspection of a database deployed from the schema states all of it: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # a column without a default is a column without one, and the default the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # live column has is removed (karr k91). A live DEFAULT NULL is no default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | push @changed, 'default_value' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | if $described && !defined $new->{default_value} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 1475 | ▶ | && defined $old->{default_value} && uc $old->{default_value} ne 'NULL'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$described and not defined $new->{"default_value"} and defined $old->{"default_value"} and uc $old->{"default_value"} ne "NULL"
$described and not defined $new->{"default_value"} and defined $old->{"default_value"}
$described and not defined $new->{"default_value"}
MC/DC100%4/4
$described and not defined $new->{"default_value"} and defined $old->{"default_value"} and uc $old->{"default_value"} ne "NULL"
$described
not defined $new->{"default_value"}
defined $old->{"default_value"}
uc $old->{"default_value"} ne "NULL"
Truth table100%5/5
A and not(B) and C and D
A= $describedB= not defined $new->{"default_value"}C= defined $old->{"default_value"}D= uc $old->{"default_value"} ne "NULL"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | return @changed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 1475 | ▶ | unless (grep { $_ eq 'default_value' } @changed) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
grep {$_ eq "default_value";} @changed and _same_text_default($old, $new)
MC/DC100%2/2
grep {$_ eq "default_value";} @changed and _same_text_default($old, $new)
grep {$_ eq "default_value";} @changed
_same_text_default($old, $new)
Truth table100%3/3
A and B
A= grep {$_ eq "default_value";} @changedB= _same_text_default($old, $new)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | && _same_text_default($old, $new); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 24 | return grep { $_ ne 'default_value' } @changed; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 449 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=20 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 449 | my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | # ($definition, $needs_rebuild) of a column the target adds. Nothing at all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | # when the model has no CREATE TABLE statement for the table: no statement, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | # or that of a view or a virtual table, which neither an ALTER nor a rebuild | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | # applies to. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 449 | my %declared; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 69 | ▶ | my $declaration = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 69 | my ($table, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 69 | ▶ | my $sql = $target_tables && ($target_tables->{$table} // {})->{sql}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition60%3/5
$target_tables && +($target_tables->{$table} // {})->{'sql'}
$target_tables->{$table} // {}
MC/DC0%0/2
$target_tables && +($target_tables->{$table} // {})->{'sql'}
$target_tables
$target_tables->{$table}
Truth table33%1/3
A && B
A= $target_tablesB= $target_tables->{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 69 | ▶ | return unless defined $sql && $sql =~ /\A\s*CREATE\s+TABLE\b/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $sql and $sql =~ /\A\s*CREATE\s+TABLE\b/i
MC/DC50%1/2
defined $sql and $sql =~ /\A\s*CREATE\s+TABLE\b/i
defined $sql
$sql =~ /\A\s*CREATE\s+TABLE\b/i
Truth table66%2/3
A and B
A= defined $sqlB= $sql =~ /\A\s*CREATE\s+TABLE\b/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 65 | ▶ | my $table_ddl = $declared{$table} //= _declarations($sql) // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
$declared{$table} //= _declarations($sql) // {}
_declarations($sql) // {}
MC/DC100%1/1
$declared{$table} //= _declarations($sql) // {}
$declared{$table}
Truth table100%2/2
A
A= $declared{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 65 | ▶ | my $column = $table_ddl->{columns}{$name} or return (undef, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | $column->{sql}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | ($column->{keyed} || $column->{nonconstant} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 64 | ▶ | || $table_ddl->{constrained}{ lc $name }) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$column->{"keyed"} or $column->{"nonconstant"} or $table_ddl->{"constrained"}{lc $name}
$column->{"keyed"} or $column->{"nonconstant"}
MC/DC100%3/3
$column->{"keyed"} or $column->{"nonconstant"} or $table_ddl->{"constrained"}{lc $name}
$column->{"keyed"}
$column->{"nonconstant"}
$table_ddl->{"constrained"}{lc $name}
Truth table100%4/4
A or B or C
A= $column->{"keyed"}B= $column->{"nonconstant"}C= $table_ddl->{"constrained"}{lc $name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 449 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # The create/change/drop walk over columns nested under retained tables is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | # the generic DBIO::Diff::Op->diff_nested. Only tables present in both models | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | # are walked (scope 'both') -- columns of brand-new / dropped tables ride | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | # along with the table op. _changed_fields is the per-engine "did this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | # column change" predicate; everything else here is SQLite-shaped ops. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | # is_auto_increment is not SQLite's to express in a WITHOUT ROWID table (it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | # needs the rowid), so the columns of such a table, in either model, are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | # compared without it -- see "What counts as a changed column". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 449 | my %no_autoinc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 449 | for my $side ([$source_cols, $source_tables], [$target_cols, $target_tables]) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 898 | my ($cols, $tables) = @$side; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 898 | ▶ | for my $table (keys %{ $tables || {} }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$tables || {}
MC/DC0%0/1
$tables || {}
$tables
Truth table50%1/2
A
A= $tables
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 1346 | ▶ | next unless $tables->{$table}{without_rowid}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 18 | ▶ | $no_autoinc{ refaddr $_ } = 1 for @{ $cols->{$table} || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$cols->{$table} || []
MC/DC0%0/1
$cols->{$table} || []
$cols->{$table}
Truth table50%1/2
A
A= $cols->{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | # A target table that has its CREATE TABLE statement is the introspection of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | # a deployed schema and states every column in full, a default included. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 449 | my %described; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 449 | ▶ | for my $table (keys %{ $target_tables || {} }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_tables || {}
MC/DC0%0/1
$target_tables || {}
$target_tables
Truth table50%1/2
A
A= $target_tables
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 677 | my $sql = $target_tables->{$table}{sql}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 677 | ▶ | next unless defined $sql && $sql =~ /\A\s*CREATE\s+TABLE\b/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $sql and $sql =~ /\A\s*CREATE\s+TABLE\b/i
MC/DC50%1/2
defined $sql and $sql =~ /\A\s*CREATE\s+TABLE\b/i
defined $sql
$sql =~ /\A\s*CREATE\s+TABLE\b/i
Truth table66%2/3
A and B
A= defined $sqlB= $sql =~ /\A\s*CREATE\s+TABLE\b/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 539 | ▶ | $described{ refaddr $_ } = 1 for @{ $target_cols->{$table} || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_cols->{$table} || []
MC/DC0%0/1
$target_cols->{$table} || []
$target_cols->{$table}
Truth table50%1/2
A
A= $target_cols->{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 1421 | ▶ | my $changed_when = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 1421 | my ($old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 1421 | ▶ | my $described = $described{ refaddr $new } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | return scalar _changed_fields($old, $new, $described) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 1421 | ▶ | unless $no_autoinc{ refaddr $old } || $no_autoinc{ refaddr $new }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$no_autoinc{builtin::refaddr($old)} or $no_autoinc{builtin::refaddr($new)}
MC/DC100%2/2
$no_autoinc{builtin::refaddr($old)} or $no_autoinc{builtin::refaddr($new)}
$no_autoinc{builtin::refaddr($old)}
$no_autoinc{builtin::refaddr($new)}
Truth table100%3/3
A or B
A= $no_autoinc{builtin::refaddr($old)}B= $no_autoinc{builtin::refaddr($new)}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 23 | my %old = %$old; my %new = %$new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 23 | delete $old{is_auto_increment}; delete $new{is_auto_increment}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 23 | return scalar _changed_fields(\%old, \%new, $described); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 449 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | my @ops = $class->diff_nested($source_cols, $target_cols, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | index_by => 'column_name', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | scope => 'both', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | source_tables => $source_tables, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | target_tables => $target_tables, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | changed_when => $changed_when, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 69 | ▶ | on_new => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 69 | my ($table, $name, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 69 | my ($definition, $needs_rebuild) = $declaration->($table, $name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 69 | $class->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | action => 'add', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | table_name => $table, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | column_name => $name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | new_info => $new, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | definition => $definition, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | needs_rebuild => $needs_rebuild, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 107 | ▶ | on_changed => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 107 | my ($table, $name, $old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | $class->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | action => 'alter', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | table_name => $table, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | column_name => $name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | old_info => $old, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | new_info => $new, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 107 | ▶ | target_described => $described{ refaddr $new } ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 8 | ▶ | on_gone => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 8 | my ($table, $name, $old) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 8 | $class->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | action => 'drop', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | table_name => $table, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | column_name => $name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | old_info => $old, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 449 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | # diff_nested walks the names sorted; put each table's add ops into the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | # target's column order. The tables come sorted and stay so, which leaves | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | # every table's ops in the places they had. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 449 | my %position; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 449 | ▶ | for my $table (keys %{ $target_cols // {} }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_cols // {}
MC/DC0%0/1
$target_cols // {}
$target_cols
Truth table50%1/2
A
A= $target_cols
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 717 | my $columns = $target_cols->{$table}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 717 | ▶ | next unless ref $columns eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 717 | $position{$table}{ $columns->[$_]{column_name} } = $_ for 0 .. $#$columns; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 449 | my @adds = grep { $ops[$_]->action eq 'add' } 0 .. $#ops; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | @ops[@adds] = sort { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 449 | $a->table_name cmp $b->table_name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | or ($position{ $a->table_name }{ $a->column_name } // 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 17 | ▶ | <=> ($position{ $b->table_name }{ $b->column_name } // 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition42%3/7
$a->table_name cmp $b->table_name || ($position{$a->table_name}{$a->column_name} // 0) <=> ($position{$b->table_name}{$b->column_name} // 0)
$position{$a->table_name}{$a->column_name} // 0
$position{$b->table_name}{$b->column_name} // 0
MC/DC0%0/2
$a->table_name cmp $b->table_name || ($position{$a->table_name}{$a->column_name} // 0) <=> ($position{$b->table_name}{$b->column_name} // 0)
$a->table_name cmp $b->table_name
$position{$a->table_name}{$a->column_name}
MC/DC0%0/1
$position{$b->table_name}{$b->column_name} // 0
$position{$b->table_name}{$b->column_name}
Truth table33%1/3
A || B
A= $a->table_name cmp $b->table_nameB= $position{$a->table_name}{$a->column_name}
Truth table50%1/2
A
A= $position{$b->table_name}{$b->column_name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | } @ops[@adds]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 449 | return @ops; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | # One changed field of an alter op, as the refusal names it. The fields are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # named in this order, whichever one the comparison found first. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 192 | my @FIELDS = qw(data_type not_null default_value size); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 192 | my %FIELD_LABEL = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | data_type => 'type', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | not_null => 'NOT NULL', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | default_value => 'default', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | size => 'size', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 60 | ▶ | sub _field_change { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_field_change: called CC=6 SCAR=18.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 60 | my ($field, $old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | my ($from, $to) = map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 60 | my $value = $_->{$field}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 120 | ▶ | $field eq 'not_null' ? ($value ? 'yes' : 'no') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | : !defined $value ? 'none' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | : ref $value eq 'ARRAY' ? join(',', @$value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | : $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | } $old, $new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 60 | ▶ | return sprintf '%s %s -> %s', $FIELD_LABEL{$field} // $field, $from, $to; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$FIELD_LABEL{$field} // $field
MC/DC0%0/2
$FIELD_LABEL{$field} // $field
$FIELD_LABEL{$field}
$field
Truth table33%1/3
A // B
A= $FIELD_LABEL{$field}B= $field
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 54 | ▶ | sub changes { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
changes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 54 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 54 | my ($old, $new) = ($self->old_info, $self->new_info); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 54 | my %changed = map { $_ => 1 } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | _changed_fields($old, $new, $self->target_described); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | return join ', ', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 54 | map { _field_change($_, $old, $new) } grep { $changed{$_} } @FIELDS; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 92 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 92 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 92 | ▶ | if ($self->action eq 'add') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 73 | ▶ | if ($self->needs_rebuild) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 2 | Carp::croak(sprintf | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 'Cannot add column %s.%s by ALTER TABLE: ADD COLUMN cannot carry its ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | . 'declaration (UNIQUE, PRIMARY KEY, a table constraint naming the ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | . 'column or a non-constant DEFAULT), table %s has to be rebuilt from its CREATE TABLE statement', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | $self->table_name, $self->column_name, $self->table_name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 71 | my $sql = sprintf 'ALTER TABLE %s ADD COLUMN %s', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | quote_identifier($self->table_name), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | quote_identifier($self->column_name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | # The column as the target declares it (karr k64). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 71 | ▶ | if (defined(my $definition = $self->definition)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 63 | ▶ | $sql .= " $definition" if length $definition; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 63 | return "$sql;"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 8 | my $info = $self->new_info; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 8 | ▶ | $sql .= ' ' . type_with_size($info->{data_type} // 'TEXT', $info->{size}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"data_type"} // "TEXT"
MC/DC0%0/1
$info->{"data_type"} // "TEXT"
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 8 | ▶ | $sql .= ' NOT NULL' if $info->{not_null}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 8 | ▶ | if (defined $info->{default_value}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 5 | $sql .= ' DEFAULT ' . default_value_sql($info->{default_value}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 8 | return "$sql;"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 19 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 11 | return sprintf 'ALTER TABLE %s DROP COLUMN %s;', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | quote_identifier($self->table_name), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | quote_identifier($self->column_name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 8 | ▶ | if ($self->action eq 'alter') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | 8 | Carp::croak(sprintf | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | 'Cannot change column %s.%s (%s): SQLite has no ALTER COLUMN, and the ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | . 'target model holds no CREATE TABLE statement to rebuild table %s from', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | $self->table_name, $self->column_name, $self->changes, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | $self->table_name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 8 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 8 | ▶ | my $prefix = $self->action eq 'add' ? '+' : $self->action eq 'drop' ? '-' : '~'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 8 | ▶ | my $type = $self->new_info ? " ($self->{new_info}{data_type})" : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 8 | return sprintf ' %scolumn: %s.%s%s', $prefix, $self->table_name, $self->column_name, $type; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 192 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||