| 1 | package DBIO::PostgreSQL::Introspect::Triggers; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect PostgreSQL triggers | ||||||||||||||||||||||||||||
| 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=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 0 | my ($class, $dbh, $filter) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 15 | SELECT | ||||||||||||||||||||||||||||
| 16 | n.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 17 | c.relname AS table_name, | ||||||||||||||||||||||||||||
| 18 | t.tgname AS trigger_name, | ||||||||||||||||||||||||||||
| 19 | pg_catalog.pg_get_triggerdef(t.oid) AS definition, | ||||||||||||||||||||||||||||
| 20 | CASE | ||||||||||||||||||||||||||||
| 21 | WHEN t.tgtype & 2 = 2 THEN 'BEFORE' | ||||||||||||||||||||||||||||
| 22 | WHEN t.tgtype & 64 = 64 THEN 'INSTEAD OF' | ||||||||||||||||||||||||||||
| 23 | ELSE 'AFTER' | ||||||||||||||||||||||||||||
| 24 | END AS timing, | ||||||||||||||||||||||||||||
| 25 | CASE | ||||||||||||||||||||||||||||
| 26 | WHEN t.tgtype & 4 = 4 THEN 'INSERT' | ||||||||||||||||||||||||||||
| 27 | WHEN t.tgtype & 8 = 8 THEN 'DELETE' | ||||||||||||||||||||||||||||
| 28 | WHEN t.tgtype & 16 = 16 THEN 'UPDATE' | ||||||||||||||||||||||||||||
| 29 | WHEN t.tgtype & 32 = 32 THEN 'TRUNCATE' | ||||||||||||||||||||||||||||
| 30 | END AS event, | ||||||||||||||||||||||||||||
| 31 | CASE WHEN t.tgtype & 1 = 1 THEN 'ROW' ELSE 'STATEMENT' END AS orientation, | ||||||||||||||||||||||||||||
| 32 | t.tgenabled AS enabled | ||||||||||||||||||||||||||||
| 33 | FROM pg_catalog.pg_trigger t | ||||||||||||||||||||||||||||
| 34 | JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid | ||||||||||||||||||||||||||||
| 35 | JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | ||||||||||||||||||||||||||||
| 36 | WHERE NOT t.tgisinternal | ||||||||||||||||||||||||||||
| 37 | }; | ||||||||||||||||||||||||||||
| 38 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n'); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | my @bind; | |||||||||||||||||||||||||||
| 41 | 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
| |||||||||||||||||||||||||||||
| 42 | 0 | $sql .= ' AND n.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 43 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | $sql .= ' ORDER BY n.nspname, c.relname, t.tgname'; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 49 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my %triggers; | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | my $table_key = DBIO::PostgreSQL::Introspect->qualified_key($row); | |||||||||||||||||||||||||||
| 54 | $triggers{$table_key}{ $row->{trigger_name} } = { | ||||||||||||||||||||||||||||
| 55 | trigger_name => $row->{trigger_name}, | ||||||||||||||||||||||||||||
| 56 | definition => $row->{definition}, | ||||||||||||||||||||||||||||
| 57 | timing => $row->{timing}, | ||||||||||||||||||||||||||||
| 58 | event => $row->{event}, | ||||||||||||||||||||||||||||
| 59 | orientation => $row->{orientation}, | ||||||||||||||||||||||||||||
| 60 | enabled => $row->{enabled}, | ||||||||||||||||||||||||||||
| 61 | 0 | }; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | return \%triggers; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 15 | 1; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | __END__ | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =pod | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head1 NAME | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | DBIO::PostgreSQL::Introspect::Triggers - Introspect PostgreSQL triggers | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head1 VERSION | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | version 0.900001 | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Fetches user-defined trigger metadata from C<pg_catalog.pg_trigger>. Internal | ||||||||||||||||||||||||||||
| 86 | (constraint) triggers are excluded. Timing (C<BEFORE>/C<AFTER>/C<INSTEAD OF>), | ||||||||||||||||||||||||||||
| 87 | event (C<INSERT>/C<UPDATE>/C<DELETE>/C<TRUNCATE>), and orientation | ||||||||||||||||||||||||||||
| 88 | (C<ROW>/C<STATEMENT>) are decoded from the C<tgtype> bitmask. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head1 METHODS | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 fetch | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | my $triggers = DBIO::PostgreSQL::Introspect::Triggers->fetch($dbh, $filter); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Returns a hashref keyed by C<schema.table>. Each value is a hashref keyed by | ||||||||||||||||||||||||||||
| 97 | trigger name. Each trigger entry has: C<trigger_name>, C<definition> (from | ||||||||||||||||||||||||||||
| 98 | C<pg_get_triggerdef>), C<timing>, C<event>, C<orientation>, C<enabled>. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 107 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 108 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 111 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||