| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition50%2/4
$row->{"pk"} // 0
$row->{"hidden"} // 0
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"}
Truth table50%1/2
A
A= $row->{"hidden"}
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition40%2/5
@pk == 1 and uc($pk[0]{"data_type"} // "") eq "INTEGER"
$pk[0]{"data_type"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||
| 76 | 193 | 1; | |||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||||||||||||