| 1 | package DBIO::PostgreSQL::Introspect::CheckConstraints; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect PostgreSQL CHECK constraints | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 0 | my ($class, $dbh, $filter) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 15 | SELECT | ||||||||||||||||||||||||||||
| 16 | sn.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 17 | cl.relname AS table_name, | ||||||||||||||||||||||||||||
| 18 | con.conname AS constraint_name, | ||||||||||||||||||||||||||||
| 19 | pg_catalog.pg_get_constraintdef(con.oid) AS definition, | ||||||||||||||||||||||||||||
| 20 | a.attname AS column_name, | ||||||||||||||||||||||||||||
| 21 | k.n AS key_seq | ||||||||||||||||||||||||||||
| 22 | FROM pg_catalog.pg_constraint con | ||||||||||||||||||||||||||||
| 23 | JOIN pg_catalog.pg_class cl ON cl.oid = con.conrelid | ||||||||||||||||||||||||||||
| 24 | JOIN pg_catalog.pg_namespace sn ON sn.oid = cl.relnamespace | ||||||||||||||||||||||||||||
| 25 | LEFT JOIN LATERAL unnest(con.conkey) WITH ORDINALITY AS k(attnum, n) ON true | ||||||||||||||||||||||||||||
| 26 | LEFT JOIN pg_catalog.pg_attribute a | ||||||||||||||||||||||||||||
| 27 | ON a.attrelid = con.conrelid AND a.attnum = k.attnum | ||||||||||||||||||||||||||||
| 28 | WHERE con.contype = 'c' | ||||||||||||||||||||||||||||
| 29 | AND NOT con.conislocal = false | ||||||||||||||||||||||||||||
| 30 | }; | ||||||||||||||||||||||||||||
| 31 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('sn'); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | my @bind; | |||||||||||||||||||||||||||
| 34 | 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
| |||||||||||||||||||||||||||||
| 35 | 0 | $sql .= ' AND sn.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 36 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | $sql .= q{ | |||||||||||||||||||||||||||
| 40 | ORDER BY sn.nspname, cl.relname, con.conname, k.n | ||||||||||||||||||||||||||||
| 41 | }; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 44 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | my %checks; | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | my $table_key = DBIO::PostgreSQL::Introspect->qualified_key($row); | |||||||||||||||||||||||||||
| 49 | my $entry = $checks{$table_key}{ $row->{constraint_name} } //= { | ||||||||||||||||||||||||||||
| 50 | constraint_name => $row->{constraint_name}, | ||||||||||||||||||||||||||||
| 51 | definition => $row->{definition}, | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | columns => [], | ||||||||||||||||||||||||||
|
Condition0%0/2
$checks{$table_key}{$row->{"constraint_name"}} //= {"constraint_name", $row->{"constraint_name"}, "definition", $row->{"definition"}, "columns", []}
MC/DC0%0/1
$checks{$table_key}{$row->{"constraint_name"}} //= {"constraint_name", $row->{"constraint_name"}, "definition", $row->{"definition"}, "columns", []}
$checks{$table_key}{$row->{"constraint_name"}}
Truth table0%0/2
A
A= $checks{$table_key}{$row->{"constraint_name"}}
| |||||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | push @{ $entry->{columns} }, $row->{column_name} if defined $row->{column_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | return \%checks; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 15 | 1; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | __END__ | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =pod | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 NAME | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | DBIO::PostgreSQL::Introspect::CheckConstraints - Introspect PostgreSQL CHECK constraints | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 VERSION | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | version 0.900001 | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Fetches CHECK constraint metadata from C<pg_catalog.pg_constraint> where | ||||||||||||||||||||||||||||
| 79 | C<contype = 'c'>. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 METHODS | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head2 fetch | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | my $checks = DBIO::PostgreSQL::Introspect::CheckConstraints->fetch($dbh, $filter); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Returns a hashref keyed by C<schema.table>. Each value is a hashref keyed by | ||||||||||||||||||||||||||||
| 88 | constraint name. Each entry has: C<constraint_name>, C<definition> (the CHECK | ||||||||||||||||||||||||||||
| 89 | expression from C<pg_get_constraintdef>), C<columns> (ArrayRef of column names | ||||||||||||||||||||||||||||
| 90 | the constraint references, may be empty for table-level checks). | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 99 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 100 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 103 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||