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

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