| 1 | package DBIO::Oracle::Introspect::Keys; | ||||||||||||
| 2 | # ABSTRACT: Introspect Oracle primary key and unique constraints | ||||||||||||
| 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 | |||||||||||||
| 10 | |||||||||||||
| 11 | 0 | ▶ | sub fetch { | ||||||||||
|
Subroutine
fetch: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||
| 12 | 0 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||
| 13 | |||||||||||||
| 14 | 0 | my %primary; | |||||||||||
| 15 | my %unique; | ||||||||||||
| 16 | |||||||||||||
| 17 | 0 | my $sth = $dbh->prepare_cached(q{ | |||||||||||
| 18 | SELECT c.constraint_name, c.constraint_type, c.table_name, | ||||||||||||
| 19 | cc.column_name, cc.position | ||||||||||||
| 20 | FROM all_constraints c | ||||||||||||
| 21 | JOIN all_cons_columns cc | ||||||||||||
| 22 | ON c.constraint_name = cc.constraint_name | ||||||||||||
| 23 | AND c.owner = cc.owner | ||||||||||||
| 24 | WHERE c.owner = ? | ||||||||||||
| 25 | AND c.constraint_type IN ('P', 'U') | ||||||||||||
| 26 | AND c.table_name NOT LIKE 'BIN$%' | ||||||||||||
| 27 | ORDER BY c.table_name, c.constraint_type, c.constraint_name, cc.position | ||||||||||||
| 28 | }); | ||||||||||||
| 29 | 0 | $sth->execute($schema); | |||||||||||
| 30 | |||||||||||||
| 31 | 0 | my %pk_cols; # table => [cols] in position order | |||||||||||
| 32 | my %uniq_cols; # table => { constraint => [cols] } | ||||||||||||
| 33 | |||||||||||||
| 34 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref('NAME_lc')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 0 | my $tbl = $row->{table_name}; | |||||||||||
| 36 | 0 | ▶ | next unless exists $tables->{$tbl}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | |||||||||||||
| 38 | 0 | ▶ | if ($row->{constraint_type} eq 'P') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | 0 | push @{ $pk_cols{$tbl} }, $row->{column_name}; | |||||||||||
| 40 | } | ||||||||||||
| 41 | else { | ||||||||||||
| 42 | 0 | push @{ $uniq_cols{$tbl}{ $row->{constraint_name} } }, $row->{column_name}; | |||||||||||
| 43 | } | ||||||||||||
| 44 | } | ||||||||||||
| 45 | 0 | $sth->finish; | |||||||||||
| 46 | |||||||||||||
| 47 | 0 | for my $tbl (keys %$tables) { | |||||||||||
| 48 | 0 | ▶ | $primary{$tbl} = $pk_cols{$tbl} // []; | ||||||||||
|
Condition0%0/2
$pk_cols{$tbl} // []
MC/DC0%0/1
$pk_cols{$tbl} // []
$pk_cols{$tbl}
Truth table0%0/2
A
A= $pk_cols{$tbl}
| |||||||||||||
| 49 | |||||||||||||
| 50 | 0 | ▶ | my $by_name = $uniq_cols{$tbl} // {}; | ||||||||||
|
Condition0%0/2
$uniq_cols{$tbl} // {}
MC/DC0%0/1
$uniq_cols{$tbl} // {}
$uniq_cols{$tbl}
Truth table0%0/2
A
A= $uniq_cols{$tbl}
| |||||||||||||
| 51 | 0 | $unique{$tbl} = [ map { [ $_ => $by_name->{$_} ] } sort keys %$by_name ]; | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | return { | ||||||||||||
| 55 | 0 | primary => \%primary, | |||||||||||
| 56 | unique => \%unique, | ||||||||||||
| 57 | }; | ||||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 27 | 1; | |||||||||||
| 61 | |||||||||||||
| 62 | __END__ | ||||||||||||