Oracle/Introspect/Keys.pm

filter: Statementstmt 25.9% 7 / 27 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 2 / 3 Podpod 100% 1 / 1 totaltotal 23.2% 10 / 43 CC 7 SCAR 37.8
CC
7
Cov
19%
fetch
56.0
BEGIN
1.0
BEGIN
1.0
CRAP 43.8
« Oracle/Introspect/Indexes.pm ↑ Summary Oracle/SQLMaker.pm »
1 package DBIO::Oracle::Introspect::Keys;
2 # ABSTRACT: Introspect Oracle primary key and unique constraints
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  
8  
9 0 ▶sub fetch {
Subroutine fetch: not called CC=7 SCAR=40.3
Pod documented
10 0 my ($class, $dbh, $schema, $tables) = @_;
11  
12 0 my %primary;
13 my %unique;
14  
15 0 my $sth = $dbh->prepare_cached(q{
16 SELECT c.constraint_name, c.constraint_type, c.table_name,
17 cc.column_name, cc.position
18 FROM all_constraints c
19 JOIN all_cons_columns cc
20 ON c.constraint_name = cc.constraint_name
21 AND c.owner = cc.owner
22 WHERE c.owner = ?
23 AND c.constraint_type IN ('P', 'U')
24 AND c.table_name NOT LIKE 'BIN$%'
25 ORDER BY c.table_name, c.constraint_type, c.constraint_name, cc.position
26 });
27 0 $sth->execute($schema);
28  
29 0 my %pk_cols; # table => [cols] in position order
30 my %uniq_cols; # table => { constraint => [cols] }
31  
32 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
33 0 my $tbl = $row->{table_name};
34 0 ▶ next unless exists $tables->{$tbl};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tables->{$tbl} 0 0 0
35  
36 0 ▶ if ($row->{constraint_type} eq 'P') {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{'constraint_type'} eq 'P') { } 0 0 0
37 0 push @{ $pk_cols{$tbl} }, $row->{column_name};
38 }
39 else {
40 0 push @{ $uniq_cols{$tbl}{ $row->{constraint_name} } }, $row->{column_name};
41 }
42 }
43 0 $sth->finish;
44  
45 0 for my $tbl (keys %$tables) {
46 0 ▶ $primary{$tbl} = $pk_cols{$tbl} // [];
Condition0%0/2
$pk_cols{$tbl} // []
l!l
00
MC/DC0%0/1
$pk_cols{$tbl} // []
$pk_cols{$tbl}
Truth table0%0/2
A
A= $pk_cols{$tbl}
Aresult
11
00
47  
48 0 ▶ my $by_name = $uniq_cols{$tbl} // {};
Condition0%0/2
$uniq_cols{$tbl} // {}
l!l
00
MC/DC0%0/1
$uniq_cols{$tbl} // {}
$uniq_cols{$tbl}
Truth table0%0/2
A
A= $uniq_cols{$tbl}
Aresult
11
00
49 0 $unique{$tbl} = [ map { [ $_ => $by_name->{$_} ] } sort keys %$by_name ];
50 }
51  
52 return {
53 0 primary => \%primary,
54 unique => \%unique,
55 };
56 }
57  
58 1 1;
59  
60 __END__
61  
62 =pod
63  
64 =encoding UTF-8
65  
66 =head1 NAME
67  
68 DBIO::Oracle::Introspect::Keys - Introspect Oracle primary key and unique constraints
69  
70 =head1 VERSION
71  
72 version 0.900001
73  
74 =head1 DESCRIPTION
75  
76 Fetches Oracle primary key (C<P>) and unique (C<U>) constraint metadata via
77 C<all_constraints> and C<all_cons_columns>. The L<DBIO::Oracle::Introspect::Indexes>
78 submodule deliberately excludes constraint-backed indexes, so this module is
79 the source of primary/unique key information for the normalized generation
80 contract in L<DBIO::Introspect::Base>.
81  
82 =head1 METHODS
83  
84 =head2 fetch
85  
86 my $keys = DBIO::Oracle::Introspect::Keys->fetch($dbh, $schema, $tables);
87  
88 Given the tables hashref from L<DBIO::Oracle::Introspect::Tables>, returns a
89 hashref:
90  
91 {
92 primary => { $table => [ @ordered_pk_columns ] },
93 unique => { $table => [ [ $constraint_name, [ @columns ] ], ... ] },
94 }
95  
96 Both sub-hashes contain an entry for every known table (empty arrayref when
97 the table has no such constraint).
98  
99 =head1 AUTHOR
100  
101 DBIO & DBIx::Class Authors
102  
103 =head1 COPYRIGHT AND LICENSE
104  
105 Copyright (C) 2026 DBIO Authors
106 Portions Copyright (C) 2005-2025 DBIx::Class Authors
107 Based on DBIx::Class, heavily modified.
108  
109 This is free software; you can redistribute it and/or modify it under
110 the same terms as the Perl 5 programming language system itself.
111  
112 =cut
« Oracle/Introspect/Indexes.pm ↑ Summary Oracle/SQLMaker.pm »