| 1 | package DBIO::SQLite::Introspect::ForeignKeys; | ||||||||||
| 2 | # ABSTRACT: Introspect SQLite foreign keys | ||||||||||
| 3 | |||||||||||
| 4 | 197 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 197 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 197 | ▶ | use DBIO::Introspect::Base (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | |||||||||||
| 10 | |||||||||||
| 11 | 26 | ▶ | sub fetch { | ||||||||
|
Subroutine
fetch: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 12 | 26 | my ($class, $dbh, $tables) = @_; | |||||||||
| 13 | 26 | my %fks; | |||||||||
| 14 | |||||||||||
| 15 | 26 | for my $table_name (sort keys %$tables) { | |||||||||
| 16 | 40 | my $list = $dbh->selectall_arrayref( | |||||||||
| 17 | qq{PRAGMA foreign_key_list("$table_name")}, { Slice => {} } | ||||||||||
| 18 | ); | ||||||||||
| 19 | 40 | ▶ | next unless @$list; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | |||||||||||
| 21 | 8 | my $groups = DBIO::Introspect::Base->_aggregate_by_ordered($list, 'id'); | |||||||||
| 22 | $fks{$table_name} = [ map { | ||||||||||
| 23 | 8 | my ($id, $rows) = @$_; | |||||||||
| 24 | 8 | my $first = $rows->[0]; | |||||||||
| 25 | 8 | my @from = map { $_->{from} } sort { $a->{seq} <=> $b->{seq} } @$rows; | |||||||||
| 26 | 8 | my @to = map { $_->{to} } sort { $a->{seq} <=> $b->{seq} } @$rows; | |||||||||
| 27 | +{ | ||||||||||
| 28 | fk_id => $id, | ||||||||||
| 29 | from_columns => \@from, | ||||||||||
| 30 | to_table => $first->{table}, | ||||||||||
| 31 | to_columns => \@to, | ||||||||||
| 32 | on_update => $first->{on_update}, | ||||||||||
| 33 | on_delete => $first->{on_delete}, | ||||||||||
| 34 | match => $first->{match}, | ||||||||||
| 35 | 8 | }; | |||||||||
| 36 | } @$groups ]; | ||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 26 | return \%fks; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 197 | 1; | |||||||||
| 43 | |||||||||||
| 44 | __END__ | ||||||||||
| 45 | |||||||||||
| 46 | =pod | ||||||||||
| 47 | |||||||||||
| 48 | =encoding UTF-8 | ||||||||||
| 49 | |||||||||||
| 50 | =head1 NAME | ||||||||||
| 51 | |||||||||||
| 52 | DBIO::SQLite::Introspect::ForeignKeys - Introspect SQLite foreign keys | ||||||||||
| 53 | |||||||||||
| 54 | =head1 VERSION | ||||||||||
| 55 | |||||||||||
| 56 | version 0.900001 | ||||||||||
| 57 | |||||||||||
| 58 | =head1 DESCRIPTION | ||||||||||
| 59 | |||||||||||
| 60 | Fetches foreign key metadata via C<PRAGMA foreign_key_list()>. Composite | ||||||||||
| 61 | FKs are grouped by their C<id> via | ||||||||||
| 62 | L<DBIO::Introspect::Base/_aggregate_by_ordered> (preserves both | ||||||||||
| 63 | first-seen id order and column-seq order within each FK). | ||||||||||
| 64 | |||||||||||
| 65 | =head1 METHODS | ||||||||||
| 66 | |||||||||||
| 67 | =head2 fetch | ||||||||||
| 68 | |||||||||||
| 69 | my $fks = DBIO::SQLite::Introspect::ForeignKeys->fetch($dbh, $tables); | ||||||||||
| 70 | |||||||||||
| 71 | Returns a hashref keyed by table name. Each value is an arrayref of | ||||||||||
| 72 | FK hashrefs with keys: C<fk_id>, C<from_columns> (arrayref), | ||||||||||
| 73 | C<to_table>, C<to_columns> (arrayref), C<on_update>, C<on_delete>, | ||||||||||
| 74 | C<match>. | ||||||||||
| 75 | |||||||||||
| 76 | =head1 AUTHOR | ||||||||||
| 77 | |||||||||||
| 78 | DBIO & DBIx::Class Authors | ||||||||||
| 79 | |||||||||||
| 80 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 81 | |||||||||||
| 82 | Copyright (C) 2026 DBIO Authors | ||||||||||
| 83 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||
| 84 | Based on DBIx::Class, heavily modified. | ||||||||||
| 85 | |||||||||||
| 86 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 87 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 88 | |||||||||||
| 89 | =cut | ||||||||||