« blib/lib/DBIO/PostgreSQL/Introspect/ForeignKeys.pm
↑ Summary
blib/lib/DBIO/PostgreSQL/Introspect/Columns.pm »
| 1 | package DBIO::PostgreSQL::Introspect::Tables; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect PostgreSQL tables | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 0 | my ($class, $dbh, $filter) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 17 | SELECT | ||||||||||||||||||||||||||||
| 18 | n.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 19 | c.relname AS table_name, | ||||||||||||||||||||||||||||
| 20 | c.oid AS table_oid, | ||||||||||||||||||||||||||||
| 21 | c.relkind AS kind, | ||||||||||||||||||||||||||||
| 22 | c.relpersistence AS persistence, | ||||||||||||||||||||||||||||
| 23 | CASE c.relkind | ||||||||||||||||||||||||||||
| 24 | WHEN 'r' THEN 'table' | ||||||||||||||||||||||||||||
| 25 | WHEN 'v' THEN 'view' | ||||||||||||||||||||||||||||
| 26 | WHEN 'm' THEN 'materialized_view' | ||||||||||||||||||||||||||||
| 27 | WHEN 'f' THEN 'foreign_table' | ||||||||||||||||||||||||||||
| 28 | WHEN 'p' THEN 'partitioned_table' | ||||||||||||||||||||||||||||
| 29 | END AS kind_label, | ||||||||||||||||||||||||||||
| 30 | pg_catalog.obj_description(c.oid, 'pg_class') AS comment, | ||||||||||||||||||||||||||||
| 31 | c.relrowsecurity AS rls_enabled, | ||||||||||||||||||||||||||||
| 32 | c.relforcerowsecurity AS rls_forced, | ||||||||||||||||||||||||||||
| 33 | CASE | ||||||||||||||||||||||||||||
| 34 | WHEN c.relkind IN ('v', 'm') THEN pg_catalog.pg_get_viewdef(c.oid) | ||||||||||||||||||||||||||||
| 35 | END AS view_definition, | ||||||||||||||||||||||||||||
| 36 | pn.nspname AS parent_schema, | ||||||||||||||||||||||||||||
| 37 | pc.relname AS parent_name | ||||||||||||||||||||||||||||
| 38 | FROM pg_catalog.pg_class c | ||||||||||||||||||||||||||||
| 39 | JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | ||||||||||||||||||||||||||||
| 40 | LEFT JOIN pg_catalog.pg_inherits i ON c.relispartition AND i.inhrelid = c.oid | ||||||||||||||||||||||||||||
| 41 | LEFT JOIN pg_catalog.pg_class pc ON pc.oid = i.inhparent | ||||||||||||||||||||||||||||
| 42 | LEFT JOIN pg_catalog.pg_namespace pn ON pn.oid = pc.relnamespace | ||||||||||||||||||||||||||||
| 43 | WHERE c.relkind IN ('r', 'v', 'm', 'f', 'p') | ||||||||||||||||||||||||||||
| 44 | }; | ||||||||||||||||||||||||||||
| 45 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n'); | |||||||||||||||||||||||||||
| 46 | 0 | $sql .= DBIO::PostgreSQL::Introspect->extension_member_filter('pg_class', 'c.oid'); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my @bind; | |||||||||||||||||||||||||||
| 49 | 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
| |||||||||||||||||||||||||||||
| 50 | 0 | $sql .= ' AND n.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 51 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | $sql .= ' ORDER BY n.nspname, c.relname'; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # pg_get_viewdef writes a relation, a function or a type without its schema | ||||||||||||||||||||||||||||
| 57 | # whenever the search_path of the asking session finds it, so this is read | ||||||||||||||||||||||||||||
| 58 | # under an empty one -- inside a model read with qualified_names the whole | ||||||||||||||||||||||||||||
| 59 | # read is already, and the call runs where it stands. | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $rows = DBIO::PostgreSQL::Introspect->under_empty_search_path($dbh, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 61 | 0 | $dbh->selectall_arrayref($sql, { Slice => {} }, @bind); | |||||||||||||||||||||||||||
| 62 | 0 | }); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my %tables; | |||||||||||||||||||||||||||
| 65 | 0 | for my $row (@$rows) { | |||||||||||||||||||||||||||
| 66 | 0 | my $key = DBIO::PostgreSQL::Introspect->qualified_key($row); | |||||||||||||||||||||||||||
| 67 | $tables{$key} = { | ||||||||||||||||||||||||||||
| 68 | schema_name => $row->{schema_name}, | ||||||||||||||||||||||||||||
| 69 | table_name => $row->{table_name}, | ||||||||||||||||||||||||||||
| 70 | oid => $row->{table_oid}, | ||||||||||||||||||||||||||||
| 71 | kind => $row->{kind}, | ||||||||||||||||||||||||||||
| 72 | kind_label => $row->{kind_label}, | ||||||||||||||||||||||||||||
| 73 | persistence => $row->{persistence}, | ||||||||||||||||||||||||||||
| 74 | comment => $row->{comment}, | ||||||||||||||||||||||||||||
| 75 | rls_enabled => $row->{rls_enabled} ? 1 : 0, | ||||||||||||||||||||||||||||
| 76 | rls_forced => $row->{rls_forced} ? 1 : 0, | ||||||||||||||||||||||||||||
| 77 | view_definition => $row->{view_definition}, | ||||||||||||||||||||||||||||
| 78 | depends_on => [], | ||||||||||||||||||||||||||||
| 79 | depends_on_columns => {}, | ||||||||||||||||||||||||||||
| 80 | inherits => [], | ||||||||||||||||||||||||||||
| 81 | partition_of => defined $row->{parent_name} | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | ? DBIO::PostgreSQL::Introspect->qualified_key(@{$row}{qw( parent_schema parent_name )}) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 83 | : undef, | ||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my $depends_on = $class->_view_dependencies($dbh, $filter); | |||||||||||||||||||||||||||
| 88 | 0 | for my $key (grep { $tables{$_} } keys %$depends_on) { | |||||||||||||||||||||||||||
| 89 | 0 | my $reads = $depends_on->{$key}; | |||||||||||||||||||||||||||
| 90 | 0 | $tables{$key}{depends_on} = [ sort keys %$reads ]; | |||||||||||||||||||||||||||
| 91 | $tables{$key}{depends_on_columns} = { | ||||||||||||||||||||||||||||
| 92 | 0 | map { $_ => [ sort @{ $reads->{$_} } ] } grep { @{ $reads->{$_} } } keys %$reads | |||||||||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | my $inherits = $class->_inheritance_parents($dbh, $filter); | |||||||||||||||||||||||||||
| 97 | 0 | for my $key (grep { $tables{$_} } keys %$inherits) { | |||||||||||||||||||||||||||
| 98 | 0 | $tables{$key}{inherits} = $inherits->{$key}; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | return \%tables; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # What each view and materialized view reads, as { view key => { relation | ||||||||||||||||||||||||||||
| 105 | # key => [ column names ] } }. A view is its rewrite rule: the rule's | ||||||||||||||||||||||||||||
| 106 | # pg_depend rows name every relation the definition mentions -- one row per | ||||||||||||||||||||||||||||
| 107 | # column used (refobjsubid is its attnum), one row without a column where the | ||||||||||||||||||||||||||||
| 108 | # relation is read as a whole row, and the view itself among them, which is | ||||||||||||||||||||||||||||
| 109 | # left out here. A relation read only as a whole row has no column names. | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub _view_dependencies { | ||||||||||||||||||||||||||
|
Subroutine
_view_dependencies: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 111 | 0 | my ($class, $dbh, $filter) = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 114 | SELECT DISTINCT | ||||||||||||||||||||||||||||
| 115 | n.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 116 | c.relname AS table_name, | ||||||||||||||||||||||||||||
| 117 | rn.nspname AS ref_schema, | ||||||||||||||||||||||||||||
| 118 | r.relname AS ref_name, | ||||||||||||||||||||||||||||
| 119 | a.attname AS ref_column | ||||||||||||||||||||||||||||
| 120 | FROM pg_catalog.pg_rewrite rw | ||||||||||||||||||||||||||||
| 121 | JOIN pg_catalog.pg_class c ON c.oid = rw.ev_class | ||||||||||||||||||||||||||||
| 122 | JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | ||||||||||||||||||||||||||||
| 123 | JOIN pg_catalog.pg_depend d | ||||||||||||||||||||||||||||
| 124 | ON d.classid = 'pg_catalog.pg_rewrite'::pg_catalog.regclass | ||||||||||||||||||||||||||||
| 125 | AND d.objid = rw.oid | ||||||||||||||||||||||||||||
| 126 | AND d.refclassid = 'pg_catalog.pg_class'::pg_catalog.regclass | ||||||||||||||||||||||||||||
| 127 | JOIN pg_catalog.pg_class r ON r.oid = d.refobjid AND r.oid <> c.oid | ||||||||||||||||||||||||||||
| 128 | JOIN pg_catalog.pg_namespace rn ON rn.oid = r.relnamespace | ||||||||||||||||||||||||||||
| 129 | LEFT JOIN pg_catalog.pg_attribute a | ||||||||||||||||||||||||||||
| 130 | ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid AND d.refobjsubid > 0 | ||||||||||||||||||||||||||||
| 131 | WHERE c.relkind IN ('v', 'm') | ||||||||||||||||||||||||||||
| 132 | }; | ||||||||||||||||||||||||||||
| 133 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n'); | |||||||||||||||||||||||||||
| 134 | 0 | $sql .= DBIO::PostgreSQL::Introspect->extension_member_filter('pg_class', 'c.oid'); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | my @bind; | |||||||||||||||||||||||||||
| 137 | 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
| |||||||||||||||||||||||||||||
| 138 | 0 | $sql .= ' AND n.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 139 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 143 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | my %depends_on; | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | my $view = DBIO::PostgreSQL::Introspect->qualified_key($row); | |||||||||||||||||||||||||||
| 148 | 0 | my $read = DBIO::PostgreSQL::Introspect->qualified_key(@{$row}{qw( ref_schema ref_name )}); | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | my $columns = $depends_on{$view}{$read} //= []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$depends_on{$view}{$read} //= []
MC/DC0%0/1
$depends_on{$view}{$read} //= []
$depends_on{$view}{$read}
Truth table0%0/2
A
A= $depends_on{$view}{$read}
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | push @$columns, $row->{ref_column} if defined $row->{ref_column}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | return \%depends_on; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # The tables each table inherits from, as { table key => [ parent keys ] } in | ||||||||||||||||||||||||||||
| 157 | # the order of pg_inherits.inhseqno. Partitions are left out: what a partition | ||||||||||||||||||||||||||||
| 158 | # belongs to is partition_of. So are the rows pg_inherits has for the indexes | ||||||||||||||||||||||||||||
| 159 | # of a partitioned table. | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub _inheritance_parents { | ||||||||||||||||||||||||||
|
Subroutine
_inheritance_parents: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 161 | 0 | my ($class, $dbh, $filter) = @_; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 164 | SELECT | ||||||||||||||||||||||||||||
| 165 | n.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 166 | c.relname AS table_name, | ||||||||||||||||||||||||||||
| 167 | pn.nspname AS parent_schema, | ||||||||||||||||||||||||||||
| 168 | pc.relname AS parent_name | ||||||||||||||||||||||||||||
| 169 | FROM pg_catalog.pg_inherits i | ||||||||||||||||||||||||||||
| 170 | JOIN pg_catalog.pg_class c ON c.oid = i.inhrelid | ||||||||||||||||||||||||||||
| 171 | JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | ||||||||||||||||||||||||||||
| 172 | JOIN pg_catalog.pg_class pc ON pc.oid = i.inhparent | ||||||||||||||||||||||||||||
| 173 | JOIN pg_catalog.pg_namespace pn ON pn.oid = pc.relnamespace | ||||||||||||||||||||||||||||
| 174 | WHERE c.relkind IN ('r', 'f') | ||||||||||||||||||||||||||||
| 175 | AND NOT c.relispartition | ||||||||||||||||||||||||||||
| 176 | }; | ||||||||||||||||||||||||||||
| 177 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n'); | |||||||||||||||||||||||||||
| 178 | 0 | $sql .= DBIO::PostgreSQL::Introspect->extension_member_filter('pg_class', 'c.oid'); | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | my @bind; | |||||||||||||||||||||||||||
| 181 | 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
| |||||||||||||||||||||||||||||
| 182 | 0 | $sql .= ' AND n.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 183 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | $sql .= ' ORDER BY n.nspname, c.relname, i.inhseqno'; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 189 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | my %inherits; | |||||||||||||||||||||||||||
| 192 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | push @{ $inherits{ DBIO::PostgreSQL::Introspect->qualified_key($row) } }, | |||||||||||||||||||||||||||
| 194 | 0 | DBIO::PostgreSQL::Introspect->qualified_key(@{$row}{qw( parent_schema parent_name )}); | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | return \%inherits; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 171 | 1; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | __END__ | ||||||||||||||||||||||||||||