Oracle/Introspect/ForeignKeys.pm

filter: Statementstmt 33.3% 11 / 33 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 26.3% 15 / 57 CC 11 SCAR 46.4
CC
11
Cov
23%
fetch
132.0
BEGIN
1.0
BEGIN
1.0
CRAP 103.9
« Oracle/Storage/AutoIncrement.pm ↑ Summary Oracle/Introspect/Indexes.pm »
1 package DBIO::Oracle::Introspect::ForeignKeys;
2 # ABSTRACT: Introspect Oracle foreign keys
3  
4 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 our $VERSION = '0.900004';
8  
9 27 ▶use base 'DBIO::Introspect::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 0 ▶sub fetch {
Subroutine fetch: not called CC=11 SCAR=48.8
Pod documented
14 0 my ($class, $dbh, $schema, $tables) = @_;
15  
16 0 my $fk_sth = $dbh->prepare_cached(q{
17 SELECT
18 cc.constraint_name AS constraint_name,
19 cc.table_name AS from_table,
20 kcu.column_name AS from_column,
21 kcu.position AS from_pos,
22 rc.table_name AS to_table,
23 rc.owner AS to_owner,
24 rc.column_name AS to_column,
25 rc.position AS to_pos,
26 cc.delete_rule AS on_delete,
27 CASE WHEN cc.deferrable = 'DEFERRABLE' THEN 1 ELSE 0 END AS is_deferrable
28 FROM all_constraints cc
29 JOIN all_cons_columns kcu
30 ON cc.constraint_name = kcu.constraint_name
31 AND cc.owner = kcu.owner
32 JOIN all_cons_columns rc
33 ON cc.r_constraint_name = rc.constraint_name
34 AND cc.r_owner = rc.owner
35 AND rc.position = kcu.position
36 WHERE cc.constraint_type = 'R'
37 AND cc.owner = ?
38 ORDER BY cc.table_name, cc.constraint_name, kcu.position
39 });
40 0 $fk_sth->execute($schema);
41  
42 # Collect every row first; _aggregate_by_ordered needs the full list, and
43 # we want a single pass for the row filter against $tables.
44 0 my @rows;
45 0 ▶ while (my $row = $fk_sth->fetchrow_hashref('NAME_lc')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $fk_sth->fetchrow_hashref("NAME_lc")) 0 0 0
46 0 ▶ next unless exists $tables->{ $row->{from_table} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$tables{$row->{"from_table"}} 0 0 0
47 0 push @rows, $row;
48 }
49 0 $fk_sth->finish;
50  
51 # Two-level grouping: by from_table, then by constraint_name. Two FKs in different
52 # tables may share a name, so the outer group is essential; _aggregate_by
53 # alone (single key) would merge them.
54 0 my $by_table = $class->_aggregate_by_ordered(\@rows, 'from_table');
55  
56 0 my %fks;
57 0 for my $t_pair (@$by_table) {
58 0 my ($table, $table_rows) = @$t_pair;
59  
60 0 my $by_constraint = $class->_aggregate_by_ordered($table_rows, 'constraint_name');
61 0 for my $c_pair (@$by_constraint) {
62 0 my ($constraint_name, $c_rows) = @$c_pair;
63 0 my $first = $c_rows->[0];
64  
65 0 push @{ $fks{$table} }, {
66 constraint_name => $constraint_name,
67 from_table => $table,
68 0 ▶ from_columns => [ map { $_->{from_column} } @$c_rows ],
Branch: 1 branch
BranchTrueFalseTotal
defined $first->{'to_owner'} && $first->{'to_owner'} ne $schema ? : 0 0 0
Condition0%0/3
defined $first->{"to_owner"} and $first->{"to_owner"} ne $schema
!ll&&!rl&&r
000
MC/DC0%0/2
defined $first->{"to_owner"} and $first->{"to_owner"} ne $schema
defined $first->{"to_owner"} $first->{"to_owner"} ne $schema
Truth table0%0/3
A and B
A= defined $first->{"to_owner"}B= $first->{"to_owner"} ne $schema
ABresult
0X0
100
111
69 # a table of another schema is named owner.table (karr k58)
70 to_table => (defined $first->{to_owner} && $first->{to_owner} ne $schema)
71 ? "$first->{to_owner}.$first->{to_table}" : $first->{to_table},
72 0 ▶ to_columns => [ map { $_->{to_column} } @$c_rows ],
Branch: 1 branch
BranchTrueFalseTotal
$first->{'is_deferrable'} ? : 0 0 0
Condition0%0/2
$first->{"on_delete"} // "NO ACTION"
l!l
00
MC/DC0%0/1
$first->{"on_delete"} // "NO ACTION"
$first->{"on_delete"}
Truth table0%0/2
A
A= $first->{"on_delete"}
Aresult
11
00
73 on_update => 'NO ACTION',
74 on_delete => $first->{on_delete} // 'NO ACTION',
75 0 is_deferrable => $first->{is_deferrable} ? 1 : 0,
76 };
77 }
78 }
79  
80 # Ensure all tables have an entry even if no FKs
81 0 ▶ $fks{$_} //= [] for keys %$tables;
Condition0%0/2
$fks{$_} //= []
l!l
00
MC/DC0%0/1
$fks{$_} //= []
$fks{$_}
Truth table0%0/2
A
A= $fks{$_}
Aresult
11
00
82  
83 0 return \%fks;
84 }
85  
86 27 1;
87  
88 __END__
« Oracle/Storage/AutoIncrement.pm ↑ Summary Oracle/Introspect/Indexes.pm »