blib/lib/DBIO/SQLite/DDL.pm

filter: Statementstmt 100% 155 / 155 Branchbran 88.1% 74 / 84 Conditioncond 76.6% 59 / 77 MC/DCmcdc 60.9% 25 / 41 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 88.0% 324 / 368 CC 82 SCAR 39.7
CC
82
Cov
91%
install_ddl
69.5
_sqlite_column_type
14.5
_fk_action
3.0
CRAP 52.8
↑ Summary blib/lib/DBIO/SQLite/Diff/Column.pm »
1 package DBIO::SQLite::DDL;
2 # ABSTRACT: Generate SQLite DDL from DBIO Result classes
3  
4 189 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 189 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 189 our $VERSION = '0.900007';
8  
9 189 ▶use DBIO::Schema::Type ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 189 ▶use DBIO::SQLite::Adapter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 189 ▶use DBIO::SQLite::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 default_literal_sql foreign_key_action quote_identifier refuse_qualified_name type_with_size
13 189 );
14 189 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 189 my $ADAPTER = DBIO::SQLite::Adapter->new;
17  
18  
19  
20 720 ▶sub install_ddl {
Subroutine install_ddl: called CC=66 SCAR=42.4
Pod documented
21 720 my ($class, $schema) = @_;
22  
23 720 my @stmts;
24 my @view_stmts;
25 720 my %seen_table;
26 720 my %seen_view;
27  
28 # Sorted here as well as by a current core, for the same reason as the
29 # relationships below.
30 720 for my $source_name (sort $schema->sources) {
31 16261 my $source = $schema->source($source_name);
32 16261 my $result_class = $source->result_class;
33 16261 my $table_name = $source->name;
34  
35 # Views: emit CREATE VIEW from the stored definition, after all
36 # tables (they may reference any table). Virtual views are ORM-only
37 # and have no database object to create.
38 16261 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("DBIO::ResultSource::View")) 748 15513 16261
39 748 ▶ next if $source->can('is_virtual') && $source->is_virtual;
Branch: 1 branch
BranchTrueFalseTotal
if $source->can("is_virtual") and $source->is_virtual 348 400 748
Condition66%2/3
$source->can("is_virtual") and $source->is_virtual
!ll&&!rl&&r
0400348
MC/DC50%1/2
$source->can("is_virtual") and $source->is_virtual
$source->can("is_virtual") $source->is_virtual
Truth table66%2/3
A and B
A= $source->can("is_virtual")B= $source->is_virtual
ABresult
0X0
100
111
40 400 ▶ my $vname = ref $table_name eq 'SCALAR' ? $$table_name : $table_name;
Branch: 1 branch
BranchTrueFalseTotal
ref $table_name eq 'SCALAR' ? : 0 400 400
41 400 ▶ next if ref $vname || $seen_view{$vname}++;
Branch: 1 branch
BranchTrueFalseTotal
if ref $vname or $seen_view{$vname}++ 0 400 400
Condition33%1/3
ref $vname or $seen_view{$vname}++
l!l&&r!l&&!r
00400
MC/DC0%0/2
ref $vname or $seen_view{$vname}++
ref $vname $seen_view{$vname}++
Truth table33%1/3
A or B
A= ref $vnameB= $seen_view{$vname}++
ABresult
1X1
011
000
42 400 ▶ my $def = $source->can('view_definition') ? $source->view_definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source->can('view_definition') ? : 400 0 400
43 400 ▶ next unless defined $def && length $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 0 400 400
Condition33%1/3
defined $def and length $def
!ll&&!rl&&r
00400
MC/DC0%0/2
defined $def and length $def
defined $def length $def
Truth table33%1/3
A and B
A= defined $defB= length $def
ABresult
0X0
100
111
44 400 ▶ refuse_qualified_name(view => $table_name) unless ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $table_name 400 0 400
45 399 push @view_stmts, sprintf 'CREATE VIEW %s AS %s;',
46 _name_sql($table_name), $def;
47 399 next;
48 }
49  
50 # A scalar-ref name is either a literal table name (\'cd', emitted
51 # as given) or an inline subquery source (\'(SELECT ...)'). Deref the
52 # former; the latter has no table to create.
53 15513 my $literal = ref $table_name eq 'SCALAR';
54 15513 ▶ if ($literal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literal) 699 14814 15513
55 699 $table_name = $$table_name;
56 699 ▶ next if $table_name =~ /\s|\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $table_name =~ /\s|\(/ 345 354 699
57 }
58 15168 ▶ next if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 15168 15168
59 15168 ▶ refuse_qualified_name(table => $table_name) unless $literal;
Branch: 1 branch
BranchTrueFalseTotal
unless $literal 14814 354 15168
60  
61 # The name as the statements below write it: quoted, unless the schema
62 # gave it as literal SQL.
63 15163 ▶ my $table_sql = $literal ? $table_name : quote_identifier($table_name);
Branch: 1 branch
BranchTrueFalseTotal
$literal ? : 354 14809 15163
64  
65 # Several result sources can map to the same physical table (e.g. a
66 # Result subclass or a source_name alias). Emit each table once.
67 15163 ▶ next if $seen_table{$table_name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_table{$table_name}++ 690 14473 15163
68  
69 14473 my @col_defs;
70 my %is_pk;
71 14473 my @pk_cols = $source->primary_columns;
72 14473 @is_pk{@pk_cols} = (1) x @pk_cols;
73  
74 # Columns that participate in a UNIQUE constraint are implicitly NOT
75 # NULL in well-formed schemas (a unique constraint permits any number
76 # of NULLs, so NOT NULL is what makes the constraint meaningful). DBIO
77 # schema definitions sometimes omit is_nullable on such columns and
78 # rely on the unique declaration to convey it -- promote them to NOT
79 # NULL so the storage layer reports the right is_nullable.
80 14473 my %unique_col;
81 14473 ▶ if ($source->can('unique_constraints')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->can("unique_constraints")) 14473 0 14473
82 14473 my %uniques = $source->unique_constraints;
83 14473 for my $uname (sort keys %uniques) {
84 20030 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 14118 5912 20030
85 5912 my $cols = $uniques{$uname};
86 5912 $unique_col{$_} = 1 for @$cols;
87 }
88 }
89  
90 14473 for my $col_name ($source->columns) {
91 46512 my $info = $source->column_info($col_name);
92 46512 my $type = _sqlite_column_type($info, $col_name);
93  
94 46506 my $def = sprintf ' %s %s', quote_identifier($col_name), $type;
95  
96 # A plain INTEGER PRIMARY KEY column is already a SQLite rowid alias --
97 # no AUTOINCREMENT keyword needed. AUTOINCREMENT only adds the extra
98 # guarantee that rowids are never reused after DELETE (tracked via
99 # sqlite_sequence), which breaks tests that delete rows then re-insert
100 # and expect the rowid to start back at 1. Emit plain PRIMARY KEY,
101 # matching DBIx::Class / SQL::Translator's SQLite producer.
102 46506 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols == 1 and $is_pk{$col_name} and uc $type eq "INTEGER" and $info->{"is_auto_increment"}) 8745 37761 46506
Condition100%9/9
@pk_cols == 1 and $is_pk{$col_name} and uc $type eq "INTEGER" and $info->{"is_auto_increment"}
!ll&&!rl&&r
3621115508745
@pk_cols == 1 and $is_pk{$col_name} and uc $type eq "INTEGER"
!ll&&!rl&&r
3551769410295
@pk_cols == 1 and $is_pk{$col_name}
!ll&&!rl&&r
121432337410989
MC/DC100%4/4
@pk_cols == 1 and $is_pk{$col_name} and uc $type eq "INTEGER" and $info->{"is_auto_increment"}
@pk_cols == 1 $is_pk{$col_name} uc $type eq "INTEGER" $info->{"is_auto_increment"}
Truth table100%5/5
A and B and C and D
A= @pk_cols == 1B= $is_pk{$col_name}C= uc $type eq "INTEGER"D= $info->{"is_auto_increment"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
103 @pk_cols == 1
104 && $is_pk{$col_name}
105 && uc($type) eq 'INTEGER'
106 && $info->{is_auto_increment}
107 ) {
108 8745 $def .= ' PRIMARY KEY';
109 }
110  
111 my $is_nullable = exists $info->{is_nullable}
112 ? $info->{is_nullable}
113 46506 ▶ : ($unique_col{$col_name} ? 0 : undef);
Branch: 2 branches
BranchTrueFalseTotal
exists $info->{'is_nullable'} ? : 13434 33072 46506
$unique_col{$col_name} ? : 6559 26513 33072
114 46506 ▶ $def .= ' NOT NULL' if !$is_nullable;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_nullable 34261 12245 46506
115  
116 46506 ▶ if (defined $info->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"default_value"}) 1127 45379 46506
117 1127 my $dv = $info->{default_value};
118 1127 ▶ if (ref $dv eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dv eq 'SCALAR') { } 354 773 1127
119 354 $def .= " DEFAULT $$dv";
120 } else {
121 # A plain value. In a column with TEXT affinity it is a string and
122 # is written as a string literal, also when it looks like a number:
123 # bare, '007' would arrive as '7'. Anywhere else a number goes
124 # bare, so that the storage layer reports the default as the number
125 # and not a string, and anything else is the string literal, with
126 # its apostrophes doubled (karr k57).
127 773 $def .= ' DEFAULT ' . default_literal_sql($dv, $type);
128 }
129 }
130  
131 46506 push @col_defs, $def;
132 }
133  
134 # Multi-column or non-INTEGER primary key: emit as a table-level constraint
135 my $had_inline_pk = (@pk_cols == 1)
136 && exists $is_pk{ $pk_cols[0] }
137 14467 ▶ && do {
Condition83%5/6
@pk_cols == 1 && exists $is_pk{$pk_cols[0]} && do { my $info = $source->column_info($pk_cols[0]); uc _sqlite_column_type($info) eq 'INTEGER' and $info->{'is_auto_increment'} }
!ll&&!rl&&r
348422388745
@pk_cols == 1 and exists $is_pk{$pk_cols[0]}
!ll&&!rl&&r
3484010983
MC/DC66%2/3
@pk_cols == 1 && exists $is_pk{$pk_cols[0]} && do { my $info = $source->column_info($pk_cols[0]); uc _sqlite_column_type($info) eq 'INTEGER' and $info->{'is_auto_increment'} }
@pk_cols == 1 exists $is_pk{$pk_cols[0]} do { my $info = $source->column_info($pk_cols[0]); uc _sqlite_column_type($info) eq 'INTEGER' and $info->{'is_auto_increment'} }
Truth table75%3/4
A and B && C
A= @pk_cols == 1B= exists $is_pk{$pk_cols[0]}C= do { my $info = $source->column_info($pk_cols[0]); uc _sqlite_column_type($info) eq 'INTEGER' and $info->{'is_auto_increment'} }
ABCresult
0XX0
10X0
1100
1111
138 10983 my $info = $source->column_info($pk_cols[0]);
139 10983 ▶ uc(_sqlite_column_type($info)) eq 'INTEGER' && $info->{is_auto_increment};
Condition100%3/3
uc _sqlite_column_type($info) eq 'INTEGER' && $info->{'is_auto_increment'}
!ll&&!rl&&r
69415448745
MC/DC100%2/2
uc _sqlite_column_type($info) eq 'INTEGER' && $info->{'is_auto_increment'}
uc _sqlite_column_type($info) eq 'INTEGER' $info->{'is_auto_increment'}
Truth table100%3/3
A && B
A= uc _sqlite_column_type($info) eq 'INTEGER'B= $info->{'is_auto_increment'}
ABresult
0X0
100
111
140 };
141 14467 ▶ if (@pk_cols && !$had_inline_pk) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols and not $had_inline_pk) 5367 9100 14467
Condition100%3/3
@pk_cols and not $had_inline_pk
!ll&&!rl&&r
35587455367
MC/DC100%2/2
@pk_cols and not $had_inline_pk
@pk_cols not $had_inline_pk
Truth table100%3/3
A and not(B)
A= @pk_colsB= not $had_inline_pk
ABresult
0X0
100
111
142 push @col_defs, sprintf ' PRIMARY KEY (%s)',
143 5367 join(', ', map { quote_identifier($_) } @pk_cols);
144 }
145  
146 # Unique constraints declared via add_unique_constraint
147 14467 ▶ if ($source->can('unique_constraints')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->can("unique_constraints")) 14467 0 14467
148 14467 my %uniques = $source->unique_constraints;
149 14467 for my $uname (sort keys %uniques) {
150 20024 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 14112 5912 20024
151 5912 my $cols = $uniques{$uname};
152 push @col_defs, sprintf ' UNIQUE (%s)',
153 5912 join(', ', map { quote_identifier($_) } @$cols);
154 }
155 }
156  
157 # Foreign keys derived from belongs_to relationships, in the order of the
158 # relationship names: a core before 7e64e7e6b (karr k74) hands them out in a
159 # hash's order, which is another one in every process; a current one
160 # (Changes under Fixes) sorts them itself, and this sort stays so the text
161 # does not depend on which core runs. Sorted, a table with several keys has
162 # the same text every time, and of two declarations of one key it is
163 # always the first by name that writes the line. Two relationships with
164 # different actions on one key still write both lines, now always in the
165 # same order -- which makes what SQLite does with them reproducible and
166 # decides nothing about whether they should be written (karr k74, open).
167 14467 my %seen_fk;
168 14467 for my $rel (sort $source->relationships) {
169 29353 my $info = $source->relationship_info($rel);
170 29353 ▶ next unless $info && $info->{attrs} && $info->{attrs}{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"} 15190 14163 29353
Condition50%3/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
01519014163
$info and $info->{"attrs"}
!ll&&!rl&&r
0029353
MC/DC33%1/3
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info $info->{"attrs"} $info->{"attrs"}{"is_foreign_key_constraint"}
Truth table50%2/4
A and B and C
A= $infoB= $info->{"attrs"}C= $info->{"attrs"}{"is_foreign_key_constraint"}
ABCresult
0XX0
10X0
1100
1111
171  
172 14163 my $foreign = $info->{class};
173 14163 my $foreign_source = eval { $schema->source($foreign) }
174 14163 ▶ // eval { $schema->source($foreign =~ s/.*:://r) };
Condition66%2/3
eval { do { $schema->source($foreign) } } // eval { do { $schema->source($foreign =~ s/.*:://r) } }
l!l&&r!l&&!r
1416102
MC/DC50%1/2
eval { do { $schema->source($foreign) } } // eval { do { $schema->source($foreign =~ s/.*:://r) } }
eval { do { $schema->source($foreign) } } eval { do { $schema->source($foreign =~ s/.*:://r) } }
Truth table66%2/3
A // B
A= eval { do { $schema->source($foreign) } }B= eval { do { $schema->source($foreign =~ s/.*:://r) } }
ABresult
1X1
011
000
175 14163 ▶ next unless $foreign_source;
Branch: 1 branch
BranchTrueFalseTotal
unless $foreign_source 2 14161 14163
176  
177 14161 my $cond = $info->{cond};
178 14161 ▶ next unless ref $cond eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cond eq "HASH" 1380 12781 14161
179  
180 12781 my (@from, @to);
181 12781 for my $foreign_col (sort keys %$cond) {
182 14542 my $fcol = $foreign_col;
183 14542 $fcol =~ s/^foreign\.//;
184 14542 my $self_col = $cond->{$foreign_col};
185 14542 $self_col =~ s/^self\.//;
186 14542 push @to, $fcol;
187 14542 push @from, $self_col;
188 }
189  
190 # A literal table name (\'cd') arrives as a scalar ref and is written
191 # as given, like in its own CREATE TABLE.
192 my $key = sprintf 'FOREIGN KEY (%s) REFERENCES %s(%s)',
193 14542 join(', ', map { quote_identifier($_) } @from),
194 _name_sql($foreign_source->name),
195 12781 join(', ', map { quote_identifier($_) } @to);
196 my $actions = _fk_action('ON DELETE', $info->{attrs}{on_delete}, $rel, $table_name)
197 12781 . _fk_action('ON UPDATE', $info->{attrs}{on_update}, $rel, $table_name);
198  
199 # Several relationships can declare the same key -- same columns, same
200 # target, same actions in whichever letter case. It is one key and is
201 # written once (karr k74). The same columns and target with different
202 # actions are not the same line, and each is still written.
203 12774 ▶ next if $seen_fk{ $key . lc $actions }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_fk{$key . lc($actions)}++ 1418 11356 12774
204  
205 11356 push @col_defs, " $key$actions";
206 }
207  
208 14460 push @stmts, sprintf "CREATE TABLE %s (\n%s\n);",
209 $table_sql, join(",\n", @col_defs);
210  
211 # Standalone (non-unique-constraint) indexes: the ones written by hand
212 # with sqlite_index, and the canonical declarations of the result source
213 # (Cake idx, indices; ADR 0061 in core, karr k77). A canonical declaration
214 # of the same name wins over the hand-written one.
215 14460 my %indexes;
216 14460 ▶ if ($result_class->can('sqlite_indexes')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("sqlite_indexes")) 33 14427 14460
217 33 my $hand = $result_class->sqlite_indexes;
218 33 $indexes{$_} = $hand->{$_} for keys %$hand;
219 }
220 # The helper refuses an entry that carries only another driver's block
221 # and an unknown key in the own block (core k333); its message is passed
222 # through. An empty own block is the plain index.
223 14460 for my $decl ($source->index_declarations_for('sqlite', [qw(where expression)])) {
224 41 ▶ my $own = ($decl->{driver} || {})->{sqlite} || {};
Condition75%3/4
+($decl->{'driver'} || {})->{'sqlite'} || {}
l!l
734
$decl->{'driver'} || {}
l!l
410
MC/DC0%0/1
+($decl->{'driver'} || {})->{'sqlite'} || {}
$decl->{'driver'}
Truth table50%1/2
A
A= $decl->{'driver'}
Aresult
11
00
225 $indexes{ $decl->{name} } = {
226 columns => $decl->{columns},
227 unique => $decl->{unique},
228 expression => $own->{expression},
229 where => $own->{where},
230 41 };
231 }
232 # The columns of every unique constraint of the table, written above as
233 # a UNIQUE line, which SQLite backs with an index of its own. A plain
234 # index over exactly those columns would be a second one for the same
235 # lookups and is not written (karr k79), whether it says UNIQUE or not.
236 # An expression or a WHERE is a different object.
237 14457 my %uniq_cols;
238 14457 ▶ if ($source->can('unique_constraints')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->can("unique_constraints")) 14457 0 14457
239 14457 my %uniques = $source->unique_constraints;
240 14457 for my $uname (keys %uniques) {
241 20014 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 14102 5912 20014
242 5912 $uniq_cols{ join "\0", @{ $uniques{$uname} } } = 1;
243 }
244 }
245  
246 14457 for my $idx_name (sort keys %indexes) {
247 140 my $idx = $indexes{$idx_name};
248 next if !defined $idx->{expression} && !$idx->{where}
249 140 ▶ && $uniq_cols{ join "\0", @{ $idx->{columns} // [] } };
Branch: 1 branch
BranchTrueFalseTotal
if not defined $idx->{"expression"} || $idx->{"where"} and $uniq_cols{join "\0", @{$idx->{"columns"} // [];}} 26 114 140
Condition87%7/8
not defined $idx->{"expression"} || $idx->{"where"} and $uniq_cols{join "\0", @{$idx->{"columns"} // [];}}
!ll&&!rl&&r
377726
defined $idx->{'expression'} || $idx->{'where'}
l!l&&r!l&&!r
1126103
$idx->{"columns"} // []
l!l
1030
MC/DC100%3/3
not defined $idx->{"expression"} || $idx->{"where"} and $uniq_cols{join "\0", @{$idx->{"columns"} // [];}}
defined $idx->{'expression'} $idx->{'where'} $uniq_cols{join "\0", @{$idx->{"columns"} // [];}}
MC/DC0%0/1
$idx->{"columns"} // []
$idx->{"columns"}
Truth table100%4/4
not(A || B) and C
A= defined $idx->{'expression'}B= $idx->{'where'}C= $uniq_cols{join "\0", @{$idx->{"columns"} // [];}}
ABCresult
1XX0
01X0
0000
0011
Truth table50%1/2
A
A= $idx->{"columns"}
Aresult
11
00
250 114 ▶ my $unique = $idx->{unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$idx->{'unique'} ? : 26 88 114
251 my $columns = defined $idx->{expression}
252 ? $idx->{expression}
253 114 ▶ : join ', ', map { quote_identifier($_) } @{ $idx->{columns} // [] };
Branch: 1 branch
BranchTrueFalseTotal
defined $idx->{'expression'} ? : 11 103 114
Condition50%1/2
$idx->{"columns"} // []
l!l
1030
MC/DC0%0/1
$idx->{"columns"} // []
$idx->{"columns"}
Truth table50%1/2
A
A= $idx->{"columns"}
Aresult
11
00
254 114 my $sql = sprintf 'CREATE %sINDEX %s ON %s (%s)',
255 $unique, quote_identifier($idx_name), $table_sql, $columns;
256 114 ▶ $sql .= " WHERE $idx->{where}" if $idx->{where};
Branch: 1 branch
BranchTrueFalseTotal
if $idx->{"where"} 27 87 114
257 114 push @stmts, "$sql;";
258 }
259 }
260  
261 698 return join "\n\n", @stmts, @view_stmts;
262 }
263  
264 # The name of a source as a statement writes it. A plain name is an identifier
265 # and is quoted. A scalar ref is literal SQL by the schema's own declaration
266 # and goes in as given -- \'main.cd' is the table cd of the schema main, and
267 # quoting the whole of it would name another table.
268 13180 ▶sub _name_sql {
Subroutine _name_sql: called CC=2 SCAR=6.9
269 13180 my ($name) = @_;
270 13180 ▶ return ref $name eq 'SCALAR' ? $$name : quote_identifier($name);
Branch: 1 branch
BranchTrueFalseTotal
ref $name eq 'SCALAR' ? : 2417 10763 13180
271 }
272  
273 # The " ON DELETE x" / " ON UPDATE x" tail of a FOREIGN KEY line: the action
274 # the belongs_to declares (karr k66), checked against SQLite's five actions and
275 # written as declared (karr k88). Not declared -- absent, undef, '' -- is no
276 # clause, and so is NO ACTION in any letter case, which is what the key has
277 # without one. An
278 # unknown word throws, naming the value and the relationship.
279 25558 ▶sub _fk_action {
Subroutine _fk_action: called CC=3 SCAR=11.0
280 25558 my ($event, $action, $rel, $table) = @_;
281 25558 $action = foreign_key_action($action, "relationship '$rel' of table '$table'");
282 25551 ▶ return '' if !defined $action || uc $action eq 'NO ACTION';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $action or uc $action eq "NO ACTION" 22987 2564 25551
Condition100%3/3
not defined $action or uc $action eq "NO ACTION"
l!l&&r!l&&!r
22975122564
MC/DC100%2/2
not defined $action or uc $action eq "NO ACTION"
not defined $action uc $action eq "NO ACTION"
Truth table100%3/3
not(A) or B
A= not defined $actionB= uc $action eq "NO ACTION"
ABresult
1X1
011
000
283 2564 return " $event $action";
284 }
285  
286 57497 ▶sub _sqlite_column_type {
Subroutine _sqlite_column_type: called CC=14 SCAR=26.7
287 57497 my ($info, $column_name) = @_;
288 57497 ▶ my $type = $info->{data_type} // 'TEXT';
Condition50%1/2
$info->{"data_type"} // "TEXT"
l!l
574970
MC/DC0%0/1
$info->{"data_type"} // "TEXT"
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
Aresult
11
00
289  
290 # Pre-parameterized types pass through unchanged
291 57497 ▶ return $type if $type =~ /\(.+\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\(.+\)$/ 0 57497 57497
292  
293 # Portable base types: delegate to the adapter for the single source of truth.
294 # No eval: the type is a base type, so the one thing canonical_column can
295 # refuse is a size of a form it cannot read, and that refusal is the answer
296 # -- falling on to the alias path wrote another type instead (karr k75).
297 57497 ▶ if (DBIO::Schema::Type::is_base_type(lc $type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DBIO::Schema::Type::is_base_type(lc $type)) 42433 15064 57497
298 42433 ▶ my $canon = DBIO::Schema::Type::canonical_column($column_name // '_', $info);
Condition100%2/2
$column_name // "_"
l!l
3179510638
MC/DC100%1/1
$column_name // "_"
$column_name
Truth table100%2/2
A
A= $column_name
Aresult
11
00
299 42427 return $ADAPTER->to_native($canon);
300 }
301  
302 # Legacy / dialect aliases: keep familiar SQLite-dialect names so that
303 # introspection round-trips cleanly (e.g. VARCHAR stays VARCHAR, not TEXT).
304 15064 my %type_map = (
305 bigint => 'INTEGER',
306 smallint => 'INTEGER',
307 int => 'INTEGER',
308 serial => 'INTEGER',
309 bigserial => 'INTEGER',
310  
311 varchar => 'VARCHAR',
312 char => 'CHAR',
313 string => 'TEXT',
314  
315 real => 'REAL',
316 float => 'REAL',
317 'double precision' => 'REAL',
318  
319 decimal => 'NUMERIC',
320  
321 bytea => 'BLOB',
322  
323 date => 'DATE',
324 datetime => 'DATETIME',
325 time => 'TIME',
326 );
327  
328 15064 ▶ my $native = $type_map{ lc $type } // uc $type;
Condition100%2/2
$type_map{lc $type} // uc $type
l!l
122872777
MC/DC100%1/1
$type_map{lc $type} // uc $type
$type_map{lc $type}
Truth table100%2/2
A
A= $type_map{lc $type}
Aresult
11
00
329  
330 # Parametrize legacy-mapped native types when a size is declared -- DBD::SQLite's
331 # column_info reports COLUMN_SIZE from the declared VARCHAR(N)/CHAR(N)/NUMERIC(P,S),
332 # and downstream consumers (e.g. tests, schema diffs) expect it back.
333 15064 ▶ if (defined $info->{size} && $native =~ /^(?:VARCHAR|CHAR|CHARACTER)$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $info->{'size'} and $native =~ /^(?:VARCHAR|CHAR|CHARACTER)$/i) { } 9867 5197 15064
elsif (ref $info->{'size'} eq 'ARRAY' and $native =~ /^(?:NUMERIC|DECIMAL)$/i) { } 6 5191 5197
elsif (not exists $type_map{lc $type} and defined $info->{'size'} and !ref($info->{'size'}) || ref $info->{'size'} eq 'ARRAY') { } 7 10368 10375
Condition100%3/3
defined $info->{"size"} and $native =~ /^(?:VARCHAR|CHAR|CHARACTER)$/i
!ll&&!rl&&r
5179189867
MC/DC100%2/2
defined $info->{"size"} and $native =~ /^(?:VARCHAR|CHAR|CHARACTER)$/i
defined $info->{"size"} $native =~ /^(?:VARCHAR|CHAR|CHARACTER)$/i
Truth table100%3/3
A and B
A= defined $info->{"size"}B= $native =~ /^(?:VARCHAR|CHAR|CHARACTER)$/i
ABresult
0X0
100
111
334 9867 $native .= "($info->{size})";
335 }
336 ▶ elsif (ref $info->{size} eq 'ARRAY' && $native =~ /^(?:NUMERIC|DECIMAL)$/i) {
Condition66%2/3
ref $info->{"size"} eq "ARRAY" and $native =~ /^(?:NUMERIC|DECIMAL)$/i
!ll&&!rl&&r
519106
MC/DC50%1/2
ref $info->{"size"} eq "ARRAY" and $native =~ /^(?:NUMERIC|DECIMAL)$/i
ref $info->{"size"} eq "ARRAY" $native =~ /^(?:NUMERIC|DECIMAL)$/i
Truth table66%2/3
A and B
A= ref $info->{"size"} eq "ARRAY"B= $native =~ /^(?:NUMERIC|DECIMAL)$/i
ABresult
0X0
100
111
337 6 $native .= '(' . join(',', @{ $info->{size} }) . ')';
338 }
339 # A type this map has no opinion about is written as declared, and so is its
340 # size: BIT(8), TINYINT(1). The mapped names above stay as they are -- an
341 # integer alias is INTEGER whatever its size, which is what keeps a key
342 # column the rowid alias.
343 ▶ elsif (!exists $type_map{ lc $type }
Condition66%6/9
not exists $type_map{lc $type} and defined $info->{"size"} and !ref($info->{"size"}) || ref $info->{"size"} eq "ARRAY"
!ll&&!rl&&r
518407
not exists $type_map{lc $type} and defined $info->{"size"}
!ll&&!rl&&r
276624187
not ref $info->{"size"} or ref $info->{"size"} eq "ARRAY"
l!l&&r!l&&!r
700
MC/DC50%2/4
not exists $type_map{lc $type} and defined $info->{"size"} and !ref($info->{"size"}) || ref $info->{"size"} eq "ARRAY"
not exists $type_map{lc $type} defined $info->{"size"} not ref $info->{"size"} ref $info->{"size"} eq "ARRAY"
Truth table60%3/5
not(A) and B and not(C) or D
A= not exists $type_map{lc $type}B= defined $info->{"size"}C= not ref $info->{"size"}D= ref $info->{"size"} eq "ARRAY"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
344 && defined $info->{size}
345 && (!ref $info->{size} || ref $info->{size} eq 'ARRAY')) {
346 7 $native = type_with_size($native, $info->{size});
347 }
348  
349 15064 return $native;
350 }
351  
352 189 1;
353  
354 __END__
↑ Summary blib/lib/DBIO/SQLite/Diff/Column.pm »