DuckDB/Introspect/ForeignKeys.pm

filter: Statementstmt 26.3% 10 / 38 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 25.9% 14 / 54 CC 7 SCAR 36.8
CC
7
Cov
21%
fetch
56.0
BEGIN
1.0
BEGIN
1.0
CRAP 39.5
« DuckDB/Diff/Column.pm ↑ Summary DuckDB/Diff/Table.pm »
1 package DBIO::DuckDB::Introspect::ForeignKeys;
2 # ABSTRACT: Introspect DuckDB foreign keys
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use DBIO::Introspect::Base ();
Subroutine BEGIN: called CC=1 SCAR=0.0
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, $catalog) = @_;
13 0 ▶ $schema //= 'main';
Condition0%0/2
$schema //= "main"
l!l
00
MC/DC0%0/1
$schema //= "main"
$schema
Truth table0%0/2
A
A= $schema
Aresult
11
00
14 0 my %fks;
15  
16 0 my $fk_sql = q{
17 SELECT
18 rc.constraint_name,
19 kcu.table_name AS from_table,
20 kcu.column_name AS from_column,
21 kcu.ordinal_position AS from_pos,
22 ccu.table_name AS to_table,
23 ccu.column_name AS to_column,
24 rc.update_rule,
25 rc.delete_rule,
26 rc.match_option
27 FROM information_schema.referential_constraints rc
28 JOIN information_schema.table_constraints tc
29 ON rc.constraint_name = tc.constraint_name
30 AND rc.constraint_schema = tc.constraint_schema
31 JOIN information_schema.key_column_usage kcu
32 ON rc.constraint_name = kcu.constraint_name
33 AND rc.constraint_schema = kcu.constraint_schema
34 JOIN information_schema.constraint_column_usage ccu
35 ON rc.unique_constraint_name = ccu.constraint_name
36 AND rc.unique_constraint_schema = ccu.constraint_schema
37 WHERE kcu.table_schema = ?
38 };
39 0 my @fk_bind = ($schema);
40  
41 0 ▶ if (defined $catalog) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $catalog) 0 0 0
42 0 $fk_sql .= q{ AND tc.table_catalog = ? AND rc.constraint_catalog = ?};
43 0 push @fk_bind, $catalog, $catalog;
44 }
45  
46 0 $fk_sql .= q{ ORDER BY kcu.table_name, rc.constraint_name, kcu.ordinal_position};
47  
48 0 my $sth = $dbh->prepare($fk_sql);
49  
50 0 my $ok = eval { $sth->execute(@fk_bind); 1 };
51 0 ▶ return \%fks unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
52  
53 0 my @rows;
54 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
55 0 ▶ next unless exists $tables->{ $row->{from_table} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$tables{$row->{"from_table"}} 0 0 0
56 0 push @rows, $row;
57 }
58  
59 # Group the (already table/constraint/ordinal-ordered) key-column rows by
60 # constraint name, preserving column order within each constraint. The
61 # query's ORDER BY keeps groups for distinct constraints contiguous and in
62 # first-seen order, so the aggregated output is deterministic.
63 0 my $groups = DBIO::Introspect::Base->_aggregate_by_ordered(\@rows, 'constraint_name');
64  
65 0 for my $pair (@$groups) {
66 0 my ($name, $group_rows) = @$pair;
67 0 my $first = $group_rows->[0];
68 my $fk = {
69 fk_id => $name,
70 from_table => $first->{from_table},
71 0 from_columns => [ map { $_->{from_column} } @$group_rows ],
72 to_table => $first->{to_table},
73 0 to_columns => [ map { $_->{to_column} } @$group_rows ],
74 on_update => $first->{update_rule},
75 on_delete => $first->{delete_rule},
76 match => $first->{match_option},
77 0 };
78 0 push @{ $fks{ $fk->{from_table} } }, $fk;
79 }
80  
81 0 return \%fks;
82 }
83  
84 5 1;
85  
86 __END__
87  
88 =pod
89  
90 =encoding UTF-8
91  
92 =head1 NAME
93  
94 DBIO::DuckDB::Introspect::ForeignKeys - Introspect DuckDB foreign keys
95  
96 =head1 VERSION
97  
98 version 0.900001
99  
100 =head1 DESCRIPTION
101  
102 Fetches foreign-key metadata via C<information_schema.referential_constraints>
103 joined against C<key_column_usage>. Composite FKs are grouped by
104 constraint name.
105  
106 DuckDB accepts FK declarations but does not enforce them as of 1.x. They
107 are still round-tripped here for schema correctness.
108  
109 =head1 METHODS
110  
111 =head2 fetch
112  
113 my $fks = DBIO::DuckDB::Introspect::ForeignKeys->fetch($dbh, $schema, $tables);
114 my $fks = DBIO::DuckDB::Introspect::ForeignKeys->fetch($dbh, $schema, $tables, $catalog);
115  
116 Returns a hashref keyed by table name, each value an arrayref of FK
117 hashrefs with: C<fk_id>, C<from_columns>, C<to_table>, C<to_columns>,
118 C<on_update>, C<on_delete>, C<match>.
119  
120 When C<$catalog> is defined, C<AND tc.table_catalog = ?> and
121 C<AND rc.constraint_catalog = ?> clauses are added.
122  
123 =head1 AUTHOR
124  
125 DBIO Authors
126  
127 =head1 COPYRIGHT AND LICENSE
128  
129 Copyright (C) 2026 DBIO Authors
130  
131 This is free software; you can redistribute it and/or modify it under
132 the same terms as the Perl 5 programming language system itself.
133  
134 =cut
« DuckDB/Diff/Column.pm ↑ Summary DuckDB/Diff/Table.pm »