| 1 | package DBIO::DB2::Introspect::Indexes; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect DB2 indexes | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 37 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 37 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 37 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | # An index is identified by schema and name: the one behind a primary key is in SYSIBM. | ||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | sub _key { | ||||||||||||||||||||||||||||||
|
Subroutine
_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 5 | my ($row) = @_; | |||||||||||||||||||||||||||||||
| 13 | 5 | return join "\0", map { s/\s+\z//r } @{$row}{qw(indschema indname)}; | |||||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 18 | 2 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||||||||||||||
| 19 | 2 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; # DBD::DB2 upper-cases hashref keys | |||||||||||||||||||||||||||||||
| 20 | 2 | my %indexes; | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 2 | my $sth = $dbh->prepare(q{ | |||||||||||||||||||||||||||||||
| 23 | SELECT i.indschema, i.indname, i.tabname, i.uniquerule, i.colcount | ||||||||||||||||||||||||||||||||
| 24 | FROM syscat.indexes i | ||||||||||||||||||||||||||||||||
| 25 | WHERE (i.indschema = ? | ||||||||||||||||||||||||||||||||
| 26 | OR (i.indschema = 'SYSIBM' AND i.uniquerule = 'P' AND i.tabschema = ?)) | ||||||||||||||||||||||||||||||||
| 27 | AND NOT EXISTS ( | ||||||||||||||||||||||||||||||||
| 28 | SELECT 1 FROM syscat.tabconst c | ||||||||||||||||||||||||||||||||
| 29 | WHERE c.tabschema = i.tabschema AND c.tabname = i.tabname | ||||||||||||||||||||||||||||||||
| 30 | AND c.constname = i.indname AND c.type = 'U') | ||||||||||||||||||||||||||||||||
| 31 | ORDER BY i.tabname, i.indname | ||||||||||||||||||||||||||||||||
| 32 | }); | ||||||||||||||||||||||||||||||||
| 33 | 2 | $sth->execute($schema, $schema); | |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | # SYSCAT.INDEXCOLUSE has no TABNAME, so map each index back to its table via | ||||||||||||||||||||||||||||||||
| 36 | # the SYSCAT.INDEXES rows (index names are unique within a schema, so the key | ||||||||||||||||||||||||||||||||
| 37 | # is schema and name: the primary key's index is in SYSIBM). | ||||||||||||||||||||||||||||||||
| 38 | 2 | my %index_table; | |||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | next unless exists $tables->{ $row->{tabname} }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | 2 | $index_table{ _key($row) } = $row->{tabname}; | |||||||||||||||||||||||||||||||
| 42 | # syscat.indexes.uniquerule: P = primary key, U = unique, D = duplicates | ||||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | my $rule = uc($row->{uniquerule} // 'D'); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$row->{"uniquerule"} // "D"
MC/DC0%0/1
$row->{"uniquerule"} // "D"
$row->{"uniquerule"}
Truth table50%1/2
A
A= $row->{"uniquerule"}
| |||||||||||||||||||||||||||||||||
| 44 | $indexes{ $row->{tabname} }{ $row->{indname} } = { | ||||||||||||||||||||||||||||||||
| 45 | index_name => $row->{indname}, | ||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | is_unique => ($rule eq 'P' || $rule eq 'U') ? 1 : 0, | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$rule eq "P" or $rule eq "U"
MC/DC50%1/2
$rule eq "P" or $rule eq "U"
$rule eq "P"
$rule eq "U"
Truth table66%2/3
A or B
A= $rule eq "P"B= $rule eq "U"
| |||||||||||||||||||||||||||||||||
| 47 | origin => ($rule eq 'P') ? 'pk' : undef, | ||||||||||||||||||||||||||||||||
| 48 | columns => [], | ||||||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | # Resolve column names for each index via SYSCAT.INDEXCOLUSE. The catalog view | ||||||||||||||||||||||||||||||||
| 53 | # identifies an index column by INDSCHEMA + INDNAME only (no TABNAME), so the | ||||||||||||||||||||||||||||||||
| 54 | # owning table is recovered from the index->table map built above. COLSEQ | ||||||||||||||||||||||||||||||||
| 55 | # preserves the column order within the index. | ||||||||||||||||||||||||||||||||
| 56 | 2 | my $col_sth = $dbh->prepare(q{ | |||||||||||||||||||||||||||||||
| 57 | SELECT indschema, indname, colname, colseq | ||||||||||||||||||||||||||||||||
| 58 | FROM syscat.indexcoluse | ||||||||||||||||||||||||||||||||
| 59 | WHERE indschema = ? | ||||||||||||||||||||||||||||||||
| 60 | OR (indschema = 'SYSIBM' AND indname IN ( | ||||||||||||||||||||||||||||||||
| 61 | SELECT indname FROM syscat.indexes | ||||||||||||||||||||||||||||||||
| 62 | WHERE indschema = 'SYSIBM' AND uniquerule = 'P' AND tabschema = ?)) | ||||||||||||||||||||||||||||||||
| 63 | ORDER BY indschema, indname, colseq | ||||||||||||||||||||||||||||||||
| 64 | }); | ||||||||||||||||||||||||||||||||
| 65 | 2 | $col_sth->execute($schema, $schema); | |||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | while (my $row = $col_sth->fetchrow_hashref) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | 3 | ▶ | my $tabname = $index_table{ _key($row) } or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | next unless exists $indexes{$tabname}{ $row->{indname} }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 2 | push @{ $indexes{$tabname}{ $row->{indname} }{columns} }, $row->{colname}; | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 2 | return \%indexes; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 37 | 1; | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||||||