| 1 | package DBIO::MSSQL::Introspect::ForeignKeys; | ||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect MSSQL foreign keys | ||||||||||||||||||||||
| 3 | |||||||||||||||||||||||
| 4 | 28 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 28 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 28 | our $VERSION = '0.900007'; | |||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 1 | ▶ | sub fetch { | ||||||||||||||||||||
|
Subroutine
fetch: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||
| 12 | 1 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||||
| 13 | 1 | ▶ | $schema //= 'dbo'; | ||||||||||||||||||||
|
Condition50%1/2
$schema //= "dbo"
MC/DC0%0/1
$schema //= "dbo"
$schema
Truth table50%1/2
A
A= $schema
| |||||||||||||||||||||||
| 14 | 1 | my %fks; | |||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | # Every column is aliased explicitly and in lower case: an alias is echoed | ||||||||||||||||||||||
| 17 | # into the result metadata verbatim, which is the one way to be sure of the | ||||||||||||||||||||||
| 18 | # hash keys below without depending on the handle's FetchHashKeyName. | ||||||||||||||||||||||
| 19 | # INFORMATION_SCHEMA reports its columns UPPERCASE; unaliased, the key case | ||||||||||||||||||||||
| 20 | # in the row hash is decided by the transport, not by this code. | ||||||||||||||||||||||
| 21 | 1 | my $sth = $dbh->prepare(q{ | |||||||||||||||||||||
| 22 | SELECT rc.constraint_name AS constraint_name, | ||||||||||||||||||||||
| 23 | fk_kcu.table_name AS from_table, | ||||||||||||||||||||||
| 24 | uk_tc.table_name AS to_table, | ||||||||||||||||||||||
| 25 | uk_tc.table_schema AS to_schema, | ||||||||||||||||||||||
| 26 | fk_kcu.column_name AS from_column, | ||||||||||||||||||||||
| 27 | uk_kcu.column_name AS to_column, | ||||||||||||||||||||||
| 28 | rc.delete_rule AS delete_rule, | ||||||||||||||||||||||
| 29 | rc.update_rule AS update_rule | ||||||||||||||||||||||
| 30 | FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc | ||||||||||||||||||||||
| 31 | JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS fk_tc | ||||||||||||||||||||||
| 32 | ON rc.constraint_name = fk_tc.constraint_name | ||||||||||||||||||||||
| 33 | AND rc.constraint_schema = fk_tc.table_schema | ||||||||||||||||||||||
| 34 | JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE fk_kcu | ||||||||||||||||||||||
| 35 | ON fk_kcu.constraint_name = rc.constraint_name | ||||||||||||||||||||||
| 36 | AND fk_kcu.table_schema = rc.constraint_schema | ||||||||||||||||||||||
| 37 | JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS uk_tc | ||||||||||||||||||||||
| 38 | ON rc.unique_constraint_name = uk_tc.constraint_name | ||||||||||||||||||||||
| 39 | AND rc.unique_constraint_schema = uk_tc.table_schema | ||||||||||||||||||||||
| 40 | JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE uk_kcu | ||||||||||||||||||||||
| 41 | ON uk_kcu.constraint_name = rc.unique_constraint_name | ||||||||||||||||||||||
| 42 | AND uk_kcu.constraint_schema = rc.unique_constraint_schema | ||||||||||||||||||||||
| 43 | AND uk_kcu.ordinal_position = fk_kcu.ordinal_position | ||||||||||||||||||||||
| 44 | WHERE fk_tc.table_schema = ? | ||||||||||||||||||||||
| 45 | ORDER BY rc.constraint_name, fk_kcu.ordinal_position | ||||||||||||||||||||||
| 46 | }); | ||||||||||||||||||||||
| 47 | 1 | $sth->execute($schema); | |||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | 1 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 50 | 1 | ▶ | next unless exists $tables->{ $row->{from_table} }; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 51 | 1 | my $fk_key = "$row->{from_table}:$row->{constraint_name}"; | |||||||||||||||||||||
| 52 | 1 | ▶ | if (!exists $fks{$fk_key}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 53 | $fks{$fk_key} = { | ||||||||||||||||||||||
| 54 | constraint_name => $row->{constraint_name}, | ||||||||||||||||||||||
| 55 | from_table => $row->{from_table}, | ||||||||||||||||||||||
| 56 | from_columns => [], | ||||||||||||||||||||||
| 57 | to_table => $row->{to_table}, | ||||||||||||||||||||||
| 58 | to_schema => $row->{to_schema}, | ||||||||||||||||||||||
| 59 | to_columns => [], | ||||||||||||||||||||||
| 60 | on_delete => uc($row->{delete_rule} // 'NO ACTION'), | ||||||||||||||||||||||
| 61 | 1 | ▶ | on_update => uc($row->{update_rule} // 'NO ACTION'), | ||||||||||||||||||||
|
Condition50%2/4
$row->{"delete_rule"} // "NO ACTION"
$row->{"update_rule"} // "NO ACTION"
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 table50%1/2
A
A= $row->{"delete_rule"}
Truth table50%1/2
A
A= $row->{"update_rule"}
| |||||||||||||||||||||||
| 62 | is_deferrable => 1, | ||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||
| 65 | 1 | push @{ $fks{$fk_key}{from_columns} }, $row->{from_column}; | |||||||||||||||||||||
| 66 | 1 | push @{ $fks{$fk_key}{to_columns} }, $row->{to_column}; | |||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | # Re-index by table name. | ||||||||||||||||||||||
| 70 | 1 | my %by_table; | |||||||||||||||||||||
| 71 | 1 | for my $fk (values %fks) { | |||||||||||||||||||||
| 72 | 1 | push @{ $by_table{ $fk->{from_table} } }, $fk; | |||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||
| 74 | |||||||||||||||||||||||
| 75 | 1 | return \%by_table; | |||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | 28 | 1; | |||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||||