| 1 | package DBIO::Firebird::Introspect::ForeignKeys; | ||||||||||||
| 2 | # ABSTRACT: Introspect Firebird foreign keys via rdb$relation_constraints | ||||||||||||
| 3 | |||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 7 | our $VERSION = '0.900007'; | |||||||||||
| 8 | |||||||||||||
| 9 | |||||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | sub fetch { | ||||||||||
|
Subroutine
fetch: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 12 | 1 | my ($class, $dbh, $tables) = @_; | |||||||||||
| 13 | 1 | my %fks; | |||||||||||
| 14 | |||||||||||||
| 15 | 1 | my $sth = $dbh->prepare(q{ | |||||||||||
| 16 | SELECT rc.rdb$constraint_name, rc.rdb$relation_name, | ||||||||||||
| 17 | ri.rdb$relation_name AS to_table, | ||||||||||||
| 18 | iseg.rdb$field_name AS from_column, | ||||||||||||
| 19 | iseg.rdb$field_position, | ||||||||||||
| 20 | ri.rdb$foreign_key, | ||||||||||||
| 21 | refc.rdb$update_rule, refc.rdb$delete_rule, refc.rdb$match_option | ||||||||||||
| 22 | FROM rdb$relation_constraints rc | ||||||||||||
| 23 | JOIN rdb$indices ix ON rc.rdb$index_name = ix.rdb$index_name | ||||||||||||
| 24 | JOIN rdb$index_segments iseg ON ix.rdb$index_name = iseg.rdb$index_name | ||||||||||||
| 25 | JOIN rdb$indices ri ON ix.rdb$foreign_key = ri.rdb$index_name | ||||||||||||
| 26 | JOIN rdb$ref_constraints refc ON refc.rdb$constraint_name = rc.rdb$constraint_name | ||||||||||||
| 27 | WHERE rc.rdb$constraint_type = 'FOREIGN KEY' | ||||||||||||
| 28 | ORDER BY rc.rdb$relation_name, rc.rdb$constraint_name, iseg.rdb$field_position | ||||||||||||
| 29 | }); | ||||||||||||
| 30 | 1 | $sth->execute; | |||||||||||
| 31 | |||||||||||||
| 32 | 1 | my %by_constraint; | |||||||||||
| 33 | 1 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 1 | my $table = $row->{'rdb$relation_name'}; | |||||||||||
| 35 | 1 | $table =~ s/\s+$//; | |||||||||||
| 36 | 1 | my $to_table = $row->{to_table}; | |||||||||||
| 37 | 1 | $to_table =~ s/\s+$//; | |||||||||||
| 38 | 1 | ▶ | next unless exists $tables->{$table}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | |||||||||||||
| 40 | 1 | my $cname = $row->{'rdb$constraint_name'}; | |||||||||||
| 41 | 1 | $cname =~ s/\s+$//; | |||||||||||
| 42 | |||||||||||||
| 43 | $by_constraint{$cname} //= { | ||||||||||||
| 44 | constraint_name => $cname, | ||||||||||||
| 45 | from_table => $table, | ||||||||||||
| 46 | from_columns => [], | ||||||||||||
| 47 | to_table => $to_table, | ||||||||||||
| 48 | to_columns => [], | ||||||||||||
| 49 | on_update => _trim($row->{'rdb$update_rule'}), | ||||||||||||
| 50 | on_delete => _trim($row->{'rdb$delete_rule'}), | ||||||||||||
| 51 | 1 | ▶ | match => _trim($row->{'rdb$match_option'}), | ||||||||||
|
Condition50%1/2
$by_constraint{$cname} //= {"constraint_name", $cname, "from_table", $table, "from_columns", [], "to_table", $to_table, "to_columns", [], "on_update", _trim($row->{"rdb\$update_rule"}), "on_delete", _trim($row->{"rdb\$delete_rule"}), "match", _trim($row->{"rdb\$match_option"})}
MC/DC0%0/1
$by_constraint{$cname} //= {"constraint_name", $cname, "from_table", $table, "from_columns", [], "to_table", $to_table, "to_columns", [], "on_update", _trim($row->{"rdb\$update_rule"}), "on_delete", _trim($row->{"rdb\$delete_rule"}), "match", _trim($row->{"rdb\$match_option"})}
$by_constraint{$cname}
Truth table50%1/2
A
A= $by_constraint{$cname}
| |||||||||||||
| 52 | }; | ||||||||||||
| 53 | 1 | my $col = $row->{from_column}; | |||||||||||
| 54 | 1 | $col =~ s/\s+$//; | |||||||||||
| 55 | 1 | push @{ $by_constraint{$cname}{from_columns} }, $col; | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | # Resolve the "to" columns by looking up the referenced index segments | ||||||||||||
| 59 | 1 | for my $cname (sort keys %by_constraint) { | |||||||||||
| 60 | 1 | my $fk = $by_constraint{$cname}; | |||||||||||
| 61 | # The referenced (parent) columns are the segments of the index the FK | ||||||||||||
| 62 | # points AT -- ix.rdb$foreign_key -- not the FK's own index (which holds | ||||||||||||
| 63 | # the local/child columns). | ||||||||||||
| 64 | 1 | my $to_sth = $dbh->prepare(q{ | |||||||||||
| 65 | SELECT iseg.rdb$field_name | ||||||||||||
| 66 | FROM rdb$index_segments iseg | ||||||||||||
| 67 | WHERE iseg.rdb$index_name = ( | ||||||||||||
| 68 | SELECT ix.rdb$foreign_key | ||||||||||||
| 69 | FROM rdb$relation_constraints rc | ||||||||||||
| 70 | JOIN rdb$indices ix ON rc.rdb$index_name = ix.rdb$index_name | ||||||||||||
| 71 | WHERE rc.rdb$constraint_name = ? | ||||||||||||
| 72 | AND rc.rdb$constraint_type = 'FOREIGN KEY' | ||||||||||||
| 73 | ) | ||||||||||||
| 74 | ORDER BY iseg.rdb$field_position | ||||||||||||
| 75 | }); | ||||||||||||
| 76 | 1 | $to_sth->execute($cname); | |||||||||||
| 77 | 1 | ▶ | while (my ($col) = $to_sth->fetchrow_array) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 1 | $col =~ s/\s+$//; | |||||||||||
| 79 | 1 | push @{ $fk->{to_columns} }, $col; | |||||||||||
| 80 | } | ||||||||||||
| 81 | 1 | push @{ $fks{ $fk->{from_table} } }, $fk; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 1 | return \%fks; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | # Trim trailing whitespace from a (possibly undef) CHAR-padded rdb$ value. | ||||||||||||
| 88 | 3 | ▶ | sub _trim { | ||||||||||
|
Subroutine
_trim: called CC=2 SCAR=7.0
| |||||||||||||
| 89 | 3 | my ($value) = @_; | |||||||||||
| 90 | 3 | ▶ | return undef unless defined $value; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | 3 | $value =~ s/\s+$//; | |||||||||||
| 92 | 3 | return $value; | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | 7 | 1; | |||||||||||
| 96 | |||||||||||||
| 97 | __END__ | ||||||||||||