| 1 | package DBIO::SQLite::Introspect::Indexes; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect SQLite indexes | ||||||||||||||||||||||||||||||||||||||||||||||
| 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 DBIO::SQLite::Util qw(quote_identifier 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 = $dbh->selectall_arrayref( | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | sprintf('PRAGMA index_list(%s)', quote_identifier($table_name)), | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | { Slice => {} }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 1459 | my %t_idx; | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 1459 | for my $idx (@$list) { | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | my $info = $dbh->selectall_arrayref( | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 420 | sprintf('PRAGMA index_info(%s)', quote_identifier($idx->{name})), | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | { Slice => {} }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | my $sql = $sql_lookup->{ $idx->{name} } | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | ? $sql_lookup->{ $idx->{name} }{sql} | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 420 | ▶ | : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # The keyword, not the letters: an index, a table or a column may be | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # named "where", and stands in the statement between quotes then. | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | my $partial = ($sql && grep { | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 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'
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } sql_tokens($sql)) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | $t_idx{ $idx->{name} } = { | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | index_name => $idx->{name}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | is_unique => $idx->{unique} ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | columns => \@cols, | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | sql => $sql, | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | origin => $idx->{origin}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 420 | ▶ | partial => $partial, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 1459 | ▶ | $indexes{$table_name} = \%t_idx if %t_idx; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 755 | return \%indexes; | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 252 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||