| 1 | package DBIO::PostgreSQL::Introspect::ForeignKeys; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect PostgreSQL foreign keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 171 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 171 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 171 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 171 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 171 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 171 | ▶ | use base 'DBIO::Introspect::Base'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | my ($class, $dbh, $filter) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | my %pg_rules = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | a => 'NO ACTION', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | r => 'RESTRICT', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | c => 'CASCADE', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | n => 'SET NULL', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | d => 'SET DEFAULT', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | my %pg_match = (s => 'SIMPLE', f => 'FULL', p => 'PARTIAL'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # confdelsetcols exists from PostgreSQL 15 on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | my $delcols = ($dbh->{pg_server_version} // 0) >= 150000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$dbh->{"pg_server_version"} // 0
MC/DC0%0/1
$dbh->{"pg_server_version"} // 0
$dbh->{"pg_server_version"}
Truth table0%0/2
A
A= $dbh->{"pg_server_version"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | ? q{( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | SELECT pg_catalog.jsonb_agg(da.attname ORDER BY dk.n) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | FROM pg_catalog.unnest(constr.confdelsetcols) WITH ORDINALITY AS dk(attnum, n) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | JOIN pg_catalog.pg_attribute da | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ON da.attrelid = constr.conrelid AND da.attnum = dk.attnum | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | )} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | : q{NULL}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | my $sql = qq{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | SELECT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | from_ns.nspname AS local_schema, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | from_class.relname AS local_table, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | constr.conname AS constraint_name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | to_ns.nspname AS remote_schema, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | to_class.relname AS remote_table, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ord.n AS key_seq, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | from_col.attname AS local_column, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | to_col.attname AS remote_column, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | constr.confdeltype AS on_delete, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | constr.confupdtype AS on_update, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | constr.condeferrable AS is_deferrable, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | constr.condeferred AS is_deferred, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | constr.convalidated AS is_valid, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | constr.confmatchtype AS match_type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | $delcols AS on_delete_columns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | FROM pg_catalog.pg_constraint constr | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | JOIN pg_catalog.pg_class from_class ON from_class.oid = constr.conrelid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | JOIN pg_catalog.pg_namespace from_ns ON from_ns.oid = from_class.relnamespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | JOIN pg_catalog.pg_class to_class ON to_class.oid = constr.confrelid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | JOIN pg_catalog.pg_namespace to_ns ON to_ns.oid = to_class.relnamespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | JOIN pg_catalog.generate_subscripts(constr.conkey, 1) AS ord(n) ON true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | JOIN pg_catalog.pg_attribute from_col | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | ON from_col.attrelid = constr.conrelid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | AND from_col.attnum = constr.conkey[ord.n] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | JOIN pg_catalog.pg_attribute to_col | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | ON to_col.attrelid = constr.confrelid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | AND to_col.attnum = constr.confkey[ord.n] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | WHERE constr.contype = 'f' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | AND constr.conparentid = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('from_ns'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $sql .= DBIO::PostgreSQL::Introspect->extension_member_filter('pg_class', 'from_class.oid'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my @bind; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($filter && @$filter) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$filter and @$filter
MC/DC0%0/2
$filter and @$filter
$filter
@$filter
Truth table0%0/3
A and B
A= $filterB= @$filter
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $sql .= ' AND from_ns.nspname = ANY($1)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $sql .= q{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | ORDER BY from_ns.nspname, from_class.relname, constr.conname, ord.n | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | my @rows; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | push @rows, $row; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | return $class->_build_foreign_keys(\@rows, \%pg_rules, \%pg_match); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # Two-level grouping: first by local schema, then by local table, then by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # constraint. We could call _aggregate_by on a composite key, but no such | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # helper exists; the cheap alternative is one grouping by schema, then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | # another by table within each schema (tables only ever have one schema, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # so this is straightforward). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub _build_foreign_keys { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_foreign_keys: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my ($class, $rows, $pg_rules, $pg_match) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $pg_match //= { s => 'SIMPLE', f => 'FULL', p => 'PARTIAL' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$pg_match //= {"s", "SIMPLE", "f", "FULL", "p", "PARTIAL"}
MC/DC0%0/1
$pg_match //= {"s", "SIMPLE", "f", "FULL", "p", "PARTIAL"}
$pg_match
Truth table0%0/2
A
A= $pg_match
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $json = JSON::MaybeXS->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | my $by_schema = $class->_aggregate_by_ordered($rows, 'local_schema'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my %foreign_keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | for my $s_pair (@$by_schema) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my ($schema, $schema_rows) = @$s_pair; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my $by_table = $class->_aggregate_by_ordered($schema_rows, 'local_table'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | for my $t_pair (@$by_table) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my ($table, $table_rows) = @$t_pair; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my $table_key = DBIO::PostgreSQL::Introspect->qualified_key($schema, $table); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $by_constraint = $class->_aggregate_by_ordered($table_rows, 'constraint_name'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | for my $c_pair (@$by_constraint) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my ($constraint_name, $c_rows) = @$c_pair; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $first = $c_rows->[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | my $entry = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | constraint_name => $constraint_name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | remote_schema => $first->{remote_schema}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | remote_table => $first->{remote_table}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | local_columns => [map { $_->{local_column} } @$c_rows], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | remote_columns => [map { $_->{remote_column} } @$c_rows], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
Condition0%0/6
$first->{"is_valid"} // 1
$$pg_match{$first->{"match_type"} // "s"} // "SIMPLE"
$first->{"match_type"} // "s"
MC/DC0%0/1
$first->{"is_valid"} // 1
$first->{"is_valid"}
MC/DC0%0/1
$$pg_match{$first->{"match_type"} // "s"} // "SIMPLE"
$$pg_match{$first->{"match_type"} // "s"}
MC/DC0%0/1
$first->{"match_type"} // "s"
$first->{"match_type"}
Truth table0%0/2
A
A= $first->{"is_valid"}
Truth table0%0/2
A
A= $$pg_match{$first->{"match_type"} // "s"}
Truth table0%0/2
A
A= $first->{"match_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | on_delete => $pg_rules->{ $first->{on_delete} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | on_update => $pg_rules->{ $first->{on_update} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | is_deferrable => $first->{is_deferrable} ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | deferrable => $first->{is_deferrable} ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | initially_deferred => $first->{is_deferred} ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | is_valid => ($first->{is_valid} // 1) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | match => $pg_match->{ $first->{match_type} // 's' } // 'SIMPLE', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | on_delete_columns => defined $first->{on_delete_columns} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ? $json->decode($first->{on_delete_columns}) : [], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | push @{ $foreign_keys{$table_key} }, $entry; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | return \%foreign_keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 171 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||