blib/lib/DBIO/SQLite/Introspect/Columns.pm

filter: Statementstmt 100% 36 / 36 Branchbran 85.0% 17 / 20 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 84.0% 63 / 75 CC 17 SCAR 25.8
CC
17
Cov
88%
fetch
16.8
_has_xinfo
2.0
BEGIN
1.0
CRAP 13.2
« blib/lib/DBIO/SQLite/Test/Conformance.pm ↑ Summary blib/lib/DBIO/SQLite/Diff/Table.pm »
1 package DBIO::SQLite::Introspect::Columns;
2 # ABSTRACT: Introspect SQLite columns
3  
4 193 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 193 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 193 our $VERSION = '0.900007';
8  
9 193 ▶use DBIO::SQLite::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 column_is_nullable pk_is_rowid_alias quote_identifier split_type_size
11 193 );
12  
13  
14  
15 757 ▶sub fetch {
Subroutine fetch: called CC=16 SCAR=28.2
Pod documented
16 757 my ($class, $dbh, $tables) = @_;
17 757 my %columns;
18  
19 # Prefer table_xinfo (SQLite 3.26+) -- it knows about hidden/generated cols.
20 757 my $use_xinfo = _has_xinfo($dbh);
21  
22 757 for my $table_name (sort keys %$tables) {
23 1463 ▶ my $pragma = $use_xinfo ? 'table_xinfo' : 'table_info';
Branch: 1 branch
BranchTrueFalseTotal
$use_xinfo ? : 1463 0 1463
24 1463 my $sth = $dbh->prepare(
25 sprintf 'PRAGMA %s(%s)', $pragma, quote_identifier($table_name));
26 1461 $sth->execute;
27  
28 1461 my @cols;
29 1461 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 3621 1461 5082
30 3621 my ($type, $size) = split_type_size($row->{type});
31 push @cols, {
32 column_name => $row->{name},
33 data_type => $type,
34 (defined $size ? (size => $size) : ()),
35 not_null => $row->{notnull} ? 1 : 0,
36 default_value => $row->{dflt_value},
37 is_pk => $row->{pk} ? 1 : 0,
38 pk_position => $row->{pk} // 0,
39 3621 ▶ hidden => $row->{hidden} // 0,
Branch: 3 branches
BranchTrueFalseTotal
defined $size ? : 407 3214 3621
$row->{'notnull'} ? : 1452 2169 3621
$row->{'pk'} ? : 1359 2262 3621
Condition50%2/4
$row->{"pk"} // 0
l!l
36210
$row->{"hidden"} // 0
l!l
36210
MC/DC0%0/1
$row->{"pk"} // 0
$row->{"pk"}
MC/DC0%0/1
$row->{"hidden"} // 0
$row->{"hidden"}
Truth table50%1/2
A
A= $row->{"pk"}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"hidden"}
Aresult
11
00
40 };
41 }
42  
43 # The extra PRAGMA is only worth running where the answer can change
44 # anything: a table with no key column has no alias to find.
45 1461 ▶ my $alias = (grep { $_->{is_pk} } @cols)
Branch: 1 branch
BranchTrueFalseTotal
grep({$_->{'is_pk'};} @cols) ? : 1310 151 1461
46 ? pk_is_rowid_alias($dbh, $table_name)
47 : 0;
48 $_->{not_null} = column_is_nullable($_->{not_null}, $_->{is_pk}, $alias) ? 0 : 1
49 1461 ▶ for @cols;
Branch: 1 branch
BranchTrueFalseTotal
column_is_nullable($_->{'not_null'}, $_->{'is_pk'}, $alias) ? : 1749 1872 3621
50  
51 # The rowid alias assigns its own keys: that is what is_auto_increment
52 # says, whether or not the AUTOINCREMENT keyword is there (it is not,
53 # ADR 0004). Only the single-column key of declared type INTEGER on a
54 # rowid table qualifies; a composite key, INT, or WITHOUT ROWID does not.
55 1461 ▶ if ($alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias) 1186 275 1461
56 1186 my @pk = grep { $_->{is_pk} } @cols;
57 $pk[0]{is_auto_increment} = 1
58 1186 ▶ if @pk == 1 && uc($pk[0]{data_type} // '') eq 'INTEGER';
Branch: 1 branch
BranchTrueFalseTotal
if @pk == 1 and uc($pk[0]{"data_type"} // "") eq "INTEGER" 1186 0 1186
Condition40%2/5
@pk == 1 and uc($pk[0]{"data_type"} // "") eq "INTEGER"
!ll&&!rl&&r
001186
$pk[0]{"data_type"} // ""
l!l
11860
MC/DC0%0/2
@pk == 1 and uc($pk[0]{"data_type"} // "") eq "INTEGER"
@pk == 1 $pk[0]{"data_type"}
Truth table33%1/3
A and B
A= @pk == 1B= $pk[0]{"data_type"}
ABresult
0X0
100
111
59 }
60  
61 1461 $columns{$table_name} = \@cols;
62 }
63  
64 755 return \%columns;
65 }
66  
67 757 ▶sub _has_xinfo {
Subroutine _has_xinfo: called CC=2 SCAR=7.0
68 757 my ($dbh) = @_;
69 757 my $ok = eval {
70 757 $dbh->selectrow_array(q{PRAGMA table_xinfo("sqlite_master")});
71 757 1;
72 };
73 757 ▶ return $ok ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 757 0 757
74 }
75  
76 193 1;
77  
78 __END__
« blib/lib/DBIO/SQLite/Test/Conformance.pm ↑ Summary blib/lib/DBIO/SQLite/Diff/Table.pm »