blib/lib/DBIO/MySQL/Introspect/ForeignKeys.pm

filter: Statementstmt 100% 38 / 38 Branchbran 100% 4 / 4 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 93.4% 57 / 61 CC 8 SCAR 12.4
CC
8
Cov
96%
fetch
5.0
_rule
4.0
BEGIN
1.0
CRAP 3.5
« blib/lib/DBIO/MySQL/Diff/Index.pm ↑ Summary blib/lib/DBIO/MySQL/Introspect/Util.pm »
1 package DBIO::MySQL::Introspect::ForeignKeys;
2 # ABSTRACT: Introspect MySQL/MariaDB foreign keys
3  
4 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 43 our $VERSION = '0.900007';
8  
9 43 ▶use DBIO::Introspect::Base ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 43 ▶use DBIO::MySQL::Introspect::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13  
14 3 ▶sub fetch {
Subroutine fetch: called CC=5 SCAR=16.1
Pod documented
15 3 my ($class, $dbh, $tables) = @_;
16  
17 # On MySQL 8 the information_schema result-column labels come back UPPERCASE
18 # regardless of the case written in the SELECT, while MariaDB returns them
19 # lowercase. Force lowercase hash keys for this fetch so the $row->{lc} reads
20 # below (and _aggregate_by_ordered's 'table_name'/'constraint_name' keys)
21 # resolve on both engines. fetchall_arrayref({}) honours the sth's
22 # FetchHashKeyName, which is captured from the dbh at prepare time.
23 3 local $dbh->{FetchHashKeyName} = 'NAME_lc';
24  
25 3 ▶ my $mariadb = (scalar($dbh->selectrow_array('SELECT VERSION()')) // '') =~ /mariadb/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(scalar $dbh->selectrow_array('SELECT VERSION()') // '') =~ /mariadb/i ? : 2 1 3
Condition50%1/2
scalar $dbh->selectrow_array("SELECT VERSION()") // ""
l!l
30
MC/DC0%0/1
scalar $dbh->selectrow_array("SELECT VERSION()") // ""
scalar $dbh->selectrow_array("SELECT VERSION()")
Truth table50%1/2
A
A= scalar $dbh->selectrow_array("SELECT VERSION()")
Aresult
11
00
26  
27 3 my $sth = $dbh->prepare(q{
28 SELECT
29 kcu.table_name,
30 kcu.constraint_name,
31 kcu.column_name,
32 kcu.referenced_table_name,
33 kcu.referenced_column_name,
34 kcu.ordinal_position,
35 rc.update_rule,
36 rc.delete_rule
37 FROM information_schema.key_column_usage kcu
38 JOIN information_schema.referential_constraints rc
39 ON rc.constraint_schema = kcu.table_schema
40 AND rc.constraint_name = kcu.constraint_name
41 WHERE kcu.table_schema = DATABASE()
42 AND kcu.referenced_table_name IS NOT NULL
43 ORDER BY kcu.table_name, kcu.constraint_name, kcu.ordinal_position
44 });
45 3 $sth->execute;
46  
47 3 my $rows = $sth->fetchall_arrayref({});
48 3 @$rows = grep { DBIO::MySQL::Introspect::Util->keep_table($tables, $_->{table_name}) } @$rows;
49  
50 # Group rows by table (preserving first-seen order) -- _aggregate_by_ordered
51 # gives us an array of [ table, [ rows ] ] pairs, so the order in which
52 # tables appear in the source DB is preserved.
53 3 my $by_table = DBIO::Introspect::Base->_aggregate_by_ordered($rows, 'table_name');
54  
55 3 my %fks;
56 3 for my $pair (@$by_table) {
57 3 my ($tbl, $tbl_rows) = @$pair;
58  
59 # Within a table, group by constraint_name (also ordered, also first-seen
60 # first). All rows of one constraint share its to_table / on_update /
61 # on_delete, so we can lift those from any row -- the first one is fine.
62 3 my $by_constraint = DBIO::Introspect::Base->_aggregate_by_ordered($tbl_rows, 'constraint_name');
63  
64 3 for my $ck_pair (@$by_constraint) {
65 11 my ($name, $ck_rows) = @$ck_pair;
66 11 my $head = $ck_rows->[0];
67 11 push @{ $fks{$tbl} }, {
68 constraint_name => $name,
69 11 from_columns => [ map { $_->{column_name} } @$ck_rows ],
70 to_table => $head->{referenced_table_name},
71 11 to_columns => [ map { $_->{referenced_column_name} } @$ck_rows ],
72 on_update => _rule($head->{update_rule}, $mariadb),
73 11 on_delete => _rule($head->{delete_rule}, $mariadb),
74 };
75 }
76 }
77  
78 3 return \%fks;
79 }
80  
81 # NO ACTION and RESTRICT are one action on a MariaDB (karr k116), two on a MySQL.
82 22 ▶sub _rule {
Subroutine _rule: called CC=4 SCAR=13.9
83 22 my ($rule, $mariadb) = @_;
84 22 ▶ return 'RESTRICT' if $mariadb && defined $rule && uc $rule eq 'NO ACTION';
Branch: 1 branch
BranchTrueFalseTotal
if $mariadb and defined $rule and uc $rule eq "NO ACTION" 6 16 22
Condition83%5/6
$mariadb and defined $rule and uc $rule eq "NO ACTION"
!ll&&!rl&&r
1066
$mariadb and defined $rule
!ll&&!rl&&r
10012
MC/DC66%2/3
$mariadb and defined $rule and uc $rule eq "NO ACTION"
$mariadb defined $rule uc $rule eq "NO ACTION"
Truth table75%3/4
A and B and C
A= $mariadbB= defined $ruleC= uc $rule eq "NO ACTION"
ABCresult
0XX0
10X0
1100
1111
85 16 return $rule;
86 }
87  
88 43 1;
89  
90 __END__
« blib/lib/DBIO/MySQL/Diff/Index.pm ↑ Summary blib/lib/DBIO/MySQL/Introspect/Util.pm »