| 1 | package DBIO::DB2::Introspect::ForeignKeys; | ||||||||||||||||||||
| 2 | # ABSTRACT: Introspect DB2 foreign keys | ||||||||||||||||||||
| 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 | 3 | ▶ | use DBIO::Introspect::Base (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | # SYSCAT.REFERENCES stores DELETERULE/UPDATERULE as single-char codes. Normalize | ||||||||||||||||||||
| 10 | # them to the SQL keywords the model carries everywhere else, so the FK clause | ||||||||||||||||||||
| 11 | # emitter (DBIO::DB2::DDL::_fk_constraint_clause) renders e.g. ON DELETE CASCADE | ||||||||||||||||||||
| 12 | # (not ON DELETE C) and a rule comparison against install_ddl-shaped FKs matches. | ||||||||||||||||||||
| 13 | 3 | my %RULE = ( | |||||||||||||||||||
| 14 | A => 'NO ACTION', | ||||||||||||||||||||
| 15 | C => 'CASCADE', | ||||||||||||||||||||
| 16 | N => 'SET NULL', | ||||||||||||||||||||
| 17 | R => 'RESTRICT', | ||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 0 | ▶ | sub _rule { | ||||||||||||||||||
|
Subroutine
_rule: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 21 | 0 | my ($code) = @_; | |||||||||||||||||||
| 22 | 0 | ▶ | return undef unless defined $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | 0 | $code =~ s/\s+$//; | |||||||||||||||||||
| 24 | 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
| |||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | ▶ | sub fetch { | ||||||||||||||||||
|
Subroutine
fetch: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 30 | 0 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||
| 31 | 0 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; # DBD::DB2 upper-cases hashref keys | |||||||||||||||||||
| 32 | 0 | my %fks; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | my $sth = $dbh->prepare(q{ | |||||||||||||||||||
| 35 | SELECT tc.constname, tc.tabname, | ||||||||||||||||||||
| 36 | kcu.colname, kcu.colseq, | ||||||||||||||||||||
| 37 | sr.reftabname, sr.reftabschema, sr.refkeyname, | ||||||||||||||||||||
| 38 | sr.deleterule, sr.updaterule | ||||||||||||||||||||
| 39 | FROM syscat.tabconst tc | ||||||||||||||||||||
| 40 | JOIN syscat.keycoluse kcu | ||||||||||||||||||||
| 41 | ON tc.constname = kcu.constname | ||||||||||||||||||||
| 42 | AND tc.tabschema = kcu.tabschema | ||||||||||||||||||||
| 43 | AND tc.tabname = kcu.tabname | ||||||||||||||||||||
| 44 | JOIN syscat.references sr | ||||||||||||||||||||
| 45 | ON tc.constname = sr.constname | ||||||||||||||||||||
| 46 | AND tc.tabschema = sr.tabschema | ||||||||||||||||||||
| 47 | AND tc.tabname = sr.tabname | ||||||||||||||||||||
| 48 | WHERE tc.tabschema = ? | ||||||||||||||||||||
| 49 | AND tc.type = 'F' | ||||||||||||||||||||
| 50 | ORDER BY tc.tabname, tc.constname, kcu.colseq | ||||||||||||||||||||
| 51 | }); | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | my $ok = eval { $sth->execute($schema); 1 }; | |||||||||||||||||||
| 54 | 0 | ▶ | return \%fks unless $ok; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | # Collect the flat rows (one per FK column, in colseq order) and group them by | ||||||||||||||||||||
| 57 | # constraint via the core ordered aggregator, which preserves column order | ||||||||||||||||||||
| 58 | # within each FK. | ||||||||||||||||||||
| 59 | 0 | my @rows; | |||||||||||||||||||
| 60 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | ▶ | next unless exists $tables->{ $row->{tabname} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | 0 | $row->{_grp} = $row->{tabname} . "\0" . $row->{constname}; | |||||||||||||||||||
| 63 | 0 | push @rows, $row; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | 0 | my $groups = DBIO::Introspect::Base->_aggregate_by_ordered(\@rows, '_grp'); | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | # Parent-PK lookup: DB2 SYSCAT.REFERENCES points at the parent's referenced | ||||||||||||||||||||
| 68 | # key by name, but we resolve the parent primary-key columns directly (kept | ||||||||||||||||||||
| 69 | # DB2-specific multi-step resolution). | ||||||||||||||||||||
| 70 | 0 | my $pk_sth = $dbh->prepare(q{ | |||||||||||||||||||
| 71 | SELECT kcu.colname, kcu.colseq | ||||||||||||||||||||
| 72 | FROM syscat.keycoluse kcu | ||||||||||||||||||||
| 73 | JOIN syscat.tabconst tc | ||||||||||||||||||||
| 74 | ON kcu.constname = tc.constname | ||||||||||||||||||||
| 75 | AND kcu.tabschema = tc.tabschema | ||||||||||||||||||||
| 76 | AND kcu.tabname = tc.tabname | ||||||||||||||||||||
| 77 | WHERE tc.tabschema = ? AND tc.tabname = ? AND tc.type = 'P' | ||||||||||||||||||||
| 78 | ORDER BY kcu.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 @pk_cols; | |||||||||||||||||||
| 96 | 0 | ▶ | if (eval { $pk_sth->execute($fk->{to_schema}, $fk->{to_table}); 1 }) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | ▶ | while (my $r = $pk_sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 0 | push @pk_cols, $r->{colname}; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | 0 | $fk->{to_columns} = \@pk_cols; | |||||||||||||||||||
| 102 | 0 | push @{ $fks{ $fk->{from_table} } }, $fk; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | return \%fks; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 3 | 1; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =pod | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =encoding UTF-8 | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =head1 NAME | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | DBIO::DB2::Introspect::ForeignKeys - Introspect DB2 foreign keys | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =head1 VERSION | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | version 0.900001 | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =head1 DESCRIPTION | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | Fetches foreign-key metadata via C<SYSCAT.TABCONST> joined with | ||||||||||||||||||||
| 127 | C<SYSCAT.KEYCOLUSE> and C<SYSCAT.REFERENCES>. Composite FKs are | ||||||||||||||||||||
| 128 | grouped by constraint name. | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =head1 METHODS | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head2 fetch | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | my $fks = DBIO::DB2::Introspect::ForeignKeys->fetch($dbh, $schema, $tables); | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | Returns a hashref keyed by table name, each value an arrayref of FK | ||||||||||||||||||||
| 137 | hashrefs with: C<constraint_name>, C<from_columns>, C<to_table>, | ||||||||||||||||||||
| 138 | C<to_columns>, C<on_update>, C<on_delete>. C<constraint_name> is the | ||||||||||||||||||||
| 139 | canonical FK key blessed by core ADR 0021. | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =head1 AUTHOR | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | DBIO & DBIx::Class Authors | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||
| 148 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||
| 149 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 152 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||