| 1 | package DBIO::Firebird::Introspect::ForeignKeys; | ||||||||||||
| 2 | # ABSTRACT: Introspect Firebird foreign keys via rdb$relation_constraints | ||||||||||||
| 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=6 SCAR=37.4
Pod
documented
| |||||||||||||
| 10 | 0 | my ($class, $dbh, $tables) = @_; | |||||||||||
| 11 | 0 | my %fks; | |||||||||||
| 12 | |||||||||||||
| 13 | 0 | my $sth = $dbh->prepare(q{ | |||||||||||
| 14 | SELECT rc.rdb$constraint_name, rc.rdb$relation_name, | ||||||||||||
| 15 | ri.rdb$relation_name AS to_table, | ||||||||||||
| 16 | iseg.rdb$field_name AS from_column, | ||||||||||||
| 17 | iseg.rdb$field_position, | ||||||||||||
| 18 | ri.rdb$foreign_key, | ||||||||||||
| 19 | refc.rdb$update_rule, refc.rdb$delete_rule, refc.rdb$match_option | ||||||||||||
| 20 | FROM rdb$relation_constraints rc | ||||||||||||
| 21 | JOIN rdb$indices ix ON rc.rdb$index_name = ix.rdb$index_name | ||||||||||||
| 22 | JOIN rdb$index_segments iseg ON ix.rdb$index_name = iseg.rdb$index_name | ||||||||||||
| 23 | JOIN rdb$indices ri ON ix.rdb$foreign_key = ri.rdb$index_name | ||||||||||||
| 24 | JOIN rdb$ref_constraints refc ON refc.rdb$constraint_name = rc.rdb$constraint_name | ||||||||||||
| 25 | WHERE rc.rdb$constraint_type = 'FOREIGN KEY' | ||||||||||||
| 26 | ORDER BY rc.rdb$relation_name, rc.rdb$constraint_name, iseg.rdb$field_position | ||||||||||||
| 27 | }); | ||||||||||||
| 28 | 0 | $sth->execute; | |||||||||||
| 29 | |||||||||||||
| 30 | 0 | my %by_constraint; | |||||||||||
| 31 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 0 | my $table = $row->{'rdb$relation_name'}; | |||||||||||
| 33 | 0 | $table =~ s/\s+$//; | |||||||||||
| 34 | 0 | my $to_table = $row->{to_table}; | |||||||||||
| 35 | 0 | $to_table =~ s/\s+$//; | |||||||||||
| 36 | 0 | ▶ | next unless exists $tables->{$table}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | |||||||||||||
| 38 | 0 | my $cname = $row->{'rdb$constraint_name'}; | |||||||||||
| 39 | 0 | $cname =~ s/\s+$//; | |||||||||||
| 40 | |||||||||||||
| 41 | $by_constraint{$cname} //= { | ||||||||||||
| 42 | fk_id => $cname, | ||||||||||||
| 43 | from_table => $table, | ||||||||||||
| 44 | from_columns => [], | ||||||||||||
| 45 | to_table => $to_table, | ||||||||||||
| 46 | to_columns => [], | ||||||||||||
| 47 | on_update => _trim($row->{'rdb$update_rule'}), | ||||||||||||
| 48 | on_delete => _trim($row->{'rdb$delete_rule'}), | ||||||||||||
| 49 | 0 | ▶ | match => _trim($row->{'rdb$match_option'}), | ||||||||||
|
Condition0%0/2
$by_constraint{$cname} //= {"fk_id", $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} //= {"fk_id", $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 table0%0/2
A
A= $by_constraint{$cname}
| |||||||||||||
| 50 | }; | ||||||||||||
| 51 | 0 | my $col = $row->{from_column}; | |||||||||||
| 52 | 0 | $col =~ s/\s+$//; | |||||||||||
| 53 | 0 | push @{ $by_constraint{$cname}{from_columns} }, $col; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | # Resolve the "to" columns by looking up the referenced index segments | ||||||||||||
| 57 | 0 | for my $cname (sort keys %by_constraint) { | |||||||||||
| 58 | 0 | my $fk = $by_constraint{$cname}; | |||||||||||
| 59 | # The referenced (parent) columns are the segments of the index the FK | ||||||||||||
| 60 | # points AT -- ix.rdb$foreign_key -- not the FK's own index (which holds | ||||||||||||
| 61 | # the local/child columns). | ||||||||||||
| 62 | 0 | my $to_sth = $dbh->prepare(q{ | |||||||||||
| 63 | SELECT iseg.rdb$field_name | ||||||||||||
| 64 | FROM rdb$index_segments iseg | ||||||||||||
| 65 | WHERE iseg.rdb$index_name = ( | ||||||||||||
| 66 | SELECT ix.rdb$foreign_key | ||||||||||||
| 67 | FROM rdb$relation_constraints rc | ||||||||||||
| 68 | JOIN rdb$indices ix ON rc.rdb$index_name = ix.rdb$index_name | ||||||||||||
| 69 | WHERE rc.rdb$constraint_name = ? | ||||||||||||
| 70 | AND rc.rdb$constraint_type = 'FOREIGN KEY' | ||||||||||||
| 71 | ) | ||||||||||||
| 72 | ORDER BY iseg.rdb$field_position | ||||||||||||
| 73 | }); | ||||||||||||
| 74 | 0 | $to_sth->execute($cname); | |||||||||||
| 75 | 0 | ▶ | while (my ($col) = $to_sth->fetchrow_array) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 0 | $col =~ s/\s+$//; | |||||||||||
| 77 | 0 | push @{ $fk->{to_columns} }, $col; | |||||||||||
| 78 | } | ||||||||||||
| 79 | 0 | push @{ $fks{ $fk->{from_table} } }, $fk; | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 0 | return \%fks; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | # Trim trailing whitespace from a (possibly undef) CHAR-padded rdb$ value. | ||||||||||||
| 86 | 0 | ▶ | sub _trim { | ||||||||||
|
Subroutine
_trim: not called CC=2 SCAR=17.9
| |||||||||||||
| 87 | 0 | my ($value) = @_; | |||||||||||
| 88 | 0 | ▶ | return undef unless defined $value; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 0 | $value =~ s/\s+$//; | |||||||||||
| 90 | 0 | return $value; | |||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | 3 | 1; | |||||||||||
| 94 | |||||||||||||
| 95 | __END__ | ||||||||||||
| 96 | |||||||||||||
| 97 | =pod | ||||||||||||
| 98 | |||||||||||||
| 99 | =encoding UTF-8 | ||||||||||||
| 100 | |||||||||||||
| 101 | =head1 NAME | ||||||||||||
| 102 | |||||||||||||
| 103 | DBIO::Firebird::Introspect::ForeignKeys - Introspect Firebird foreign keys via rdb$relation_constraints | ||||||||||||
| 104 | |||||||||||||
| 105 | =head1 VERSION | ||||||||||||
| 106 | |||||||||||||
| 107 | version 0.900001 | ||||||||||||
| 108 | |||||||||||||
| 109 | =head1 DESCRIPTION | ||||||||||||
| 110 | |||||||||||||
| 111 | Fetches foreign-key metadata via C<rdb$relation_constraints> joined | ||||||||||||
| 112 | against C<rdb$index_segments> and C<rdb$indices>. Composite FKs are | ||||||||||||
| 113 | grouped by constraint name. | ||||||||||||
| 114 | |||||||||||||
| 115 | =head1 METHODS | ||||||||||||
| 116 | |||||||||||||
| 117 | =head2 fetch | ||||||||||||
| 118 | |||||||||||||
| 119 | my $fks = DBIO::Firebird::Introspect::ForeignKeys->fetch($dbh, $tables); | ||||||||||||
| 120 | |||||||||||||
| 121 | Returns a hashref keyed by table name, each value an arrayref of FK | ||||||||||||
| 122 | hashrefs with: C<fk_id>, C<from_columns>, C<to_table>, C<to_columns>, | ||||||||||||
| 123 | C<on_update>, C<on_delete>, C<match>. | ||||||||||||
| 124 | |||||||||||||
| 125 | =head1 AUTHOR | ||||||||||||
| 126 | |||||||||||||
| 127 | DBIO & DBIx::Class Authors | ||||||||||||
| 128 | |||||||||||||
| 129 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 130 | |||||||||||||
| 131 | Copyright (C) 2026 DBIO Authors | ||||||||||||
| 132 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||
| 133 | Based on DBIx::Class, heavily modified. | ||||||||||||
| 134 | |||||||||||||
| 135 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 136 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 137 | |||||||||||||
| 138 | =cut | ||||||||||||