| 1 | package DBIO::DB2::Introspect::Indexes; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect DB2 indexes | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 10 | 0 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||||||||||||||
| 11 | 0 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; # DBD::DB2 upper-cases hashref keys | |||||||||||||||||||||||||||||||
| 12 | 0 | my %indexes; | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 0 | my $sth = $dbh->prepare(q{ | |||||||||||||||||||||||||||||||
| 15 | SELECT indname, tabname, uniquerule, colcount | ||||||||||||||||||||||||||||||||
| 16 | FROM syscat.indexes | ||||||||||||||||||||||||||||||||
| 17 | WHERE indschema = ? | ||||||||||||||||||||||||||||||||
| 18 | ORDER BY tabname, indname | ||||||||||||||||||||||||||||||||
| 19 | }); | ||||||||||||||||||||||||||||||||
| 20 | 0 | $sth->execute($schema); | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | # SYSCAT.INDEXCOLUSE has no TABNAME, so map each index name back to its table | ||||||||||||||||||||||||||||||||
| 23 | # via the SYSCAT.INDEXES rows (index names are unique within a schema). | ||||||||||||||||||||||||||||||||
| 24 | 0 | my %index_table; | |||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | next unless exists $tables->{ $row->{tabname} }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | 0 | $index_table{ $row->{indname} } = $row->{tabname}; | |||||||||||||||||||||||||||||||
| 28 | # syscat.indexes.uniquerule: P = primary key, U = unique, D = duplicates | ||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | my $rule = uc($row->{uniquerule} // 'D'); | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$row->{"uniquerule"} // "D"
MC/DC0%0/1
$row->{"uniquerule"} // "D"
$row->{"uniquerule"}
Truth table0%0/2
A
A= $row->{"uniquerule"}
| |||||||||||||||||||||||||||||||||
| 30 | $indexes{ $row->{tabname} }{ $row->{indname} } = { | ||||||||||||||||||||||||||||||||
| 31 | index_name => $row->{indname}, | ||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | is_unique => ($rule eq 'P' || $rule eq 'U') ? 1 : 0, | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$rule eq "P" or $rule eq "U"
MC/DC0%0/2
$rule eq "P" or $rule eq "U"
$rule eq "P"
$rule eq "U"
Truth table0%0/3
A or B
A= $rule eq "P"B= $rule eq "U"
| |||||||||||||||||||||||||||||||||
| 33 | origin => ($rule eq 'P') ? 'pk' : undef, | ||||||||||||||||||||||||||||||||
| 34 | columns => [], | ||||||||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | # Resolve column names for each index via SYSCAT.INDEXCOLUSE. The catalog view | ||||||||||||||||||||||||||||||||
| 39 | # identifies an index column by INDSCHEMA + INDNAME only (no TABNAME), so the | ||||||||||||||||||||||||||||||||
| 40 | # owning table is recovered from the index->table map built above. COLSEQ | ||||||||||||||||||||||||||||||||
| 41 | # preserves the column order within the index. | ||||||||||||||||||||||||||||||||
| 42 | 0 | my $col_sth = $dbh->prepare(q{ | |||||||||||||||||||||||||||||||
| 43 | SELECT indname, colname, colseq | ||||||||||||||||||||||||||||||||
| 44 | FROM syscat.indexcoluse | ||||||||||||||||||||||||||||||||
| 45 | WHERE indschema = ? | ||||||||||||||||||||||||||||||||
| 46 | ORDER BY indname, colseq | ||||||||||||||||||||||||||||||||
| 47 | }); | ||||||||||||||||||||||||||||||||
| 48 | 0 | $col_sth->execute($schema); | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | while (my $row = $col_sth->fetchrow_hashref) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $tabname = $index_table{ $row->{indname} } or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | next unless exists $indexes{$tabname}{ $row->{indname} }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | 0 | push @{ $indexes{$tabname}{ $row->{indname} }{columns} }, $row->{colname}; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 0 | return \%indexes; | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 3 | 1; | |||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | __END__ | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | =pod | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | DBIO::DB2::Introspect::Indexes - Introspect DB2 indexes | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | version 0.900001 | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | Fetches index metadata via C<SYSCAT.INDEXES> and C<SYSCAT.INDEXCOLUSE>. | ||||||||||||||||||||||||||||||||
| 78 | Primary-key and unique constraint-backed indexes are included. | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | =head2 fetch | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | my $indexes = DBIO::DB2::Introspect::Indexes->fetch($dbh, $schema, $tables); | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | Returns a hashref keyed by table name, each value a hashref keyed by | ||||||||||||||||||||||||||||||||
| 87 | index name with: C<index_name>, C<is_unique>, C<columns> (arrayref), and | ||||||||||||||||||||||||||||||||
| 88 | C<origin> (C<'pk'> for the primary-key-backing index, else C<undef>). The | ||||||||||||||||||||||||||||||||
| 89 | C<origin> marker lets L<DBIO::Introspect::Base/table_uniq_info> drop the PK | ||||||||||||||||||||||||||||||||
| 90 | index from the unique-constraint list. | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||||||
| 99 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||||||
| 100 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 103 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||||||