| 1 | package DBIO::SQLite::Introspect::Indexes; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect SQLite indexes | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 197 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 197 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 26 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 26 | my ($class, $dbh, $tables) = @_; | |||||||||||||||||||||||||||
| 11 | 26 | my %indexes; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Lookup of CREATE INDEX statements from sqlite_master so we can detect | ||||||||||||||||||||||||||||
| 14 | # partial indexes and recover the original column expressions. | ||||||||||||||||||||||||||||
| 15 | 26 | my $sql_lookup = $dbh->selectall_hashref( | |||||||||||||||||||||||||||
| 16 | q{SELECT name, sql FROM sqlite_master WHERE type = 'index'}, | ||||||||||||||||||||||||||||
| 17 | 'name', | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 26 | for my $table_name (sort keys %$tables) { | |||||||||||||||||||||||||||
| 21 | 40 | my $list = $dbh->selectall_arrayref( | |||||||||||||||||||||||||||
| 22 | qq{PRAGMA index_list("$table_name")}, { Slice => {} } | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 40 | my %t_idx; | |||||||||||||||||||||||||||
| 26 | 40 | for my $idx (@$list) { | |||||||||||||||||||||||||||
| 27 | 12 | my $info = $dbh->selectall_arrayref( | |||||||||||||||||||||||||||
| 28 | qq{PRAGMA index_info("$idx->{name}")}, { Slice => {} } | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | 12 | ▶ | my @cols = map { $_->{name} // '' } sort { $a->{seqno} <=> $b->{seqno} } @$info; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_->{"name"} // ""
MC/DC0%0/1
$_->{"name"} // ""
$_->{"name"}
Truth table50%1/2
A
A= $_->{"name"}
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | my $sql = $sql_lookup->{ $idx->{name} } | ||||||||||||||||||||||||||||
| 33 | ? $sql_lookup->{ $idx->{name} }{sql} | ||||||||||||||||||||||||||||
| 34 | 12 | ▶ | : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 12 | ▶ | my $partial = ($sql && $sql =~ /\bWHERE\b/i) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$sql and $sql =~ /\bWHERE\b/i
MC/DC100%2/2
$sql and $sql =~ /\bWHERE\b/i
$sql
$sql =~ /\bWHERE\b/i
Truth table100%3/3
A and B
A= $sqlB= $sql =~ /\bWHERE\b/i
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | $t_idx{ $idx->{name} } = { | ||||||||||||||||||||||||||||
| 39 | index_name => $idx->{name}, | ||||||||||||||||||||||||||||
| 40 | is_unique => $idx->{unique} ? 1 : 0, | ||||||||||||||||||||||||||||
| 41 | columns => \@cols, | ||||||||||||||||||||||||||||
| 42 | sql => $sql, | ||||||||||||||||||||||||||||
| 43 | origin => $idx->{origin}, | ||||||||||||||||||||||||||||
| 44 | 12 | ▶ | partial => $partial, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 40 | ▶ | $indexes{$table_name} = \%t_idx if %t_idx; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 26 | return \%indexes; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 197 | 1; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | __END__ | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =pod | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head1 NAME | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | DBIO::SQLite::Introspect::Indexes - Introspect SQLite indexes | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 VERSION | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | version 0.900001 | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Fetches index metadata via C<PRAGMA index_list()> and C<PRAGMA | ||||||||||||||||||||||||||||
| 72 | index_info()>, plus the original C<CREATE INDEX> statement from | ||||||||||||||||||||||||||||
| 73 | C<sqlite_master> for partial / expression indexes. Skips | ||||||||||||||||||||||||||||
| 74 | auto-generated indexes (PRIMARY KEY, UNIQUE constraint indexes). | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 METHODS | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 fetch | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | my $indexes = DBIO::SQLite::Introspect::Indexes->fetch($dbh, $tables); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Returns a hashref keyed by table name. Each value is a hashref keyed by | ||||||||||||||||||||||||||||
| 83 | index name. Each index entry has: C<index_name>, C<is_unique>, | ||||||||||||||||||||||||||||
| 84 | C<columns> (arrayref), C<sql> (CREATE statement, may be undef for | ||||||||||||||||||||||||||||
| 85 | auto-generated UNIQUE indexes), C<origin> (C<c>=CREATE, C<u>=UNIQUE, | ||||||||||||||||||||||||||||
| 86 | C<pk>=PRIMARY KEY), C<partial> (1 if WHERE clause present). | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 95 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 96 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 99 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||||||||||