| 1 | package DBIO::DuckDB::Introspect::Indexes; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect DuckDB indexes | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 10 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | # The elements of the first parenthesised list of a CREATE INDEX statement, | ||||||||||||||||||||||||||||||||
| 12 | # split at top-level commas. Double-quoted identifiers and single-quoted | ||||||||||||||||||||||||||||||||
| 13 | # literals are skipped, so a comma or parenthesis in one does not count. | ||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub _index_elements { | ||||||||||||||||||||||||||||||
|
Subroutine
_index_elements: not called CC=18 SCAR=58.3
| |||||||||||||||||||||||||||||||||
| 15 | 0 | my ($sql) = @_; | |||||||||||||||||||||||||||||||
| 16 | 0 | my ($depth, $start, $quote, @el, $cur) = (0); | |||||||||||||||||||||||||||||||
| 17 | 0 | my @c = split //, $sql; | |||||||||||||||||||||||||||||||
| 18 | 0 | for my $i (0 .. $#c) { | |||||||||||||||||||||||||||||||
| 19 | 0 | my $ch = $c[$i]; | |||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | if ($quote) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | $cur .= $ch if $depth; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | $quote = undef if $ch eq $quote; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 23 | 0 | next; | |||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | if ($ch eq '"' || $ch eq "'") { $quote = $ch; $cur .= $ch if $depth; next } | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$ch eq "\"" or $ch eq "'"
MC/DC0%0/2
$ch eq "\"" or $ch eq "'"
$ch eq "\""
$ch eq "'"
Truth table0%0/3
A or B
A= $ch eq "\""B= $ch eq "'"
| |||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | if ($ch eq '(') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | 0 | $depth++; | |||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | $cur .= $ch if $depth > 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | 0 | next; | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($ch eq ')') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 32 | 0 | $depth--; | |||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ($depth == 0) { push @el, $cur if defined $cur; last } | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 34 | 0 | $cur .= $ch; | |||||||||||||||||||||||||||||||
| 35 | 0 | next; | |||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if ($ch eq ',' && $depth == 1) { push @el, $cur; $cur = ''; next } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ch eq "," and $depth == 1
MC/DC0%0/2
$ch eq "," and $depth == 1
$ch eq ","
$depth == 1
Truth table0%0/3
A and B
A= $ch eq ","B= $depth == 1
| |||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | $cur .= $ch if $depth; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | 0 | for (@el) { | |||||||||||||||||||||||||||||||
| 41 | 0 | s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | s/^"((?:[^"]|"")*)"$/$1 =~ s{""}{"}gr/e unless /^\(/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | 0 | return @el; | |||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 48 | 0 | my ($class, $dbh, $schema, $tables, $catalog) = @_; | |||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $schema //= 'main'; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$schema //= "main"
MC/DC0%0/1
$schema //= "main"
$schema
Truth table0%0/2
A
A= $schema
| |||||||||||||||||||||||||||||||||
| 50 | 0 | my %indexes; | |||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 0 | my $idx_sql = q{ | |||||||||||||||||||||||||||||||
| 53 | SELECT table_name, index_name, is_unique, is_primary, sql | ||||||||||||||||||||||||||||||||
| 54 | FROM duckdb_indexes() | ||||||||||||||||||||||||||||||||
| 55 | WHERE schema_name = ? | ||||||||||||||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||||||||||||||
| 57 | 0 | my @idx_bind = ($schema); | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if (defined $catalog) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | 0 | $idx_sql .= q{ AND database_name = ?}; | |||||||||||||||||||||||||||||||
| 61 | 0 | push @idx_bind, $catalog; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 0 | $idx_sql .= q{ ORDER BY table_name, index_name}; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 0 | my $sth = $dbh->prepare($idx_sql); | |||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 0 | my $ok = eval { $sth->execute(@idx_bind); 1 }; | |||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | return \%indexes unless $ok; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | next unless exists $tables->{ $row->{table_name} }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | next if $row->{is_primary}; # skip PK-backed auto indexes | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $sql = $row->{sql} // ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$row->{"sql"} // ""
MC/DC0%0/1
$row->{"sql"} // ""
$row->{"sql"}
Truth table0%0/2
A
A= $row->{"sql"}
| |||||||||||||||||||||||||||||||||
| 76 | 0 | my @cols = _index_elements($sql); | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $partial = ($sql =~ /\bWHERE\b/i) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | $indexes{ $row->{table_name} }{ $row->{index_name} } = { | ||||||||||||||||||||||||||||||||
| 81 | index_name => $row->{index_name}, | ||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | is_unique => $row->{is_unique} ? 1 : 0, | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | columns => \@cols, | ||||||||||||||||||||||||||||||||
| 84 | sql => $sql, | ||||||||||||||||||||||||||||||||
| 85 | origin => 'c', | ||||||||||||||||||||||||||||||||
| 86 | partial => $partial, | ||||||||||||||||||||||||||||||||
| 87 | }; | ||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 0 | return \%indexes; | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 10 | 1; | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | __END__ | ||||||||||||||||||||||||||||||||