| 1 | package DBIO::MySQL::Introspect::Indexes; | ||||||||||||||
| 2 | # ABSTRACT: Introspect MySQL/MariaDB indexes | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 42 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 42 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 42 | our $VERSION = '0.900007'; | |||||||||||||
| 8 | |||||||||||||||
| 9 | 42 | ▶ | use DBIO::MySQL::Introspect::Util (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | 0 | ▶ | sub fetch { | ||||||||||||
|
Subroutine
fetch: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||
| 14 | 0 | my ($class, $dbh, $tables) = @_; | |||||||||||||
| 15 | 0 | my %indexes; | |||||||||||||
| 16 | |||||||||||||||
| 17 | # On MySQL 8 the information_schema result-column labels come back UPPERCASE | ||||||||||||||
| 18 | # regardless of the case written in the SELECT, while MariaDB returns them | ||||||||||||||
| 19 | # lowercase. Force lowercase hash keys for this fetch so the $row->{lc} reads | ||||||||||||||
| 20 | # below resolve on both engines. The sth captures FetchHashKeyName from the | ||||||||||||||
| 21 | # dbh at prepare time, so it must be set before prepare (Tables.pm sidesteps | ||||||||||||||
| 22 | # this by aliasing every column, which echoes the alias case verbatim). | ||||||||||||||
| 23 | 0 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; | |||||||||||||
| 24 | |||||||||||||||
| 25 | 0 | my $sth = $dbh->prepare(q{ | |||||||||||||
| 26 | SELECT | ||||||||||||||
| 27 | table_name, | ||||||||||||||
| 28 | index_name, | ||||||||||||||
| 29 | non_unique, | ||||||||||||||
| 30 | seq_in_index, | ||||||||||||||
| 31 | column_name, | ||||||||||||||
| 32 | index_type | ||||||||||||||
| 33 | FROM information_schema.statistics | ||||||||||||||
| 34 | WHERE table_schema = DATABASE() | ||||||||||||||
| 35 | ORDER BY table_name, index_name, seq_in_index | ||||||||||||||
| 36 | }); | ||||||||||||||
| 37 | 0 | $sth->execute; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | 0 | ▶ | next unless DBIO::MySQL::Introspect::Util->keep_table($tables, $row->{table_name}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | |||||||||||||||
| 42 | 0 | my $idx_name = $row->{index_name}; | |||||||||||||
| 43 | 0 | ▶ | my $is_unique = $row->{non_unique} ? 0 : 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | 0 | my $origin; | |||||||||||||
| 46 | 0 | ▶ | if ($idx_name eq 'PRIMARY') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 47 | 0 | $origin = 'pk'; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | elsif ($is_unique) { | ||||||||||||||
| 50 | 0 | $origin = 'u'; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | else { | ||||||||||||||
| 53 | 0 | $origin = 'c'; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | my $entry = $indexes{ $row->{table_name} }{$idx_name} //= { | ||||||||||||||
| 57 | index_name => $idx_name, | ||||||||||||||
| 58 | is_unique => $is_unique, | ||||||||||||||
| 59 | columns => [], | ||||||||||||||
| 60 | index_type => $row->{index_type}, | ||||||||||||||
| 61 | 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}
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}
| |||||||||||||||
| 62 | }; | ||||||||||||||
| 63 | 0 | $entry->{columns}[ $row->{seq_in_index} - 1 ] = $row->{column_name}; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 0 | return \%indexes; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 42 | 1; | |||||||||||||
| 70 | |||||||||||||||
| 71 | __END__ | ||||||||||||||