| 1 | package DBIO::DB2::Introspect::ForeignKeys; | ||||||||||||||||||||
| 2 | # ABSTRACT: Introspect DB2 foreign keys | ||||||||||||||||||||
| 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 | 37 | ▶ | use DBIO::Introspect::Base (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | # SYSCAT.REFERENCES stores DELETERULE/UPDATERULE as single-char codes. Normalize | ||||||||||||||||||||
| 12 | # them to the SQL keywords the model carries everywhere else, so the FK clause | ||||||||||||||||||||
| 13 | # emitter (DBIO::DB2::DDL::_fk_constraint_clause) renders e.g. ON DELETE CASCADE | ||||||||||||||||||||
| 14 | # (not ON DELETE C) and a rule comparison against install_ddl-shaped FKs matches. | ||||||||||||||||||||
| 15 | 37 | my %RULE = ( | |||||||||||||||||||
| 16 | A => 'NO ACTION', | ||||||||||||||||||||
| 17 | C => 'CASCADE', | ||||||||||||||||||||
| 18 | N => 'SET NULL', | ||||||||||||||||||||
| 19 | R => 'RESTRICT', | ||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 0 | ▶ | sub _rule { | ||||||||||||||||||
|
Subroutine
_rule: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 23 | 0 | my ($code) = @_; | |||||||||||||||||||
| 24 | 0 | ▶ | return undef unless defined $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 0 | $code =~ s/\s+$//; | |||||||||||||||||||
| 26 | 0 | ▶ | return $RULE{ uc $code } // $code; | ||||||||||||||||||
|
Condition0%0/3
$RULE{uc $code} // $code
MC/DC0%0/2
$RULE{uc $code} // $code
$RULE{uc $code}
$code
Truth table0%0/3
A // B
A= $RULE{uc $code}B= $code
| |||||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 0 | ▶ | sub fetch { | ||||||||||||||||||
|
Subroutine
fetch: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 32 | 0 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||
| 33 | 0 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; # DBD::DB2 upper-cases hashref keys | |||||||||||||||||||
| 34 | 0 | my %fks; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 0 | my $sth = $dbh->prepare(q{ | |||||||||||||||||||
| 37 | SELECT tc.constname, tc.tabname, | ||||||||||||||||||||
| 38 | kcu.colname, kcu.colseq, | ||||||||||||||||||||
| 39 | sr.reftabname, sr.reftabschema, sr.refkeyname, | ||||||||||||||||||||
| 40 | sr.deleterule, sr.updaterule | ||||||||||||||||||||
| 41 | FROM syscat.tabconst tc | ||||||||||||||||||||
| 42 | JOIN syscat.keycoluse kcu | ||||||||||||||||||||
| 43 | ON tc.constname = kcu.constname | ||||||||||||||||||||
| 44 | AND tc.tabschema = kcu.tabschema | ||||||||||||||||||||
| 45 | AND tc.tabname = kcu.tabname | ||||||||||||||||||||
| 46 | JOIN syscat.references sr | ||||||||||||||||||||
| 47 | ON tc.constname = sr.constname | ||||||||||||||||||||
| 48 | AND tc.tabschema = sr.tabschema | ||||||||||||||||||||
| 49 | AND tc.tabname = sr.tabname | ||||||||||||||||||||
| 50 | WHERE tc.tabschema = ? | ||||||||||||||||||||
| 51 | AND tc.type = 'F' | ||||||||||||||||||||
| 52 | ORDER BY tc.tabname, tc.constname, kcu.colseq | ||||||||||||||||||||
| 53 | }); | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | my $ok = eval { $sth->execute($schema); 1 }; | |||||||||||||||||||
| 56 | 0 | ▶ | return \%fks unless $ok; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # Collect the flat rows (one per FK column, in colseq order) and group them by | ||||||||||||||||||||
| 59 | # constraint via the core ordered aggregator, which preserves column order | ||||||||||||||||||||
| 60 | # within each FK. | ||||||||||||||||||||
| 61 | 0 | my @rows; | |||||||||||||||||||
| 62 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 0 | ▶ | next unless exists $tables->{ $row->{tabname} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | $row->{_grp} = $row->{tabname} . "\0" . $row->{constname}; | |||||||||||||||||||
| 65 | 0 | push @rows, $row; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | 0 | my $groups = DBIO::Introspect::Base->_aggregate_by_ordered(\@rows, '_grp'); | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | # Columns of the referenced key: SYSCAT.REFERENCES.REFKEYNAME names the | ||||||||||||||||||||
| 70 | # parent's key (primary key or UNIQUE constraint) in REFTABSCHEMA.REFTABNAME, | ||||||||||||||||||||
| 71 | # and SYSCAT.KEYCOLUSE holds its columns in COLSEQ order. Resolving the | ||||||||||||||||||||
| 72 | # parent's primary key instead gave the wrong columns for an FK onto a | ||||||||||||||||||||
| 73 | # UNIQUE constraint (karr k45). | ||||||||||||||||||||
| 74 | 0 | my $key_sth = $dbh->prepare(q{ | |||||||||||||||||||
| 75 | SELECT colname, colseq | ||||||||||||||||||||
| 76 | FROM syscat.keycoluse | ||||||||||||||||||||
| 77 | WHERE constname = ? AND tabschema = ? AND tabname = ? | ||||||||||||||||||||
| 78 | ORDER BY colseq | ||||||||||||||||||||
| 79 | }); | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | for my $pair (@$groups) { | |||||||||||||||||||
| 82 | 0 | my ($key, $group) = @$pair; | |||||||||||||||||||
| 83 | 0 | my $first = $group->[0]; | |||||||||||||||||||
| 84 | my $fk = { | ||||||||||||||||||||
| 85 | constraint_name => $first->{constname}, | ||||||||||||||||||||
| 86 | from_table => $first->{tabname}, | ||||||||||||||||||||
| 87 | 0 | from_columns => [ map { $_->{colname} } @$group ], | |||||||||||||||||||
| 88 | to_table => $first->{reftabname}, | ||||||||||||||||||||
| 89 | to_schema => $first->{reftabschema}, | ||||||||||||||||||||
| 90 | to_columns => [], | ||||||||||||||||||||
| 91 | on_update => _rule($first->{updaterule}), | ||||||||||||||||||||
| 92 | 0 | on_delete => _rule($first->{deleterule}), | |||||||||||||||||||
| 93 | }; | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | my @to_cols; | |||||||||||||||||||
| 96 | 0 | ▶ | if (eval { $key_sth->execute($first->{refkeyname}, $fk->{to_schema}, $fk->{to_table}); 1 }) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | ▶ | while (my $r = $key_sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 0 | push @to_cols, $r->{colname}; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | 0 | $fk->{to_columns} = \@to_cols; | |||||||||||||||||||
| 102 | 0 | push @{ $fks{ $fk->{from_table} } }, $fk; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | return \%fks; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 37 | 1; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||