Oracle/Introspect/ForeignKeys.pm

filter: Statementstmt 66.6% 22 / 33 Branchbran 12.5% 1 / 8 Conditioncond 14.2% 1 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 50.8% 29 / 57 CC 11 SCAR 35.5
CC
11
Cov
50%
fetch
44.0
BEGIN
1.0
BEGIN
1.0
CRAP 34.8
« Oracle/Type.pm ↑ Summary Oracle/Diff.pm »
1 package DBIO::Oracle::Introspect::ForeignKeys;
2 # ABSTRACT: Introspect Oracle foreign keys
3  
4 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 29 our $VERSION = '0.900007';
8  
9 29 ▶use base 'DBIO::Introspect::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 1 ▶sub fetch {
Subroutine fetch: called CC=11 SCAR=37.8
Pod documented
14 1 my ($class, $dbh, $schema, $tables) = @_;
15  
16 1 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 NLSSORT(cc.table_name, 'NLS_SORT=BINARY'),
39 NLSSORT(cc.constraint_name, 'NLS_SORT=BINARY'), kcu.position
40 });
41 1 $fk_sth->execute($schema);
42  
43 # Collect every row first; _aggregate_by_ordered needs the full list, and
44 # we want a single pass for the row filter against $tables.
45 1 my @rows;
46 1 ▶ while (my $row = $fk_sth->fetchrow_hashref('NAME_lc')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $fk_sth->fetchrow_hashref("NAME_lc")) 0 1 1
47 0 ▶ next unless exists $tables->{ $row->{from_table} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$tables{$row->{"from_table"}} 0 0 0
48 0 push @rows, $row;
49 }
50 1 $fk_sth->finish;
51  
52 # Two-level grouping: by from_table, then by constraint_name. Two FKs in different
53 # tables may share a name, so the outer group is essential; _aggregate_by
54 # alone (single key) would merge them.
55 1 my $by_table = $class->_aggregate_by_ordered(\@rows, 'from_table');
56  
57 1 my %fks;
58 1 for my $t_pair (@$by_table) {
59 0 my ($table, $table_rows) = @$t_pair;
60  
61 0 my $by_constraint = $class->_aggregate_by_ordered($table_rows, 'constraint_name');
62 0 for my $c_pair (@$by_constraint) {
63 0 my ($constraint_name, $c_rows) = @$c_pair;
64 0 my $first = $c_rows->[0];
65  
66 0 push @{ $fks{$table} }, {
67 constraint_name => $constraint_name,
68 from_table => $table,
69 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
70 # a table of another schema is named owner.table (karr k58)
71 to_table => (defined $first->{to_owner} && $first->{to_owner} ne $schema)
72 ? "$first->{to_owner}.$first->{to_table}" : $first->{to_table},
73 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
74 on_update => 'NO ACTION',
75 on_delete => $first->{on_delete} // 'NO ACTION',
76 0 is_deferrable => $first->{is_deferrable} ? 1 : 0,
77 };
78 }
79 }
80  
81 # Ensure all tables have an entry even if no FKs
82 1 ▶ $fks{$_} //= [] for keys %$tables;
Condition50%1/2
$fks{$_} //= []
l!l
01
MC/DC0%0/1
$fks{$_} //= []
$fks{$_}
Truth table50%1/2
A
A= $fks{$_}
Aresult
11
00
83  
84 1 return \%fks;
85 }
86  
87 29 1;
88  
89 __END__
« Oracle/Type.pm ↑ Summary Oracle/Diff.pm »