MySQL/Introspect/Indexes.pm

filter: Statementstmt 37.0% 10 / 27 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 31.1% 14 / 45 CC 7 SCAR 36.8
CC
7
Cov
26%
fetch
56.0
BEGIN
1.0
BEGIN
1.0
CRAP 39.5
« MySQL/SQLMaker/MariaDB.pm ↑ Summary MySQL/Introspect/Tables.pm »
1 package DBIO::MySQL::Introspect::Indexes;
2 # ABSTRACT: Introspect MySQL/MariaDB indexes
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use DBIO::MySQL::Introspect::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10  
11 0 ▶sub fetch {
Subroutine fetch: not called CC=7 SCAR=40.3
Pod documented
12 0 my ($class, $dbh, $tables) = @_;
13 0 my %indexes;
14  
15 # On MySQL 8 the information_schema result-column labels come back UPPERCASE
16 # regardless of the case written in the SELECT, while MariaDB returns them
17 # lowercase. Force lowercase hash keys for this fetch so the $row->{lc} reads
18 # below resolve on both engines. The sth captures FetchHashKeyName from the
19 # dbh at prepare time, so it must be set before prepare (Tables.pm sidesteps
20 # this by aliasing every column, which echoes the alias case verbatim).
21 0 local $dbh->{FetchHashKeyName} = 'NAME_lc';
22  
23 0 my $sth = $dbh->prepare(q{
24 SELECT
25 table_name,
26 index_name,
27 non_unique,
28 seq_in_index,
29 column_name,
30 index_type
31 FROM information_schema.statistics
32 WHERE table_schema = DATABASE()
33 ORDER BY table_name, index_name, seq_in_index
34 });
35 0 $sth->execute;
36  
37 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
38 0 ▶ next unless DBIO::MySQL::Introspect::Util->keep_table($tables, $row->{table_name});
Branch: 1 branch
BranchTrueFalseTotal
unless "DBIO::MySQL::Introspect::Util"->keep_table($tables, $row->{"table_name"}) 0 0 0
39  
40 0 my $idx_name = $row->{index_name};
41 0 ▶ my $is_unique = $row->{non_unique} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$row->{'non_unique'} ? : 0 0 0
42  
43 0 my $origin;
44 0 ▶ if ($idx_name eq 'PRIMARY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($idx_name eq 'PRIMARY') { } 0 0 0
elsif ($is_unique) { } 0 0 0
45 0 $origin = 'pk';
46 }
47 elsif ($is_unique) {
48 0 $origin = 'u';
49 }
50 else {
51 0 $origin = 'c';
52 }
53  
54 my $entry = $indexes{ $row->{table_name} }{$idx_name} //= {
55 index_name => $idx_name,
56 is_unique => $is_unique,
57 columns => [],
58 index_type => $row->{index_type},
59 0 ▶ origin => $origin,
Condition0%0/2
$indexes{$row->{"table_name"}}{$idx_name} //= {"index_name", $idx_name, "is_unique", $is_unique, "columns", [], "index_type", $row->{"index_type"}, "origin", $origin}
l!l
00
MC/DC0%0/1
$indexes{$row->{"table_name"}}{$idx_name} //= {"index_name", $idx_name, "is_unique", $is_unique, "columns", [], "index_type", $row->{"index_type"}, "origin", $origin}
$indexes{$row->{"table_name"}}{$idx_name}
Truth table0%0/2
A
A= $indexes{$row->{"table_name"}}{$idx_name}
Aresult
11
00
60 };
61 0 $entry->{columns}[ $row->{seq_in_index} - 1 ] = $row->{column_name};
62 }
63  
64 0 return \%indexes;
65 }
66  
67 4 1;
68  
69 __END__
70  
71 =pod
72  
73 =encoding UTF-8
74  
75 =head1 NAME
76  
77 DBIO::MySQL::Introspect::Indexes - Introspect MySQL/MariaDB indexes
78  
79 =head1 VERSION
80  
81 version 0.900001
82  
83 =head1 DESCRIPTION
84  
85 Fetches index metadata from C<information_schema.statistics>, scoped to the
86 current C<DATABASE()> and filtered to the tables surfaced by
87 L<DBIO::MySQL::Introspect::Tables>. Multi-column indexes are reassembled
88 from their per-column C<statistics> rows in C<seq_in_index> order.
89  
90 =head1 METHODS
91  
92 =head2 fetch
93  
94 my $indexes = DBIO::MySQL::Introspect::Indexes->fetch($dbh, $tables);
95  
96 Given the tables hashref from L<DBIO::MySQL::Introspect::Tables>, returns a
97 hashref keyed by table name. Each value is a hashref of index hashrefs keyed
98 by index name, with keys: C<index_name>, C<is_unique>, C<columns> (arrayref),
99 C<index_type>, C<origin> (C<pk> / C<u> / C<c>).
100  
101 =head1 AUTHOR
102  
103 DBIO & DBIx::Class Authors
104  
105 =head1 COPYRIGHT AND LICENSE
106  
107 Copyright (C) 2026 DBIO Authors
108 Portions Copyright (C) 2005-2025 DBIx::Class Authors
109 Based on DBIx::Class, heavily modified.
110  
111 This is free software; you can redistribute it and/or modify it under
112 the same terms as the Perl 5 programming language system itself.
113  
114 =cut
« MySQL/SQLMaker/MariaDB.pm ↑ Summary MySQL/Introspect/Tables.pm »