| 1 | package DBIO::Firebird::Introspect::Uniques; | ||||||||||
| 2 | # ABSTRACT: Introspect Firebird UNIQUE constraints via rdb$relation_constraints | ||||||||||
| 3 | |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | 0 | ▶ | sub fetch { | ||||||||
|
Subroutine
fetch: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 10 | 0 | my ($class, $dbh, $tables) = @_; | |||||||||
| 11 | |||||||||||
| 12 | 0 | my $sth = $dbh->prepare(q{ | |||||||||
| 13 | SELECT rc.rdb$constraint_name, rc.rdb$relation_name, | ||||||||||
| 14 | iseg.rdb$field_name, iseg.rdb$field_position | ||||||||||
| 15 | FROM rdb$relation_constraints rc | ||||||||||
| 16 | JOIN rdb$index_segments iseg ON rc.rdb$index_name = iseg.rdb$index_name | ||||||||||
| 17 | WHERE rc.rdb$constraint_type = 'UNIQUE' | ||||||||||
| 18 | ORDER BY rc.rdb$relation_name, rc.rdb$constraint_name, iseg.rdb$field_position | ||||||||||
| 19 | }); | ||||||||||
| 20 | 0 | $sth->execute; | |||||||||
| 21 | |||||||||||
| 22 | 0 | my %by_table; | |||||||||
| 23 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | my $table = _trim($row->{'rdb$relation_name'}); | |||||||||
| 25 | 0 | ▶ | next unless exists $tables->{$table}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | 0 | my $cname = _trim($row->{'rdb$constraint_name'}); | |||||||||
| 28 | 0 | my $col = _trim($row->{'rdb$field_name'}); | |||||||||
| 29 | |||||||||||
| 30 | 0 | push @{ $by_table{$table}{$cname} }, $col; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 0 | my %uniques; | |||||||||
| 34 | 0 | for my $table (keys %by_table) { | |||||||||
| 35 | $uniques{$table} = [ | ||||||||||
| 36 | 0 | map { [ $_ => $by_table{$table}{$_} ] } | |||||||||
| 37 | 0 | sort keys %{ $by_table{$table} } | |||||||||
| 38 | ]; | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 0 | return \%uniques; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | # Trim trailing whitespace from a CHAR-padded rdb$ value. | ||||||||||
| 45 | 0 | ▶ | sub _trim { | ||||||||
|
Subroutine
_trim: not called CC=2 SCAR=17.9
| |||||||||||
| 46 | 0 | my ($value) = @_; | |||||||||
| 47 | 0 | ▶ | return $value unless defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | $value =~ s/\s+$//; | |||||||||
| 49 | 0 | return $value; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 3 | 1; | |||||||||
| 53 | |||||||||||
| 54 | __END__ | ||||||||||
| 55 | |||||||||||
| 56 | =pod | ||||||||||
| 57 | |||||||||||
| 58 | =encoding UTF-8 | ||||||||||
| 59 | |||||||||||
| 60 | =head1 NAME | ||||||||||
| 61 | |||||||||||
| 62 | DBIO::Firebird::Introspect::Uniques - Introspect Firebird UNIQUE constraints via rdb$relation_constraints | ||||||||||
| 63 | |||||||||||
| 64 | =head1 VERSION | ||||||||||
| 65 | |||||||||||
| 66 | version 0.900001 | ||||||||||
| 67 | |||||||||||
| 68 | =head1 DESCRIPTION | ||||||||||
| 69 | |||||||||||
| 70 | Fetches C<UNIQUE> I<constraint> metadata via C<rdb$relation_constraints> | ||||||||||
| 71 | joined against C<rdb$index_segments>. This is distinct from | ||||||||||
| 72 | L<DBIO::Firebird::Introspect::Indexes>, which deliberately excludes | ||||||||||
| 73 | constraint-backed indexes: a Firebird C<UNIQUE> constraint and a standalone | ||||||||||
| 74 | C<CREATE UNIQUE INDEX> are different objects, and only the former belongs in | ||||||||||
| 75 | the normalized C<table_uniq_info> generation contract. | ||||||||||
| 76 | |||||||||||
| 77 | =head1 METHODS | ||||||||||
| 78 | |||||||||||
| 79 | =head2 fetch | ||||||||||
| 80 | |||||||||||
| 81 | my $uniques = DBIO::Firebird::Introspect::Uniques->fetch($dbh, $tables); | ||||||||||
| 82 | |||||||||||
| 83 | Given the tables hashref from L<DBIO::Firebird::Introspect::Tables>, returns a | ||||||||||
| 84 | hashref keyed by table name. Each value is an arrayref of | ||||||||||
| 85 | C<[ $constraint_name, \@column_names ]> pairs (columns in | ||||||||||
| 86 | C<rdb$field_position> order), sorted by constraint name. | ||||||||||
| 87 | |||||||||||
| 88 | =head1 AUTHOR | ||||||||||
| 89 | |||||||||||
| 90 | DBIO & DBIx::Class Authors | ||||||||||
| 91 | |||||||||||
| 92 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 93 | |||||||||||
| 94 | Copyright (C) 2026 DBIO Authors | ||||||||||
| 95 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||
| 96 | Based on DBIx::Class, heavily modified. | ||||||||||
| 97 | |||||||||||
| 98 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 99 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 100 | |||||||||||
| 101 | =cut | ||||||||||