PostgreSQL/Introspect/ForeignKeys.pm

filter: Statementstmt 27.6% 13 / 47 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 4 / 6 Podpod 100% 1 / 1 totaltotal 27.6% 18 / 65 CC 8 SCAR 28.9
CC
8
Cov
23%
_build_foreign_keys
30.0
fetch
20.0
BEGIN
1.0
CRAP 18.0
« PostgreSQL/Introspect/Sequences.pm ↑ Summary PostgreSQL/Storage.pm »
1 package DBIO::PostgreSQL::Introspect::ForeignKeys;
2 # ABSTRACT: Introspect PostgreSQL foreign keys
3  
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use DBIO::PostgreSQL::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 15 ▶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=4 SCAR=30.0
Pod documented
14 0 my ($class, $dbh, $filter) = @_;
15  
16 0 my %pg_rules = (
17 a => 'NO ACTION',
18 r => 'RESTRICT',
19 c => 'CASCADE',
20 n => 'SET NULL',
21 d => 'SET DEFAULT',
22 );
23  
24 0 my $sql = q{
25 SELECT
26 from_ns.nspname AS local_schema,
27 from_class.relname AS local_table,
28 constr.conname AS constraint_name,
29 to_ns.nspname AS remote_schema,
30 to_class.relname AS remote_table,
31 ord.n AS key_seq,
32 from_col.attname AS local_column,
33 to_col.attname AS remote_column,
34 constr.confdeltype AS on_delete,
35 constr.confupdtype AS on_update,
36 constr.condeferrable AS is_deferrable
37 FROM pg_catalog.pg_constraint constr
38 JOIN pg_catalog.pg_class from_class ON from_class.oid = constr.conrelid
39 JOIN pg_catalog.pg_namespace from_ns ON from_ns.oid = from_class.relnamespace
40 JOIN pg_catalog.pg_class to_class ON to_class.oid = constr.confrelid
41 JOIN pg_catalog.pg_namespace to_ns ON to_ns.oid = to_class.relnamespace
42 JOIN pg_catalog.generate_subscripts(constr.conkey, 1) AS ord(n) ON true
43 JOIN pg_catalog.pg_attribute from_col
44 ON from_col.attrelid = constr.conrelid
45 AND from_col.attnum = constr.conkey[ord.n]
46 JOIN pg_catalog.pg_attribute to_col
47 ON to_col.attrelid = constr.confrelid
48 AND to_col.attnum = constr.confkey[ord.n]
49 WHERE constr.contype = 'f'
50 };
51 0 $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('from_ns');
52  
53 0 my @bind;
54 0 ▶ if ($filter && @$filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter and @$filter) 0 0 0
Condition0%0/3
$filter and @$filter
!ll&&!rl&&r
000
MC/DC0%0/2
$filter and @$filter
$filter @$filter
Truth table0%0/3
A and B
A= $filterB= @$filter
ABresult
0X0
100
111
55 0 $sql .= ' AND from_ns.nspname = ANY($1)';
56 0 push @bind, $filter;
57 }
58  
59 0 $sql .= q{
60 ORDER BY from_ns.nspname, from_class.relname, constr.conname, ord.n
61 };
62  
63 0 my $sth = $dbh->prepare($sql);
64 0 $sth->execute(@bind);
65  
66 0 my @rows;
67 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
68 0 push @rows, $row;
69 }
70  
71 0 return $class->_build_foreign_keys(\@rows, \%pg_rules);
72 }
73  
74 # Two-level grouping: first by local schema, then by local table, then by
75 # constraint. We could call _aggregate_by on a composite key, but no such
76 # helper exists; the cheap alternative is one grouping by schema, then
77 # another by table within each schema (tables only ever have one schema,
78 # so this is straightforward).
79 0 ▶sub _build_foreign_keys {
Subroutine _build_foreign_keys: not called CC=5 SCAR=34.0
80 0 my ($class, $rows, $pg_rules) = @_;
81  
82 0 my $by_schema = $class->_aggregate_by_ordered($rows, 'local_schema');
83 0 my %foreign_keys;
84 0 for my $s_pair (@$by_schema) {
85 0 my ($schema, $schema_rows) = @$s_pair;
86  
87 0 my $by_table = $class->_aggregate_by_ordered($schema_rows, 'local_table');
88 0 for my $t_pair (@$by_table) {
89 0 my ($table, $table_rows) = @$t_pair;
90 0 my $table_key = DBIO::PostgreSQL::Introspect->qualified_key($schema, $table);
91  
92 0 my $by_constraint = $class->_aggregate_by_ordered($table_rows, 'constraint_name');
93 0 for my $c_pair (@$by_constraint) {
94 0 my ($constraint_name, $c_rows) = @$c_pair;
95 0 my $first = $c_rows->[0];
96  
97 my $entry = {
98 constraint_name => $constraint_name,
99 remote_schema => $first->{remote_schema},
100 remote_table => $first->{remote_table},
101 0 local_columns => [map { $_->{local_column} } @$c_rows],
102 0 ▶ remote_columns => [map { $_->{remote_column} } @$c_rows],
Branch: 1 branch
BranchTrueFalseTotal
$first->{'is_deferrable'} ? : 0 0 0
103 on_delete => $pg_rules->{ $first->{on_delete} },
104 on_update => $pg_rules->{ $first->{on_update} },
105 0 is_deferrable => $first->{is_deferrable} ? 1 : 0,
106 };
107 0 push @{ $foreign_keys{$table_key} }, $entry;
108 }
109 }
110 }
111  
112 0 return \%foreign_keys;
113 }
114  
115 15 1;
116  
117 __END__
118  
119 =pod
120  
121 =encoding UTF-8
122  
123 =head1 NAME
124  
125 DBIO::PostgreSQL::Introspect::ForeignKeys - Introspect PostgreSQL foreign keys
126  
127 =head1 VERSION
128  
129 version 0.900001
130  
131 =head1 DESCRIPTION
132  
133 Fetches PostgreSQL foreign key metadata from C<pg_catalog.pg_constraint>,
134 including ordered local and remote columns plus referential actions and
135 deferrability.
136  
137 =head1 METHODS
138  
139 =head2 fetch
140  
141 my $foreign_keys = DBIO::PostgreSQL::Introspect::ForeignKeys->fetch($dbh, $filter);
142  
143 Returns a hashref keyed by C<schema.table>. Each value is an ArrayRef of
144 foreign key hashrefs with keys: C<constraint_name>, C<remote_schema>,
145 C<remote_table>, C<local_columns>, C<remote_columns>, C<on_delete>,
146 C<on_update>, and C<is_deferrable>.
147  
148 =head1 AUTHOR
149  
150 DBIO & DBIx::Class Authors
151  
152 =head1 COPYRIGHT AND LICENSE
153  
154 Copyright (C) 2026 DBIO Authors
155 Portions Copyright (C) 2005-2025 DBIx::Class Authors
156 Based on DBIx::Class, heavily modified.
157  
158 This is free software; you can redistribute it and/or modify it under
159 the same terms as the Perl 5 programming language system itself.
160  
161 =cut
« PostgreSQL/Introspect/Sequences.pm ↑ Summary PostgreSQL/Storage.pm »