SQLite/Introspect/Tables.pm

filter: Statementstmt 100% 71 / 71 Branchbran 96.4% 27 / 28 Conditioncond 61.2% 19 / 31 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 84.6% 132 / 156 CC 30 SCAR 20.7
CC
30
Cov
90%
_table_options
12.0
_drop_shadow_tables
9.1
_libsqlite_version
7.6
CRAP 7.9
« SQLite/Diff/View.pm ↑ Summary SQLite/Introspect/Indexes.pm »
1 package DBIO::SQLite::Introspect::Tables;
2 # ABSTRACT: Introspect SQLite tables and views
3  
4 252 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 252 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 252 our $VERSION = '0.900004';
8  
9 252 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 252 ▶use DBIO::SQLite::Util qw(create_name_index create_table_name_index sql_tokens);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13  
14 761 ▶sub fetch {
Subroutine fetch: called CC=3 SCAR=11.0
Pod documented
15 761 my ($class, $dbh) = @_;
16  
17 761 $class->_refuse_attached_databases($dbh);
18  
19 760 my $sth = $dbh->prepare(q{
20 SELECT name, type, sql
21 FROM sqlite_master
22 WHERE type IN ('table', 'view')
23 AND name NOT LIKE 'sqlite_%'
24 ORDER BY name
25 });
26 760 $sth->execute;
27  
28 760 my %tables;
29 760 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 1602 760 2362
30 1602 ▶ my $sql = $row->{sql} // '';
Condition50%1/2
$row->{"sql"} // ""
l!l
16020
MC/DC0%0/1
$row->{"sql"} // ""
$row->{"sql"}
Truth table50%1/2
A
A= $row->{"sql"}
Aresult
11
00
31  
32 1602 my ($without_rowid, $strict) = _table_options($sql);
33  
34 $tables{ $row->{name} } = {
35 table_name => $row->{name},
36 kind => $row->{type},
37 1602 sql => $sql,
38 without_rowid => $without_rowid,
39 strict => $strict,
40 };
41 }
42  
43 760 $class->_drop_shadow_tables($dbh, \%tables);
44  
45 757 return \%tables;
46 }
47  
48 # (without_rowid, strict) of a stored statement, each 1 or 0: the two options
49 # a CREATE TABLE may carry behind the closing parenthesis of its column list,
50 # in either order and any letter case -- `) WITHOUT ROWID, STRICT`. They are
51 # read there, with the driver's lexer, and nowhere else: a column named
52 # "strict", a default 'WITHOUT ROWID' or a view that selects such a string is
53 # not an option. A view and a virtual table have neither.
54 #
55 # Two regular expressions over the whole statement used to do this. The one
56 # for STRICT wanted it directly behind the parenthesis and at the end, and so
57 # missed it beside WITHOUT ROWID in both orders; the one for WITHOUT ROWID
58 # found the words anywhere, in a string or a quoted name as well (karr k72).
59 1602 ▶sub _table_options {
Subroutine _table_options: called CC=12 SCAR=24.9
60 1602 my ($sql) = @_;
61 1602 my @tokens = sql_tokens($sql);
62 1602 my $at = create_table_name_index(\@tokens);
63 1602 ▶ return (0, 0) unless defined $at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at 130 1472 1602
64  
65 # Behind the name: the column list, to its own closing parenthesis.
66 1472 my $depth = 0;
67 1472 ▶ for ($at++; $at <= $#tokens; $at++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at <= $#tokens) 42590 0 42590
68 42590 my ($kind, $text) = @{ $tokens[$at] };
69 42590 ▶ next unless $kind eq 'punct';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "punct" 33760 8830 42590
70 8830 ▶ $depth++ if $text eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "(" 2935 5895 8830
71 8830 ▶ $depth-- if $text eq ')';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq ")" 2935 5895 8830
72 8830 ▶ last if $text eq ')' && $depth == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $text eq ")" and $depth == 0 1472 7358 8830
Condition100%3/3
$text eq ")" and $depth == 0
!ll&&!rl&&r
589514631472
MC/DC100%2/2
$text eq ")" and $depth == 0
$text eq ")" $depth == 0
Truth table100%3/3
A and B
A= $text eq ")"B= $depth == 0
ABresult
0X0
100
111
73 }
74  
75 1472 my @words = map { uc $_->[1] } grep { $_->[0] eq 'word' } @tokens[ $at + 1 .. $#tokens ];
76 my $without_rowid = grep {
77 1472 ▶ $words[$_] eq 'WITHOUT' && ($words[ $_ + 1 ] // '') eq 'ROWID'
Condition60%3/5
$words[$_] eq 'WITHOUT' && ($words[$_ + 1] // '') eq 'ROWID'
!ll&&!rl&&r
93082
$words[$_ + 1] // ""
l!l
820
MC/DC50%1/2
$words[$_] eq 'WITHOUT' && ($words[$_ + 1] // '') eq 'ROWID'
$words[$_] eq 'WITHOUT' $words[$_ + 1]
Truth table66%2/3
A && B
A= $words[$_] eq 'WITHOUT'B= $words[$_ + 1]
ABresult
0X0
100
111
78 } 0 .. $#words;
79 1472 my $strict = grep { $_ eq 'STRICT' } @words;
80 1472 ▶ return ($without_rowid ? 1 : 0, $strict ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$without_rowid ? : 82 1390 1472
$strict ? : 11 1461 1472
81 }
82  
83  
84 761 ▶sub _refuse_attached_databases {
Subroutine _refuse_attached_databases: called CC=4 SCAR=13.9
85 761 my ($class, $dbh) = @_;
86  
87 761 my $list = $dbh->selectall_arrayref('PRAGMA database_list', { Slice => {} });
88 my @attached =
89 851 ▶ grep { $_ ne 'main' && $_ ne 'temp' }
Condition100%3/3
$_ ne 'main' && $_ ne 'temp'
!ll&&!rl&&r
761891
MC/DC100%2/2
$_ ne 'main' && $_ ne 'temp'
$_ ne 'main' $_ ne 'temp'
Truth table100%3/3
A && B
A= $_ ne 'main'B= $_ ne 'temp'
ABresult
0X0
100
111
90 761 ▶ map { $_->{name} // '' } @$list;
Condition50%1/2
$_->{"name"} // ""
l!l
8510
MC/DC0%0/1
$_->{"name"} // ""
$_->{"name"}
Truth table50%1/2
A
A= $_->{"name"}
Aresult
11
00
91  
92 761 ▶ return unless @attached;
Branch: 1 branch
BranchTrueFalseTotal
unless @attached 760 1 761
93  
94 Carp::croak(
95 'refusing to introspect: this SQLite handle has attached database(s) '
96 1 . join(', ', map { "'$_'" } @attached)
97 . ". Introspection reads only main's sqlite_master, so every table in an "
98 . 'attached database is invisible here. A deploy diff would then read those '
99 . 'tables as missing and emit CREATE TABLE for them in main, shadowing the '
100 . 'real, still-populated attached tables with fresh empty ones in main -- '
101 . 'silent data loss. Introspect a handle without attached databases.'
102 );
103 }
104  
105 # Remove the shadow tables of virtual tables from an already-collected
106 # sqlite_master result. Restricted to schema 'main' on purpose: sqlite_master
107 # without a schema prefix is main's, so anything table_list reports for an
108 # ATTACHed database describes names this fetch never collected.
109 752 ▶sub _drop_shadow_tables {
Subroutine _drop_shadow_tables: called CC=9 SCAR=22.1
110 752 my ($class, $dbh, $tables) = @_;
111  
112 752 my ($version, $numeric) = _libsqlite_version($dbh);
113  
114 752 ▶ if ($numeric < 3_037_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numeric < 3037000) 9 743 752
115 # A virtual table is known by how its stored statement begins, read with
116 # the driver's lexer as DBIO::SQLite::Diff reads it. The words anywhere
117 # else -- in a default, a comment, a view's SELECT -- make none, and no
118 # refusal (karr k53).
119 my ($virtual) = sort grep {
120 9 ▶ my @tokens = sql_tokens($tables->{$_}{sql} // '');
Condition50%1/2
$tables->{$_}{"sql"} // ""
l!l
390
MC/DC0%0/1
$tables->{$_}{"sql"} // ""
$tables->{$_}{"sql"}
Truth table50%1/2
A
A= $tables->{$_}{"sql"}
Aresult
11
00
121 39 defined create_name_index(\@tokens, 'VIRTUAL', 'TABLE');
122 } keys %$tables;
123  
124 # No virtual table, no shadow tables, nothing to classify.
125 9 ▶ return unless defined $virtual;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virtual 6 3 9
126  
127 3 Carp::croak(
128 "cannot introspect virtual table '$virtual' on libsqlite $version: "
129 . 'identifying its shadow tables needs PRAGMA table_list (libsqlite '
130 . '3.37+). Without it the shadow tables could only be guessed from '
131 . 'their names, and a wrong guess is worse than no answer: SQLite drops '
132 . 'the shadow tables together with the virtual table, so every '
133 . 'additional DROP TABLE the diff emits fails with "no such table" and '
134 . 'the non-transactional DDL run aborts partway through'
135 );
136 }
137  
138 743 my $list = $dbh->selectall_arrayref('PRAGMA table_list', { Slice => {} });
139 743 for my $entry (@$list) {
140 3010 ▶ next unless ($entry->{schema} // '') eq 'main';
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry->{"schema"} // "") eq "main" 744 2266 3010
Condition50%1/2
$entry->{"schema"} // ""
l!l
30100
MC/DC0%0/1
$entry->{"schema"} // ""
$entry->{"schema"}
Truth table50%1/2
A
A= $entry->{"schema"}
Aresult
11
00
141 2266 ▶ next unless ($entry->{type} // '') eq 'shadow';
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry->{"type"} // "") eq "shadow" 2148 118 2266
Condition50%1/2
$entry->{"type"} // ""
l!l
22660
MC/DC0%0/1
$entry->{"type"} // ""
$entry->{"type"}
Truth table50%1/2
A
A= $entry->{"type"}
Aresult
11
00
142 118 delete $tables->{ $entry->{name} };
143 }
144  
145 743 return;
146 }
147  
148 # Returns the libsqlite version as ($string, $comparable_integer). Compared
149 # numerically per component, never as text: "3.9" lt "3.37" is true as a
150 # string and false as a version.
151 752 ▶sub _libsqlite_version {
Subroutine _libsqlite_version: called CC=6 SCAR=20.3
152 752 my ($dbh) = @_;
153 752 my ($version) = $dbh->selectrow_array('SELECT sqlite_version()');
154 752 ▶ my @part = split /\./, ($version // '0');
Condition50%1/2
$version // 0
l!l
7520
MC/DC0%0/1
$version // 0
$version
Truth table50%1/2
A
A= $version
Aresult
11
00
155 return (
156 752 ▶ $version // '(unknown)',
Condition50%4/8
$version // "(unknown)"
l!l
7520
$part[0] // 0
l!l
7520
$part[1] // 0
l!l
7520
$part[2] // 0
l!l
7520
MC/DC0%0/1
$version // "(unknown)"
$version
MC/DC0%0/1
$part[0] // 0
$part[0]
MC/DC0%0/1
$part[1] // 0
$part[1]
MC/DC0%0/1
$part[2] // 0
$part[2]
Truth table50%1/2
A
A= $version
Aresult
11
00
Truth table50%1/2
A
A= $part[0]
Aresult
11
00
Truth table50%1/2
A
A= $part[1]
Aresult
11
00
Truth table50%1/2
A
A= $part[2]
Aresult
11
00
157 ($part[0] // 0) * 1_000_000 + ($part[1] // 0) * 1_000 + ($part[2] // 0),
158 );
159 }
160  
161 252 1;
162  
163 __END__
« SQLite/Diff/View.pm ↑ Summary SQLite/Introspect/Indexes.pm »