MySQL/Introspect/ForeignKeys.pm

filter: Statementstmt 39.3% 13 / 33 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 46.1% 18 / 39 CC 3 SCAR 17.4
CC
3
Cov
39%
fetch
12.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.7
« MySQL/Storage/MariaDB.pm ↑ Summary MySQL/Adapter.pm »
1 package DBIO::MySQL::Introspect::ForeignKeys;
2 # ABSTRACT: Introspect MySQL/MariaDB foreign keys
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use DBIO::Introspect::Base ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use DBIO::MySQL::Introspect::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11  
12 0 ▶sub fetch {
Subroutine fetch: not called CC=3 SCAR=24.8
Pod documented
13 0 my ($class, $dbh, $tables) = @_;
14  
15 # On MySQL 8 the information_schema result-column labels come back UPPERCASE
16 # regardless of the case written in the SELECT, while MariaDB returns them
17 # lowercase. Force lowercase hash keys for this fetch so the $row->{lc} reads
18 # below (and _aggregate_by_ordered's 'table_name'/'constraint_name' keys)
19 # resolve on both engines. fetchall_arrayref({}) honours the sth's
20 # FetchHashKeyName, which is captured from the dbh at prepare time.
21 0 local $dbh->{FetchHashKeyName} = 'NAME_lc';
22  
23 0 my $sth = $dbh->prepare(q{
24 SELECT
25 kcu.table_name,
26 kcu.constraint_name,
27 kcu.column_name,
28 kcu.referenced_table_name,
29 kcu.referenced_column_name,
30 kcu.ordinal_position,
31 rc.update_rule,
32 rc.delete_rule
33 FROM information_schema.key_column_usage kcu
34 JOIN information_schema.referential_constraints rc
35 ON rc.constraint_schema = kcu.table_schema
36 AND rc.constraint_name = kcu.constraint_name
37 WHERE kcu.table_schema = DATABASE()
38 AND kcu.referenced_table_name IS NOT NULL
39 ORDER BY kcu.table_name, kcu.constraint_name, kcu.ordinal_position
40 });
41 0 $sth->execute;
42  
43 0 my $rows = $sth->fetchall_arrayref({});
44 0 @$rows = grep { DBIO::MySQL::Introspect::Util->keep_table($tables, $_->{table_name}) } @$rows;
45  
46 # Group rows by table (preserving first-seen order) -- _aggregate_by_ordered
47 # gives us an array of [ table, [ rows ] ] pairs, so the order in which
48 # tables appear in the source DB is preserved.
49 0 my $by_table = DBIO::Introspect::Base->_aggregate_by_ordered($rows, 'table_name');
50  
51 0 my %fks;
52 0 for my $pair (@$by_table) {
53 0 my ($tbl, $tbl_rows) = @$pair;
54  
55 # Within a table, group by constraint_name (also ordered, also first-seen
56 # first). All rows of one constraint share its to_table / on_update /
57 # on_delete, so we can lift those from any row -- the first one is fine.
58 0 my $by_constraint = DBIO::Introspect::Base->_aggregate_by_ordered($tbl_rows, 'constraint_name');
59  
60 0 for my $ck_pair (@$by_constraint) {
61 0 my ($name, $ck_rows) = @$ck_pair;
62 0 my $head = $ck_rows->[0];
63 0 push @{ $fks{$tbl} }, {
64 constraint_name => $name,
65 0 from_columns => [ map { $_->{column_name} } @$ck_rows ],
66 to_table => $head->{referenced_table_name},
67 0 to_columns => [ map { $_->{referenced_column_name} } @$ck_rows ],
68 on_update => $head->{update_rule},
69 on_delete => $head->{delete_rule},
70 0 };
71 }
72 }
73  
74 0 return \%fks;
75 }
76  
77 4 1;
78  
79 __END__
80  
81 =pod
82  
83 =encoding UTF-8
84  
85 =head1 NAME
86  
87 DBIO::MySQL::Introspect::ForeignKeys - Introspect MySQL/MariaDB foreign keys
88  
89 =head1 VERSION
90  
91 version 0.900001
92  
93 =head1 DESCRIPTION
94  
95 Fetches foreign key metadata by joining
96 C<information_schema.key_column_usage> against
97 C<information_schema.referential_constraints>, scoped to the current
98 C<DATABASE()> and filtered to the tables surfaced by
99 L<DBIO::MySQL::Introspect::Tables>. Composite foreign keys are grouped per
100 constraint via L<DBIO::Introspect::Base/_aggregate_by_ordered> (preserves
101 both first-seen table/constraint order and the column pairing within each
102 constraint).
103  
104 =head1 METHODS
105  
106 =head2 fetch
107  
108 my $fks = DBIO::MySQL::Introspect::ForeignKeys->fetch($dbh, $tables);
109  
110 Given the tables hashref from L<DBIO::MySQL::Introspect::Tables>, returns a
111 hashref keyed by table name. Each value is an arrayref of foreign key
112 hashrefs (one per constraint) with keys: C<constraint_name>,
113 C<from_columns> (arrayref), C<to_table>, C<to_columns> (arrayref),
114 C<on_update>, C<on_delete>.
115  
116 =head1 AUTHOR
117  
118 DBIO & DBIx::Class Authors
119  
120 =head1 COPYRIGHT AND LICENSE
121  
122 Copyright (C) 2026 DBIO Authors
123 Portions Copyright (C) 2005-2025 DBIx::Class Authors
124 Based on DBIx::Class, heavily modified.
125  
126 This is free software; you can redistribute it and/or modify it under
127 the same terms as the Perl 5 programming language system itself.
128  
129 =cut
« MySQL/Storage/MariaDB.pm ↑ Summary MySQL/Adapter.pm »