| 1 | package DBIO::DuckDB::Introspect::ForeignKeys; | ||||||||||||
| 2 | # ABSTRACT: Introspect DuckDB foreign keys | ||||||||||||
| 3 | |||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 11 | our $VERSION = '0.900007'; | |||||||||||
| 8 | |||||||||||||
| 9 | 11 | ▶ | use DBIO::Introspect::Base (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | |||||||||||||
| 12 | |||||||||||||
| 13 | 0 | ▶ | sub fetch { | ||||||||||
|
Subroutine
fetch: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||
| 14 | 0 | my ($class, $dbh, $schema, $tables, $catalog) = @_; | |||||||||||
| 15 | 0 | ▶ | $schema //= 'main'; | ||||||||||
|
Condition0%0/2
$schema //= "main"
MC/DC0%0/1
$schema //= "main"
$schema
Truth table0%0/2
A
A= $schema
| |||||||||||||
| 16 | 0 | my %fks; | |||||||||||
| 17 | |||||||||||||
| 18 | # The columns come from duckdb_constraints(), which keeps the two column | ||||||||||||
| 19 | # lists of a key in order and side by side. information_schema cannot do | ||||||||||||
| 20 | # that: its constraint_column_usage has no position, and joining it to | ||||||||||||
| 21 | # key_column_usage pairs every key column with every parent column (a | ||||||||||||
| 22 | # composite key came out as n*n columns, and once more for each constraint | ||||||||||||
| 23 | # of the parent on the same columns). The actions and the match option come | ||||||||||||
| 24 | # from information_schema.referential_constraints, by constraint name. | ||||||||||||
| 25 | 0 | my $fk_sql = q{ | |||||||||||
| 26 | SELECT table_name, constraint_name, | ||||||||||||
| 27 | constraint_column_names, referenced_table, referenced_column_names | ||||||||||||
| 28 | FROM duckdb_constraints() | ||||||||||||
| 29 | WHERE constraint_type = 'FOREIGN KEY' AND schema_name = ? | ||||||||||||
| 30 | }; | ||||||||||||
| 31 | 0 | my @fk_bind = ($schema); | |||||||||||
| 32 | |||||||||||||
| 33 | 0 | my $rules_sql = q{ | |||||||||||
| 34 | SELECT constraint_name, update_rule, delete_rule, match_option | ||||||||||||
| 35 | FROM information_schema.referential_constraints | ||||||||||||
| 36 | WHERE constraint_schema = ? | ||||||||||||
| 37 | }; | ||||||||||||
| 38 | 0 | my @rules_bind = ($schema); | |||||||||||
| 39 | |||||||||||||
| 40 | 0 | ▶ | if (defined $catalog) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 0 | $fk_sql .= q{ AND database_name = ?}; | |||||||||||
| 42 | 0 | push @fk_bind, $catalog; | |||||||||||
| 43 | 0 | $rules_sql .= q{ AND constraint_catalog = ?}; | |||||||||||
| 44 | 0 | push @rules_bind, $catalog; | |||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | 0 | $fk_sql .= q{ ORDER BY table_name, constraint_name}; | |||||||||||
| 48 | |||||||||||||
| 49 | 0 | my $sth = $dbh->prepare($fk_sql); | |||||||||||
| 50 | 0 | my $ok = eval { $sth->execute(@fk_bind); 1 }; | |||||||||||
| 51 | 0 | ▶ | return \%fks unless $ok; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | |||||||||||||
| 53 | 0 | my %rule; | |||||||||||
| 54 | { | ||||||||||||
| 55 | 0 | my $rsth = $dbh->prepare($rules_sql); | |||||||||||
| 56 | 0 | ▶ | if (eval { $rsth->execute(@rules_bind); 1 }) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | 0 | ▶ | while (my $row = $rsth->fetchrow_hashref) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 0 | $rule{ $row->{constraint_name} } = $row; | |||||||||||
| 59 | } | ||||||||||||
| 60 | } | ||||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 0 | ▶ | next unless exists $tables->{ $row->{table_name} }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 0 | ▶ | my $rule = $rule{ $row->{constraint_name} } || {}; | ||||||||||
|
Condition0%0/2
$rule{$row->{'constraint_name'}} || {}
MC/DC0%0/1
$rule{$row->{'constraint_name'}} || {}
$rule{$row->{'constraint_name'}}
Truth table0%0/2
A
A= $rule{$row->{'constraint_name'}}
| |||||||||||||
| 66 | 0 | push @{ $fks{ $row->{table_name} } }, { | |||||||||||
| 67 | constraint_name => $row->{constraint_name}, | ||||||||||||
| 68 | from_table => $row->{table_name}, | ||||||||||||
| 69 | 0 | from_columns => [ @{ $row->{constraint_column_names} } ], | |||||||||||
| 70 | to_table => $row->{referenced_table}, | ||||||||||||
| 71 | 0 | to_columns => [ @{ $row->{referenced_column_names} } ], | |||||||||||
| 72 | on_update => $rule->{update_rule}, | ||||||||||||
| 73 | on_delete => $rule->{delete_rule}, | ||||||||||||
| 74 | match => $rule->{match_option}, | ||||||||||||
| 75 | 0 | }; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | 0 | return \%fks; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 11 | 1; | |||||||||||
| 82 | |||||||||||||
| 83 | __END__ | ||||||||||||