blib/lib/DBIO/SQLite/Diff/Column.pm

filter: Statementstmt 99.2% 247 / 249 Branchbran 88.0% 132 / 150 Conditioncond 73.2% 115 / 157 MC/DCmcdc 57.1% 48 / 84 Subroutinesub 100% 26 / 26 Podpod 100% 6 / 6 totaltotal 85.4% 574 / 672 CC 144 SCAR 29.1
CC
144
Cov
89%
_declarations
39.6
diff
21.0
_column_clauses
19.4
CRAP 18.4
« blib/lib/DBIO/SQLite/DDL.pm ↑ Summary blib/lib/DBIO/SQLite.pm »
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
BranchTrueFalseTotal
unless $from eq $to 25344 4992 30336
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
BranchTrueFalseTotal
unless $self->action eq "alter" 35 56 91
49 56 ▶ return column_change_lossy($self->old_info, $self->new_info, \%LOSSLESS_PAIRS) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
column_change_lossy($self->old_info, $self->new_info, \%LOSSLESS_PAIRS) ? : 7 49 56
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
BranchTrueFalseTotal
$tokens->[$from] and $tokens->[$from][0] eq "skip" and do { ++$from } 88 88 176
Condition66%2/3
$tokens->[$from] and $tokens->[$from][0] eq "skip"
!ll&&!rl&&r
08888
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"
ABresult
0X0
100
111
96 88 my $open = $tokens->[$from];
97 88 ▶ return unless $open && $open->[0] eq 'punct' && $open->[1] eq '(';
Branch: 1 branch
BranchTrueFalseTotal
unless $open and $open->[0] eq "punct" and $open->[1] eq "(" 0 88 88
Condition33%2/6
$open and $open->[0] eq "punct" and $open->[1] eq "("
!ll&&!rl&&r
0088
$open and $open->[0] eq "punct"
!ll&&!rl&&r
0088
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 "("
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if $kind eq "punct" and $text eq ")" and $depth == $open->[2] - 1 88 455 543
Condition100%6/6
$kind eq "punct" and $text eq ")" and $depth == $open->[2] - 1
!ll&&!rl&&r
4381788
$kind eq "punct" and $text eq ")"
!ll&&!rl&&r
41721105
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
ABCresult
0XX0
10X0
1100
1111
103 if $kind eq 'punct' && $text eq ')' && $depth == $open->[2] - 1;
104 455 ▶ push @inner, $text unless $kind eq 'skip';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "skip" 297 158 455
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
BranchTrueFalseTotal
unless $kind eq "word" and $depth == 1 9201 7185 16386
Condition100%3/3
$kind eq "word" and $depth == 1
!ll&&!rl&&r
86685337185
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
ABresult
0X0
100
111
120 7185 my $word = uc $text;
121  
122 7185 ▶ if ($word eq 'DEFAULT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "DEFAULT") 164 7021 7185
123 164 ▶ $nonconstant = 1 if _nonconstant_default($tokens, $at + 1);
Branch: 1 branch
BranchTrueFalseTotal
if _nonconstant_default($tokens, $at + 1) 34 130 164
124 }
125  
126 7185 ▶ if ($word eq 'CHECK') {
Branch: 3 branches
BranchTrueFalseTotal
if ($word eq 'CHECK') { } 38 7147 7185
elsif ($word eq 'COLLATE') { } 18 7129 7147
elsif ($word eq 'AS') { } 20 7109 7129
127 38 my ($expr) = _group($tokens, $at + 1);
128 38 ▶ return unless defined $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 0 38 38
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
BranchTrueFalseTotal
unless $name and $name->[0] eq "word" || $name->[0] eq "quoted" 0 18 18
Condition33%2/6
$name and $name->[0] eq "word" || $name->[0] eq "quoted"
!ll&&!rl&&r
0018
$name->[0] eq "word" or $name->[0] eq "quoted"
l!l&&r!l&&!r
1800
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"
ABCresult
0XX0
1000
11X1
1011
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
BranchTrueFalseTotal
unless defined $expr 0 20 20
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'
!ll&&!rl&&r
0155
$next and $next->[0] eq "word"
!ll&&!rl&&r
0020
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'
ABCresult
0XX0
10X0
1100
1111
141 20 ▶ $generated = $expr . ($stored ? ' STORED' : ' VIRTUAL');
Branch: 1 branch
BranchTrueFalseTotal
$stored ? : 5 15 20
142 }
143 }
144  
145 return {
146 2738 ▶ checks => [ sort keys %checks ],
Branch: 1 branch
BranchTrueFalseTotal
$nonconstant ? : 34 2704 2738
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
BranchTrueFalseTotal
my $first = $rest[0] or return 0 164 0 164
163  
164 164 ▶ return 1 if $first->[0] eq 'word'
Branch: 1 branch
BranchTrueFalseTotal
if $first->[0] eq "word" and $first->[1] =~ /\ACURRENT_(?:TIME|DATE|TIMESTAMP)\z/i 23 141 164
Condition100%3/3
$first->[0] eq "word" and $first->[1] =~ /\ACURRENT_(?:TIME|DATE|TIMESTAMP)\z/i
!ll&&!rl&&r
677423
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
ABresult
0X0
100
111
165 && $first->[1] =~ /\ACURRENT_(?:TIME|DATE|TIMESTAMP)\z/i;
166 141 ▶ return 0 unless $first->[0] eq 'punct' && $first->[1] eq '(';
Branch: 1 branch
BranchTrueFalseTotal
unless $first->[0] eq "punct" and $first->[1] eq "(" 124 17 141
Condition66%2/3
$first->[0] eq "punct" and $first->[1] eq "("
!ll&&!rl&&r
124017
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 "("
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $expr 0 17 17
170 17 my $open = $from;
171 17 ▶ $open++ while $tokens->[$open][0] eq 'skip';
Branch: 1 branch
BranchTrueFalseTotal
$tokens->[$open][0] eq "skip" and do { ++$open } 17 17 34
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
BranchTrueFalseTotal
if @inner == 2 and $inner[0][0] eq "word" and $inner[0][1] =~ /\A[-+]\z/ 2 15 17
Condition66%4/6
@inner == 2 and $inner[0][0] eq "word" and $inner[0][1] =~ /\A[-+]\z/
!ll&&!rl&&r
1502
@inner == 2 and $inner[0][0] eq "word"
!ll&&!rl&&r
1502
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/
ABCresult
0XX0
10X0
1100
1111
174 17 ▶ return 1 unless @inner == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @inner == 1 11 6 17
175 6 my ($kind, $text) = @{ $inner[0] };
176 6 ▶ return 0 if $kind eq 'quoted' && $text =~ /\A'/;
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "quoted" and $text =~ /\A'/ 2 4 6
Condition66%2/3
$kind eq "quoted" and $text =~ /\A'/
!ll&&!rl&&r
402
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'/
ABresult
0X0
100
111
177 4 ▶ return 0 if $kind eq 'word'
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "word" and $text =~ /\A(?:NULL|TRUE|FALSE|[0-9][0-9a-z_.]*|\.[0-9][0-9a-z_.]*)\z/i 4 0 4
Condition33%1/3
$kind eq "word" and $text =~ /\A(?:NULL|TRUE|FALSE|[0-9][0-9a-z_.]*|\.[0-9][0-9a-z_.]*)\z/i
!ll&&!rl&&r
004
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $name_at 0 1074 1074
189 1074 splice @tokens, 0, $name_at + 1;
190 1074 ▶ shift @tokens while @tokens && $tokens[0][0] eq 'skip';
Branch: 1 branch
BranchTrueFalseTotal
@tokens and $tokens[0][0] eq "skip" and do { shift @tokens } 1074 1074 2148
Condition66%2/3
@tokens and $tokens[0][0] eq "skip"
!ll&&!rl&&r
010741074
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"
ABresult
0X0
100
111
191 1074 my $open = shift @tokens;
192 1074 ▶ return unless $open && $open->[0] eq 'punct' && $open->[1] eq '(';
Branch: 1 branch
BranchTrueFalseTotal
unless $open and $open->[0] eq "punct" and $open->[1] eq "(" 0 1074 1074
Condition33%2/6
$open and $open->[0] eq "punct" and $open->[1] eq "("
!ll&&!rl&&r
001074
$open and $open->[0] eq "punct"
!ll&&!rl&&r
001074
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 "("
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if ($depth and @tokens) 30914 1074 31988
Condition66%2/3
$depth and @tokens
!ll&&!rl&&r
1074030914
MC/DC50%1/2
$depth and @tokens
$depth @tokens
Truth table66%2/3
A and B
A= $depthB= @tokens
ABresult
0X0
100
111
200 30914 my ($kind, $text) = @{ shift @tokens };
201 30914 ▶ if ($kind eq 'punct') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "punct") 5526 25388 30914
202 5526 ▶ $depth++ if $text eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "(" 1118 4408 5526
203 5526 ▶ $depth-- if $text eq ')';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq ")" 2192 3334 5526
204 5526 ▶ if ($depth == 0 || ($depth == 1 && $text eq ',')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0 or $depth == 1 and $text eq ",") 3231 2295 5526
Condition100%6/6
$depth == 0 or $depth == 1 and $text eq ","
l!l&&r!l&&!r
107421572295
$depth == 1 and $text eq ","
!ll&&!rl&&r
119411012157
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 ","
ABCresult
1XX1
0111
00X0
0100
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
BranchTrueFalseTotal
if $depth 0 1074 1074
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
BranchTrueFalseTotal
@tokens and $tokens[0][0] eq "skip" and do { shift @tokens } 2796 3231 6027
Condition66%2/3
@tokens and $tokens[0][0] eq "skip"
!ll&&!rl&&r
032312796
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"
ABresult
0X0
100
111
218 3231 ▶ return unless @tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless @tokens 0 3231 3231
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
BranchTrueFalseTotal
if ($kind eq "word" and $text =~ /\A(?:CONSTRAINT|PRIMARY|UNIQUE|CHECK|FOREIGN)\z/i) 493 2738 3231
Condition100%3/3
$kind eq "word" and $text =~ /\A(?:CONSTRAINT|PRIMARY|UNIQUE|CHECK|FOREIGN)\z/i
!ll&&!rl&&r
16471091493
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $k eq "word" and $d == 1 and uc $t eq "CHECK" 5750 13 5763
Condition100%6/6
$k eq "word" and $d == 1 and uc $t eq "CHECK"
!ll&&!rl&&r
4379137113
$k eq "word" and $d == 1
!ll&&!rl&&r
42631161384
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"
ABCresult
0XX0
10X0
1100
1111
228 13 my ($expr) = _group(\@whole, $at + 1);
229 13 ▶ return unless defined $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 0 13 13
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
BranchTrueFalseTotal
if $k eq "word" and $d == 1 and uc $t eq "REFERENCES" 212 3344 3556
Condition100%6/6
$k eq "word" and $d == 1 and uc $t eq "REFERENCES"
!ll&&!rl&&r
2926418212
$k eq "word" and $d == 1
!ll&&!rl&&r
283492630
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"
ABCresult
0XX0
10X0
1100
1111
236 3344 ▶ $constrained{ lc token_identifier($k, $t) } = 1 if $k eq 'word' || $k eq 'quoted';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "word" or $k eq "quoted" 978 2366 3344
Condition100%3/3
$k eq "word" or $k eq "quoted"
l!l&&r!l&&!r
5104682366
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"
ABresult
1X1
011
000
237 }
238 493 next;
239 }
240  
241 2738 my $name = token_identifier($kind, $text);
242 2738 ▶ return if exists $columns{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $columns{$name} 0 2738 2738
243  
244 2738 ▶ my $definition = join '', map { $_->[0] eq 'skip' ? ' ' : $_->[1] } @tokens;
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq 'skip' ? : 7763 8623 16386
245 2738 $definition =~ s/\A | \z//g;
246 2738 ▶ my $clauses = _column_clauses(\@tokens) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $clauses = _column_clauses(\@tokens) or return 2738 0 2738
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
!ll&&!rl&&r
92016309876
$_->[0] eq "word" and $_->[2] == 1
!ll&&!rl&&r
86685337185
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
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if grep {type_affinity($_->{"data_type"}) ne "TEXT";} $old, $new 26 84 110
278  
279 84 my @defaults = ($old->{default_value}, $new->{default_value});
280 84 ▶ return 0 if grep { !defined } @defaults;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @defaults 6 78 84
281  
282 78 for my $pair ([@defaults], [ reverse @defaults ]) {
283 139 my ($bare, $quoted) = @$pair;
284 139 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $bare =~ /$STRING_AS_INTEGER/ and $bare ne "-0" and $quoted eq "'${bare}'" 24 115 139
Condition100%6/6
$bare =~ /$STRING_AS_INTEGER/ and $bare ne "-0" and $quoted eq "'${bare}'"
!ll&&!rl&&r
107824
$bare =~ /$STRING_AS_INTEGER/ and $bare ne "-0"
!ll&&!rl&&r
105232
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}'"
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if $described and not defined $new->{"default_value"} and defined $old->{"default_value"} and uc $old->{"default_value"} ne "NULL" 4 1471 1475
Condition100%9/9
$described and not defined $new->{"default_value"} and defined $old->{"default_value"} and uc $old->{"default_value"} ne "NULL"
!ll&&!rl&&r
147014
$described and not defined $new->{"default_value"} and defined $old->{"default_value"}
!ll&&!rl&&r
24412265
$described and not defined $new->{"default_value"}
!ll&&!rl&&r
174701231
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"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
301  
302 return @changed
303 1475 ▶ unless (grep { $_ eq 'default_value' } @changed)
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "default_value";} @changed and _same_text_default($old, $new) 1451 24 1475
Condition100%3/3
grep {$_ eq "default_value";} @changed and _same_text_default($old, $new)
!ll&&!rl&&r
13658624
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)
ABresult
0X0
100
111
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'}
!ll&&!rl&&r
0465
$target_tables->{$table} // {}
l!l
690
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}
ABresult
0X0
100
111
320 69 ▶ return unless defined $sql && $sql =~ /\A\s*CREATE\s+TABLE\b/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sql and $sql =~ /\A\s*CREATE\s+TABLE\b/i 4 65 69
Condition66%2/3
defined $sql and $sql =~ /\A\s*CREATE\s+TABLE\b/i
!ll&&!rl&&r
4065
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
ABresult
0X0
100
111
321  
322 65 ▶ my $table_ddl = $declared{$table} //= _declarations($sql) // {};
Condition75%3/4
$declared{$table} //= _declarations($sql) // {}
l!l
1154
_declarations($sql) // {}
l!l
540
MC/DC100%1/1
$declared{$table} //= _declarations($sql) // {}
$declared{$table}
Truth table100%2/2
A
A= $declared{$table}
Aresult
11
00
323 65 ▶ my $column = $table_ddl->{columns}{$name} or return (undef, 1);
Branch: 1 branch
BranchTrueFalseTotal
my $column = $table_ddl->{"columns"}{$name} or return undef, 1 64 1 65
324 return (
325 $column->{sql},
326 ($column->{keyed} || $column->{nonconstant}
327 64 ▶ || $table_ddl->{constrained}{ lc $name }) ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$column->{'keyed'} || $column->{'nonconstant'} || $table_ddl->{'constrained'}{lc $name} ? : 20 44 64
Condition100%6/6
$column->{"keyed"} or $column->{"nonconstant"} or $table_ddl->{"constrained"}{lc $name}
l!l&&r!l&&!r
12844
$column->{"keyed"} or $column->{"nonconstant"}
l!l&&r!l&&!r
5752
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}
ABCresult
1XX1
01X1
0011
0000
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 || {}
l!l
8980
MC/DC0%0/1
$tables || {}
$tables
Truth table50%1/2
A
A= $tables
Aresult
11
00
344 1346 ▶ next unless $tables->{$table}{without_rowid};
Branch: 1 branch
BranchTrueFalseTotal
unless $tables->{$table}{"without_rowid"} 1328 18 1346
345 18 ▶ $no_autoinc{ refaddr $_ } = 1 for @{ $cols->{$table} || [] };
Condition50%1/2
$cols->{$table} || []
l!l
180
MC/DC0%0/1
$cols->{$table} || []
$cols->{$table}
Truth table50%1/2
A
A= $cols->{$table}
Aresult
11
00
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 || {}
l!l
4490
MC/DC0%0/1
$target_tables || {}
$target_tables
Truth table50%1/2
A
A= $target_tables
Aresult
11
00
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
BranchTrueFalseTotal
unless defined $sql and $sql =~ /\A\s*CREATE\s+TABLE\b/i 138 539 677
Condition66%2/3
defined $sql and $sql =~ /\A\s*CREATE\s+TABLE\b/i
!ll&&!rl&&r
1380539
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
ABresult
0X0
100
111
354 539 ▶ $described{ refaddr $_ } = 1 for @{ $target_cols->{$table} || [] };
Condition50%1/2
$target_cols->{$table} || []
l!l
5390
MC/DC0%0/1
$target_cols->{$table} || []
$target_cols->{$table}
Truth table50%1/2
A
A= $target_cols->{$table}
Aresult
11
00
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
BranchTrueFalseTotal
$described{builtin::refaddr($new)} ? : 1255 166 1421
359 return scalar _changed_fields($old, $new, $described)
360 1421 ▶ unless $no_autoinc{ refaddr $old } || $no_autoinc{ refaddr $new };
Branch: 1 branch
BranchTrueFalseTotal
unless $no_autoinc{builtin::refaddr($old)} or $no_autoinc{builtin::refaddr($new)} 1398 23 1421
Condition100%3/3
$no_autoinc{builtin::refaddr($old)} or $no_autoinc{builtin::refaddr($new)}
l!l&&r!l&&!r
1941398
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)}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
$described{builtin::refaddr($new)} ? : 46 61 107
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 // {}
l!l
4490
MC/DC0%0/1
$target_cols // {}
$target_cols
Truth table50%1/2
A
A= $target_cols
Aresult
11
00
411 717 my $columns = $target_cols->{$table};
412 717 ▶ next unless ref $columns eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $columns eq "ARRAY" 0 717 717
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)
l!l&&r!l&&!r
0170
$position{$a->table_name}{$a->column_name} // 0
l!l
170
$position{$b->table_name}{$b->column_name} // 0
l!l
170
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}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $position{$b->table_name}{$b->column_name}
Aresult
11
00
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
BranchTrueFalseTotal
$field eq 'not_null' ? : 64 56 120
$value ? : 32 32 64
!defined($value) ? : 5 51 56
ref $value eq 'ARRAY' ? : 0 51 51
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
l!l&&r!l&&!r
6000
MC/DC0%0/2
$FIELD_LABEL{$field} // $field
$FIELD_LABEL{$field} $field
Truth table33%1/3
A // B
A= $FIELD_LABEL{$field}B= $field
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($self->action eq "add") 73 19 92
462 73 ▶ if ($self->needs_rebuild) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->needs_rebuild) 2 71 73
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
BranchTrueFalseTotal
if (defined(my $definition = $self->definition)) 63 8 71
476 63 ▶ $sql .= " $definition" if length $definition;
Branch: 1 branch
BranchTrueFalseTotal
if length $definition 63 0 63
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"
l!l
80
MC/DC0%0/1
$info->{"data_type"} // "TEXT"
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
Aresult
11
00
482 8 ▶ $sql .= ' NOT NULL' if $info->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"not_null"} 3 5 8
483 8 ▶ if (defined $info->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"default_value"}) 5 3 8
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
BranchTrueFalseTotal
if ($self->action eq "drop") 11 8 19
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
BranchTrueFalseTotal
if ($self->action eq "alter") 8 0 8
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
BranchTrueFalseTotal
$self->action eq 'add' ? : 2 6 8
$self->action eq 'drop' ? : 2 4 6
506 8 ▶ my $type = $self->new_info ? " ($self->{new_info}{data_type})" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->new_info ? : 6 2 8
507 8 return sprintf ' %scolumn: %s.%s%s', $prefix, $self->table_name, $self->column_name, $type;
508 }
509  
510 192 1;
511  
512 __END__
« blib/lib/DBIO/SQLite/DDL.pm ↑ Summary blib/lib/DBIO/SQLite.pm »