blib/lib/DBIO/DuckDB/Introspect/Indexes.pm

filter: Statementstmt 12.5% 8 / 64 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 2 / 4 Podpod 100% 1 / 1 totaltotal 8.8% 11 / 125 CC 27 SCAR 54.9
CC
27
Cov
7%
_index_elements
342.0
fetch
110.0
BEGIN
1.0
CRAP 241.9
↑ Summary blib/lib/DBIO/DuckDB/Diff/Column.pm »
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
BranchTrueFalseTotal
if ($quote) 0 0 0
21 0 ▶ $cur .= $ch if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 0 0 0
22 0 ▶ $quote = undef if $ch eq $quote;
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq $quote 0 0 0
23 0 next;
24 }
25 0 ▶ if ($ch eq '"' || $ch eq "'") { $quote = $ch; $cur .= $ch if $depth; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq "\"" or $ch eq "'") 0 0 0
if $depth 0 0 0
Condition0%0/3
$ch eq "\"" or $ch eq "'"
l!l&&r!l&&!r
000
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 "'"
ABresult
1X1
011
000
26 0 ▶ if ($ch eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "(") 0 0 0
27 0 $depth++;
28 0 ▶ $cur .= $ch if $depth > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 1 0 0 0
29 0 next;
30 }
31 0 ▶ if ($ch eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ")") 0 0 0
32 0 $depth--;
33 0 ▶ if ($depth == 0) { push @el, $cur if defined $cur; last }
Branch: 2 branches
BranchTrueFalseTotal
if ($depth == 0) 0 0 0
if defined $cur 0 0 0
34 0 $cur .= $ch;
35 0 next;
36 }
37 0 ▶ if ($ch eq ',' && $depth == 1) { push @el, $cur; $cur = ''; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "," and $depth == 1) 0 0 0
Condition0%0/3
$ch eq "," and $depth == 1
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
38 0 ▶ $cur .= $ch if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 0 0 0
39 }
40 0 for (@el) {
41 0 s/^\s+|\s+$//g;
42 0 ▶ s/^"((?:[^"]|"")*)"$/$1 =~ s{""}{"}gr/e unless /^\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\(/ 0 0 0
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"
l!l
00
MC/DC0%0/1
$schema //= "main"
$schema
Truth table0%0/2
A
A= $schema
Aresult
11
00
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
BranchTrueFalseTotal
if (defined $catalog) 0 0 0
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
BranchTrueFalseTotal
unless $ok 0 0 0
70  
71 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
72 0 ▶ next unless exists $tables->{ $row->{table_name} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$tables{$row->{"table_name"}} 0 0 0
73 0 ▶ next if $row->{is_primary}; # skip PK-backed auto indexes
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"is_primary"} 0 0 0
74  
75 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
76 0 my @cols = _index_elements($sql);
77  
78 0 ▶ my $partial = ($sql =~ /\bWHERE\b/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ /\bWHERE\b/i ? : 0 0 0
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
BranchTrueFalseTotal
$row->{'is_unique'} ? : 0 0 0
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__
↑ Summary blib/lib/DBIO/DuckDB/Diff/Column.pm »