| 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
| |||||||||||||||||||||||||||||
| 33 | 1602 | ▶ | my $sql = $row->{sql} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$row->{"sql"} // ""
MC/DC0%0/1
$row->{"sql"} // ""
$row->{"sql"}
Truth table50%1/2
A
A= $row->{"sql"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 71 | 42590 | my ($kind, $text) = @{ $tokens[$at] }; | |||||||||||||||||||||||||||
| 72 | 42590 | ▶ | next unless $kind eq 'punct'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 8830 | ▶ | $depth++ if $text eq '('; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 8830 | ▶ | $depth-- if $text eq ')'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 8830 | ▶ | last if $text eq ')' && $depth == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$text eq ")" and $depth == 0
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
| |||||||||||||||||||||||||||||
| 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'
$words[$_ + 1] // ""
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]
| |||||||||||||||||||||||||||||
| 81 | } 0 .. $#words; | ||||||||||||||||||||||||||||
| 82 | 1472 | my $strict = grep { $_ eq 'STRICT' } @words; | |||||||||||||||||||||||||||
| 83 | 1472 | ▶ | return ($without_rowid ? 1 : 0, $strict ? 1 : 0); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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'
MC/DC100%2/2
$_ ne 'main' && $_ ne 'temp'
$_ ne 'main'
$_ ne 'temp'
Truth table100%3/3
A && B
A= $_ ne 'main'B= $_ ne 'temp'
| |||||||||||||||||||||||||||||
| 93 | 761 | ▶ | map { $_->{name} // '' } @$list; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_->{"name"} // ""
MC/DC0%0/1
$_->{"name"} // ""
$_->{"name"}
Truth table50%1/2
A
A= $_->{"name"}
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 761 | ▶ | return unless @attached; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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"} // ""
MC/DC0%0/1
$tables->{$_}{"sql"} // ""
$tables->{$_}{"sql"}
Truth table50%1/2
A
A= $tables->{$_}{"sql"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition50%1/2
$entry->{"schema"} // ""
MC/DC0%0/1
$entry->{"schema"} // ""
$entry->{"schema"}
Truth table50%1/2
A
A= $entry->{"schema"}
| |||||||||||||||||||||||||||||
| 144 | 2266 | ▶ | next unless ($entry->{type} // '') eq 'shadow'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$entry->{"type"} // ""
MC/DC0%0/1
$entry->{"type"} // ""
$entry->{"type"}
Truth table50%1/2
A
A= $entry->{"type"}
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||