| 1 | package DBIO::SQLite::Introspect::Indexes; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect SQLite indexes | ||||||||||||||||||||||||||||||||||||||||||||||
| 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(quote_identifier selectall_hashrefs sql_tokens); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 755 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 755 | my ($class, $dbh, $tables) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | 755 | my %indexes; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # Lookup of CREATE INDEX statements from sqlite_master so we can detect | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # partial indexes and recover the original column expressions. | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 755 | my $sql_lookup = $dbh->selectall_hashref( | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | q{SELECT name, sql FROM sqlite_master WHERE type = 'index'}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 'name', | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 755 | for my $table_name (sort keys %$tables) { | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1459 | my $list = selectall_hashrefs($dbh, | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | sprintf('PRAGMA index_list(%s)', quote_identifier($table_name))); | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1459 | my %t_idx; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | 1459 | for my $idx (@$list) { | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | my $info = selectall_hashrefs($dbh, | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 420 | sprintf('PRAGMA index_info(%s)', quote_identifier($idx->{name}))); | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 420 | ▶ | my @cols = map { $_->{name} // '' } sort { $a->{seqno} <=> $b->{seqno} } @$info; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_->{"name"} // ""
MC/DC100%1/1
$_->{"name"} // ""
$_->{"name"}
Truth table100%2/2
A
A= $_->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | my $sql = $sql_lookup->{ $idx->{name} } | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ? $sql_lookup->{ $idx->{name} }{sql} | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 420 | ▶ | : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # The keyword, not the letters: an index, a table or a column may be | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # named "where", and stands in the statement between quotes then. | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | my $partial = ($sql && grep { | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 420 | ▶ | $_->[0] eq 'word' && uc $_->[1] eq 'WHERE' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$sql and grep {$_->[0] eq "word" and uc $_->[1] eq "WHERE";} sql_tokens($sql)
$_->[0] eq 'word' && uc $_->[1] eq 'WHERE'
MC/DC100%2/2
$sql and grep {$_->[0] eq "word" and uc $_->[1] eq "WHERE";} sql_tokens($sql)
$sql
grep {$_->[0] eq "word" and uc $_->[1] eq "WHERE";} sql_tokens($sql)
MC/DC100%2/2
$_->[0] eq 'word' && uc $_->[1] eq 'WHERE'
$_->[0] eq 'word'
uc $_->[1] eq 'WHERE'
Truth table100%3/3
A and B
A= $sqlB= grep {$_->[0] eq "word" and uc $_->[1] eq "WHERE";} sql_tokens($sql)
Truth table100%3/3
A && B
A= $_->[0] eq 'word'B= uc $_->[1] eq 'WHERE'
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } sql_tokens($sql)) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | $t_idx{ $idx->{name} } = { | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | index_name => $idx->{name}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | is_unique => $idx->{unique} ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | columns => \@cols, | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | sql => $sql, | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | origin => $idx->{origin}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 420 | ▶ | partial => $partial, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 1459 | ▶ | $indexes{$table_name} = \%t_idx if %t_idx; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 755 | return \%indexes; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 193 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||