DuckDB/Introspect/Indexes.pm

filter: Statementstmt 21.2% 7 / 33 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 2 / 3 Podpod 100% 1 / 1 totaltotal 16.9% 10 / 59 CC 12 SCAR 49.0
CC
12
Cov
13%
fetch
156.0
BEGIN
1.0
BEGIN
1.0
CRAP 133.9
« DuckDB/Introspect/Columns.pm ↑ Summary DuckDB/Diff/Column.pm »
1 package DBIO::DuckDB::Introspect::Indexes;
2 # ABSTRACT: Introspect DuckDB indexes
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8  
9 0 ▶sub fetch {
Subroutine fetch: not called CC=12 SCAR=50.5
Pod documented
10 0 my ($class, $dbh, $schema, $tables, $catalog) = @_;
11 0 ▶ $schema //= 'main';
Condition0%0/2
$schema //= "main"
l!l
00
MC/DC0%0/1
$schema //= "main"
$schema
Truth table0%0/2
A
A= $schema
Aresult
11
00
12 0 my %indexes;
13  
14 0 my $idx_sql = q{
15 SELECT table_name, index_name, is_unique, is_primary, sql
16 FROM duckdb_indexes()
17 WHERE schema_name = ?
18 };
19 0 my @idx_bind = ($schema);
20  
21 0 ▶ if (defined $catalog) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $catalog) 0 0 0
22 0 $idx_sql .= q{ AND database_name = ?};
23 0 push @idx_bind, $catalog;
24 }
25  
26 0 $idx_sql .= q{ ORDER BY table_name, index_name};
27  
28 0 my $sth = $dbh->prepare($idx_sql);
29  
30 0 my $ok = eval { $sth->execute(@idx_bind); 1 };
31 0 ▶ return \%indexes unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
32  
33 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
34 0 ▶ next unless exists $tables->{ $row->{table_name} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$tables{$row->{"table_name"}} 0 0 0
35 0 ▶ next if $row->{is_primary}; # skip PK-backed auto indexes
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"is_primary"} 0 0 0
36  
37 0 ▶ my $sql = $row->{sql} // '';
Condition0%0/2
$row->{"sql"} // ""
l!l
00
MC/DC0%0/1
$row->{"sql"} // ""
$row->{"sql"}
Truth table0%0/2
A
A= $row->{"sql"}
Aresult
11
00
38 0 my @cols;
39 0 ▶ if ($sql =~ /\(([^()]*)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /\(([^()]*)\)/) 0 0 0
40 0 @cols = map { s/^\s+|\s+$//gr } split /,/, $1;
41 0 s/^"(.*)"$/$1/ for @cols;
42 }
43  
44 0 ▶ my $partial = ($sql =~ /\bWHERE\b/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ /\bWHERE\b/i ? : 0 0 0
45  
46 $indexes{ $row->{table_name} }{ $row->{index_name} } = {
47 index_name => $row->{index_name},
48 0 ▶ is_unique => $row->{is_unique} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$row->{'is_unique'} ? : 0 0 0
49 columns => \@cols,
50 sql => $sql,
51 origin => 'c',
52 partial => $partial,
53 };
54 }
55  
56 0 return \%indexes;
57 }
58  
59 5 1;
60  
61 __END__
62  
63 =pod
64  
65 =encoding UTF-8
66  
67 =head1 NAME
68  
69 DBIO::DuckDB::Introspect::Indexes - Introspect DuckDB indexes
70  
71 =head1 VERSION
72  
73 version 0.900001
74  
75 =head1 DESCRIPTION
76  
77 Fetches index metadata via the C<duckdb_indexes()> system view. Each
78 entry captures C<index_name>, C<is_unique>, and the raw C<sql> of the
79 C<CREATE INDEX> statement. Column list is parsed out of the SQL when
80 possible.
81  
82 Auto-generated indexes backing PRIMARY KEY and UNIQUE constraints are
83 omitted -- they belong to the table, not to explicit CREATE INDEX.
84  
85 =head1 METHODS
86  
87 =head2 fetch
88  
89 my $indexes = DBIO::DuckDB::Introspect::Indexes->fetch($dbh, $schema, $tables);
90 my $indexes = DBIO::DuckDB::Introspect::Indexes->fetch($dbh, $schema, $tables, $catalog);
91  
92 Returns a hashref keyed by table name, each value a hashref keyed by
93 index name with: C<index_name>, C<is_unique>, C<columns> (arrayref),
94 C<sql>, C<origin>, C<partial>.
95  
96 When C<$catalog> is defined, C<AND database_name = ?> is added to the
97 C<duckdb_indexes()> query.
98  
99 =head1 AUTHOR
100  
101 DBIO Authors
102  
103 =head1 COPYRIGHT AND LICENSE
104  
105 Copyright (C) 2026 DBIO Authors
106  
107 This is free software; you can redistribute it and/or modify it under
108 the same terms as the Perl 5 programming language system itself.
109  
110 =cut
« DuckDB/Introspect/Columns.pm ↑ Summary DuckDB/Diff/Column.pm »