| 1 | package DBIO::PostgreSQL::Introspect::Triggers; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect PostgreSQL triggers | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 171 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 171 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 171 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 171 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 171 | ▶ | use DBIO::PostgreSQL::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 34528 | ▶ | sub execute_call { | ||||||||||||||||||||||||||
|
Subroutine
execute_call: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 34528 | my ($class, $row) = @_; | |||||||||||||||||||||||||||
| 17 | 34528 | my $function = $row->{function_signature}; | |||||||||||||||||||||||||||
| 18 | 34528 | ▶ | return undef unless defined $function && $function =~ /\(\)\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $function and $function =~ /\(\)\z/
MC/DC50%1/2
defined $function and $function =~ /\(\)\z/
defined $function
$function =~ /\(\)\z/
Truth table66%2/3
A and B
A= defined $functionB= $function =~ /\(\)\z/
| |||||||||||||||||||||||||||||
| 19 | 34521 | substr($function, -2) = ''; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 34521 | my @args; | |||||||||||||||||||||||||||
| 22 | 34521 | ▶ | my $count = $row->{argument_count} // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$row->{"argument_count"} // 0
MC/DC0%0/1
$row->{"argument_count"} // 0
$row->{"argument_count"}
Truth table50%1/2
A
A= $row->{"argument_count"}
| |||||||||||||||||||||||||||||
| 23 | 34521 | ▶ | if ($count > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 34507 | ▶ | my $bytes = pack 'H*', $row->{arguments_hex} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$row->{"arguments_hex"} // ""
MC/DC0%0/1
$row->{"arguments_hex"} // ""
$row->{"arguments_hex"}
Truth table50%1/2
A
A= $row->{"arguments_hex"}
| |||||||||||||||||||||||||||||
| 25 | 34507 | @args = (split /\0/, $bytes, -1)[ 0 .. $count - 1 ]; | |||||||||||||||||||||||||||
| 26 | 34507 | for my $arg (@args) { | |||||||||||||||||||||||||||
| 27 | 34521 | ▶ | $arg //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$arg //= ""
MC/DC0%0/1
$arg //= ""
$arg
Truth table50%1/2
A
A= $arg
| |||||||||||||||||||||||||||||
| 28 | 34521 | utf8::decode($arg); | |||||||||||||||||||||||||||
| 29 | 34521 | $arg = DBIO::PostgreSQL::Util::quote_literal($arg); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 34521 | return $function . '(' . join(', ', @args) . ')'; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my ($class, $dbh, $filter) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 39 | SELECT | ||||||||||||||||||||||||||||
| 40 | n.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 41 | c.relname AS table_name, | ||||||||||||||||||||||||||||
| 42 | t.tgname AS trigger_name, | ||||||||||||||||||||||||||||
| 43 | pg_catalog.pg_get_triggerdef(t.oid) AS definition, | ||||||||||||||||||||||||||||
| 44 | t.tgfoid::pg_catalog.regprocedure::pg_catalog.text AS function_signature, | ||||||||||||||||||||||||||||
| 45 | t.tgnargs AS argument_count, | ||||||||||||||||||||||||||||
| 46 | pg_catalog.encode(t.tgargs, 'hex') AS arguments_hex, | ||||||||||||||||||||||||||||
| 47 | CASE | ||||||||||||||||||||||||||||
| 48 | WHEN t.tgtype & 2 = 2 THEN 'BEFORE' | ||||||||||||||||||||||||||||
| 49 | WHEN t.tgtype & 64 = 64 THEN 'INSTEAD OF' | ||||||||||||||||||||||||||||
| 50 | ELSE 'AFTER' | ||||||||||||||||||||||||||||
| 51 | END AS timing, | ||||||||||||||||||||||||||||
| 52 | CASE | ||||||||||||||||||||||||||||
| 53 | WHEN t.tgtype & 4 = 4 THEN 'INSERT' | ||||||||||||||||||||||||||||
| 54 | WHEN t.tgtype & 8 = 8 THEN 'DELETE' | ||||||||||||||||||||||||||||
| 55 | WHEN t.tgtype & 16 = 16 THEN 'UPDATE' | ||||||||||||||||||||||||||||
| 56 | WHEN t.tgtype & 32 = 32 THEN 'TRUNCATE' | ||||||||||||||||||||||||||||
| 57 | END AS event, | ||||||||||||||||||||||||||||
| 58 | CASE WHEN t.tgtype & 1 = 1 THEN 'ROW' ELSE 'STATEMENT' END AS orientation, | ||||||||||||||||||||||||||||
| 59 | t.tgenabled AS enabled | ||||||||||||||||||||||||||||
| 60 | FROM pg_catalog.pg_trigger t | ||||||||||||||||||||||||||||
| 61 | JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid | ||||||||||||||||||||||||||||
| 62 | JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | ||||||||||||||||||||||||||||
| 63 | WHERE NOT t.tgisinternal | ||||||||||||||||||||||||||||
| 64 | AND t.tgparentid = 0 | ||||||||||||||||||||||||||||
| 65 | }; | ||||||||||||||||||||||||||||
| 66 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n'); | |||||||||||||||||||||||||||
| 67 | 0 | $sql .= DBIO::PostgreSQL::Introspect->extension_member_filter('pg_class', 'c.oid'); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | my @bind; | |||||||||||||||||||||||||||
| 70 | 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
| |||||||||||||||||||||||||||||
| 71 | 0 | $sql .= ' AND n.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 72 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | $sql .= ' ORDER BY n.nspname, c.relname, t.tgname'; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 78 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $reads = DBIO::PostgreSQL::Introspect->column_reads( | |||||||||||||||||||||||||||
| 81 | $dbh, $filter, 'pg_trigger', 'tgrelid', 'tgname', | ||||||||||||||||||||||||||||
| 82 | 'NOT o.tgisinternal AND o.tgparentid = 0', | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | my %triggers; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | my $table_key = DBIO::PostgreSQL::Introspect->qualified_key($row); | |||||||||||||||||||||||||||
| 88 | $triggers{$table_key}{ $row->{trigger_name} } = { | ||||||||||||||||||||||||||||
| 89 | schema_name => $row->{schema_name}, | ||||||||||||||||||||||||||||
| 90 | table_name => $row->{table_name}, | ||||||||||||||||||||||||||||
| 91 | trigger_name => $row->{trigger_name}, | ||||||||||||||||||||||||||||
| 92 | definition => $row->{definition}, | ||||||||||||||||||||||||||||
| 93 | execute => $class->execute_call($row), | ||||||||||||||||||||||||||||
| 94 | timing => $row->{timing}, | ||||||||||||||||||||||||||||
| 95 | event => $row->{event}, | ||||||||||||||||||||||||||||
| 96 | orientation => $row->{orientation}, | ||||||||||||||||||||||||||||
| 97 | enabled => $row->{enabled}, | ||||||||||||||||||||||||||||
| 98 | depends_on_columns => $reads->{$table_key}{ $row->{trigger_name} } // {}, | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$reads->{$table_key}{$row->{"trigger_name"}} // {}
MC/DC0%0/1
$reads->{$table_key}{$row->{"trigger_name"}} // {}
$reads->{$table_key}{$row->{"trigger_name"}}
Truth table0%0/2
A
A= $reads->{$table_key}{$row->{"trigger_name"}}
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | return \%triggers; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 171 | 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||