| 1 | package DBIO::Oracle::Introspect::ForeignKeys; | ||||||||||||||||||||
| 2 | # ABSTRACT: Introspect Oracle foreign keys | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use base 'DBIO::Introspect::Base'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 0 | ▶ | sub fetch { | ||||||||||||||||||
|
Subroutine
fetch: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||
| 12 | 0 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 0 | my $fk_sth = $dbh->prepare_cached(q{ | |||||||||||||||||||
| 15 | SELECT | ||||||||||||||||||||
| 16 | cc.constraint_name AS fk_name, | ||||||||||||||||||||
| 17 | cc.table_name AS from_table, | ||||||||||||||||||||
| 18 | kcu.column_name AS from_column, | ||||||||||||||||||||
| 19 | kcu.position AS from_pos, | ||||||||||||||||||||
| 20 | rcc.table_name AS to_table, | ||||||||||||||||||||
| 21 | rcc.column_name AS to_column, | ||||||||||||||||||||
| 22 | rc.position AS to_pos, | ||||||||||||||||||||
| 23 | cc.delete_rule AS on_delete, | ||||||||||||||||||||
| 24 | CASE WHEN cc.deferrable = 'DEFERRABLE' THEN 1 ELSE 0 END AS is_deferrable | ||||||||||||||||||||
| 25 | FROM all_constraints cc | ||||||||||||||||||||
| 26 | JOIN all_cons_columns kcu | ||||||||||||||||||||
| 27 | ON cc.constraint_name = kcu.constraint_name | ||||||||||||||||||||
| 28 | AND cc.owner = kcu.owner | ||||||||||||||||||||
| 29 | JOIN all_indexes ix | ||||||||||||||||||||
| 30 | ON ix.index_name = cc.index_name | ||||||||||||||||||||
| 31 | AND ix.owner = cc.owner | ||||||||||||||||||||
| 32 | JOIN all_cons_columns rc | ||||||||||||||||||||
| 33 | ON cc.r_constraint_name = rc.constraint_name | ||||||||||||||||||||
| 34 | AND cc.r_owner = rc.owner | ||||||||||||||||||||
| 35 | WHERE cc.constraint_type = 'R' | ||||||||||||||||||||
| 36 | AND cc.owner = ? | ||||||||||||||||||||
| 37 | ORDER BY cc.table_name, cc.constraint_name, kcu.position | ||||||||||||||||||||
| 38 | }); | ||||||||||||||||||||
| 39 | 0 | $fk_sth->execute($schema); | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | # Collect every row first; _aggregate_by_ordered needs the full list, and | ||||||||||||||||||||
| 42 | # we want a single pass for the row filter against $tables. | ||||||||||||||||||||
| 43 | 0 | my @rows; | |||||||||||||||||||
| 44 | 0 | ▶ | while (my $row = $fk_sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 0 | ▶ | next unless exists $tables->{ $row->{from_table} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 0 | push @rows, $row; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | 0 | $fk_sth->finish; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # Two-level grouping: by from_table, then by fk_name. Two FKs in different | ||||||||||||||||||||
| 51 | # tables may share a name, so the outer group is essential; _aggregate_by | ||||||||||||||||||||
| 52 | # alone (single key) would merge them. | ||||||||||||||||||||
| 53 | 0 | my $by_table = $class->_aggregate_by_ordered(\@rows, 'from_table'); | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | my %fks; | |||||||||||||||||||
| 56 | 0 | for my $t_pair (@$by_table) { | |||||||||||||||||||
| 57 | 0 | my ($table, $table_rows) = @$t_pair; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | my $by_constraint = $class->_aggregate_by_ordered($table_rows, 'fk_name'); | |||||||||||||||||||
| 60 | 0 | for my $c_pair (@$by_constraint) { | |||||||||||||||||||
| 61 | 0 | my ($fk_name, $c_rows) = @$c_pair; | |||||||||||||||||||
| 62 | 0 | my $first = $c_rows->[0]; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | push @{ $fks{$table} }, { | |||||||||||||||||||
| 65 | fk_name => $fk_name, | ||||||||||||||||||||
| 66 | from_table => $table, | ||||||||||||||||||||
| 67 | 0 | from_columns => [ map { $_->{from_column} } @$c_rows ], | |||||||||||||||||||
| 68 | to_table => $first->{to_table}, | ||||||||||||||||||||
| 69 | 0 | ▶ | to_columns => [ map { $_->{to_column} } @$c_rows ], | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$first->{"on_delete"} // "NO ACTION"
MC/DC0%0/1
$first->{"on_delete"} // "NO ACTION"
$first->{"on_delete"}
Truth table0%0/2
A
A= $first->{"on_delete"}
| |||||||||||||||||||||
| 70 | on_update => 'NO ACTION', | ||||||||||||||||||||
| 71 | on_delete => $first->{on_delete} // 'NO ACTION', | ||||||||||||||||||||
| 72 | 0 | is_deferrable => $first->{is_deferrable} ? 1 : 0, | |||||||||||||||||||
| 73 | }; | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | # Ensure all tables have an entry even if no FKs | ||||||||||||||||||||
| 78 | 0 | ▶ | $fks{$_} //= [] for keys %$tables; | ||||||||||||||||||
|
Condition0%0/2
$fks{$_} //= []
MC/DC0%0/1
$fks{$_} //= []
$fks{$_}
Truth table0%0/2
A
A= $fks{$_}
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | return \%fks; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 1 | 1; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =pod | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =encoding UTF-8 | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =head1 NAME | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | DBIO::Oracle::Introspect::ForeignKeys - Introspect Oracle foreign keys | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =head1 VERSION | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | version 0.900001 | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =head1 DESCRIPTION | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | Fetches Oracle foreign key metadata via C<all_constraints>, | ||||||||||||||||||||
| 102 | C<all_cons_columns>, and C<all_indexes>. Includes deferrability | ||||||||||||||||||||
| 103 | information from C<all_constraints> directly. | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =head1 METHODS | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =head2 fetch | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | my $fks = DBIO::Oracle::Introspect::ForeignKeys->fetch($dbh, $schema, $tables); | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | Returns a hashref keyed by table name, each value an arrayref of FK | ||||||||||||||||||||
| 112 | hashrefs with: C<fk_name>, C<from_columns>, C<to_table>, C<to_columns>, | ||||||||||||||||||||
| 113 | C<on_update>, C<on_delete>, C<is_deferrable>. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =head1 AUTHOR | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | DBIO & DBIx::Class Authors | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||
| 122 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||
| 123 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 126 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||