MSSQL/Introspect/ForeignKeys.pm

filter: Statementstmt 26.9% 7 / 26 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 66.6% 2 / 3 Podpod 100% 1 / 1 totaltotal 22.2% 10 / 45 CC 8 SCAR 40.6
CC
8
Cov
18%
fetch
72.0
BEGIN
1.0
BEGIN
1.0
CRAP 57.8
« MSSQL/Introspect/Columns.pm ↑ Summary MSSQL/Introspect/Indexes.pm »
1 package DBIO::MSSQL::Introspect::ForeignKeys;
2 # ABSTRACT: Introspect MSSQL foreign keys
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8  
9 0 ▶sub fetch {
Subroutine fetch: not called CC=8 SCAR=42.8
Pod documented
10 0 my ($class, $dbh, $schema, $tables) = @_;
11 0 ▶ $schema //= 'dbo';
Condition0%0/2
$schema //= "dbo"
l!l
00
MC/DC0%0/1
$schema //= "dbo"
$schema
Truth table0%0/2
A
A= $schema
Aresult
11
00
12 0 my %fks;
13  
14 0 my $sth = $dbh->prepare(q{
15 SELECT rc.constraint_name,
16 fk_kcu.table_name AS from_table,
17 uk_tc.table_name AS to_table,
18 fk_kcu.column_name AS from_column,
19 uk_kcu.column_name AS to_column,
20 rc.delete_rule,
21 rc.update_rule
22 FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
23 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS fk_tc
24 ON rc.constraint_name = fk_tc.constraint_name
25 AND rc.constraint_schema = fk_tc.table_schema
26 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE fk_kcu
27 ON fk_kcu.constraint_name = rc.constraint_name
28 AND fk_kcu.table_schema = rc.constraint_schema
29 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS uk_tc
30 ON rc.unique_constraint_name = uk_tc.constraint_name
31 AND rc.unique_constraint_schema = uk_tc.table_schema
32 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE uk_kcu
33 ON uk_kcu.constraint_name = rc.unique_constraint_name
34 AND uk_kcu.ordinal_position = fk_kcu.ordinal_position
35 WHERE fk_tc.table_schema = ?
36 ORDER BY rc.constraint_name, fk_kcu.ordinal_position
37 });
38 0 $sth->execute($schema);
39  
40 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
41 0 ▶ next unless exists $tables->{ $row->{from_table} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$tables{$row->{"from_table"}} 0 0 0
42 0 my $fk_key = "$row->{from_table}:$row->{constraint_name}";
43 0 ▶ if (!exists $fks{$fk_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fks{$fk_key}) 0 0 0
44 $fks{$fk_key} = {
45 constraint_name => $row->{constraint_name},
46 from_table => $row->{from_table},
47 from_columns => [],
48 to_table => $row->{to_table},
49 to_columns => [],
50 on_delete => uc($row->{delete_rule} // 'NO ACTION'),
51 0 ▶ on_update => uc($row->{update_rule} // 'NO ACTION'),
Condition0%0/4
$row->{"delete_rule"} // "NO ACTION"
l!l
00
$row->{"update_rule"} // "NO ACTION"
l!l
00
MC/DC0%0/1
$row->{"delete_rule"} // "NO ACTION"
$row->{"delete_rule"}
MC/DC0%0/1
$row->{"update_rule"} // "NO ACTION"
$row->{"update_rule"}
Truth table0%0/2
A
A= $row->{"delete_rule"}
Aresult
11
00
Truth table0%0/2
A
A= $row->{"update_rule"}
Aresult
11
00
52 is_deferrable => 1,
53 };
54 }
55 0 push @{ $fks{$fk_key}{from_columns} }, $row->{from_column};
56 0 push @{ $fks{$fk_key}{to_columns} }, $row->{to_column};
57 }
58  
59 # Re-index by table name.
60 0 my %by_table;
61 0 for my $fk (values %fks) {
62 0 push @{ $by_table{ $fk->{from_table} } }, $fk;
63 }
64  
65 0 return \%by_table;
66 }
67  
68 2 1;
69  
70 __END__
71  
72 =pod
73  
74 =encoding UTF-8
75  
76 =head1 NAME
77  
78 DBIO::MSSQL::Introspect::ForeignKeys - Introspect MSSQL foreign keys
79  
80 =head1 VERSION
81  
82 version 0.900001
83  
84 =head1 DESCRIPTION
85  
86 Fetches foreign key metadata via C<INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS>
87 and related views.
88  
89 =head1 METHODS
90  
91 =head2 fetch
92  
93 my $fks = DBIO::MSSQL::Introspect::ForeignKeys->fetch($dbh, $schema, $tables);
94  
95 Given the tables hashref from L<DBIO::MSSQL::Introspect::Tables>,
96 returns a hashref keyed by table name. Each value is an arrayref of
97 foreign key hashrefs with keys: C<constraint_name>, C<from_columns>,
98 C<to_columns>, C<to_table>, C<to_schema>, C<on_delete>, C<on_update>,
99 C<is_deferrable>.
100  
101 =head1 AUTHOR
102  
103 DBIO & DBIx::Class Authors
104  
105 =head1 COPYRIGHT AND LICENSE
106  
107 Copyright (C) 2026 DBIO Authors
108 Portions Copyright (C) 2005-2025 DBIx::Class Authors
109 Based on DBIx::Class, heavily modified.
110  
111 This is free software; you can redistribute it and/or modify it under
112 the same terms as the Perl 5 programming language system itself.
113  
114 =cut
« MSSQL/Introspect/Columns.pm ↑ Summary MSSQL/Introspect/Indexes.pm »