PostgreSQL/Introspect.pm

filter: Statementstmt 90.9% 210 / 231 Branchbran 72.7% 64 / 88 Conditioncond 52.8% 64 / 121 MC/DCmcdc 22.9% 14 / 61 Subroutinesub 92.1% 35 / 38 Podpod 88.2% 15 / 17 totaltotal 72.3% 402 / 556 CC 106 SCAR 32.3
CC
106
Cov
77%
result_class_extra_statements
60.1
qualified_key
56.0
_build_model
12.0
CRAP 25.2
« PostgreSQL/Introspect/Tables.pm ↑ Summary PostgreSQL/Introspect/Types.pm »
1 package DBIO::PostgreSQL::Introspect;
2 # ABSTRACT: Introspect a PostgreSQL database via pg_catalog
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 base 'DBIO::Introspect::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 15 ▶use DBIO::PostgreSQL::Introspect::Schemas;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use DBIO::PostgreSQL::Introspect::Tables;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use DBIO::PostgreSQL::Introspect::Columns;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use DBIO::PostgreSQL::Introspect::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use DBIO::PostgreSQL::Introspect::Indexes;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use DBIO::PostgreSQL::Introspect::Triggers;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use DBIO::PostgreSQL::Introspect::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use DBIO::PostgreSQL::Introspect::Extensions;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use DBIO::PostgreSQL::Introspect::Policies;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 15 ▶use DBIO::PostgreSQL::Introspect::Sequences;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 15 ▶use DBIO::PostgreSQL::Introspect::ForeignKeys;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 15 ▶use DBIO::PostgreSQL::Introspect::CheckConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 15 ▶use DBIO::PostgreSQL::Introspect::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 5 ▶sub schema_filter { $_[0]->{schema_filter} }
Subroutine schema_filter: called CC=1 SCAR=0.0
Pod documented
25 55 ▶sub preserve_case { $_[0]->{preserve_case} // 0 }
Subroutine preserve_case: called CC=2 SCAR=7.6
Pod undocumented
Condition50%1/2
$_[0]{"preserve_case"} // 0
l!l
550
MC/DC0%0/1
$_[0]{"preserve_case"} // 0
$_[0]{"preserve_case"}
Truth table50%1/2
A
A= $_[0]{"preserve_case"}
Aresult
11
00
26  
27  
28 0 ▶sub _build_model {
Subroutine _build_model: not called CC=3 SCAR=24.8
29 0 my ($self) = @_;
30 0 my $dbh = $self->dbh;
31 0 my $filter = $self->schema_filter;
32  
33 # model key => Introspect::Section class name.
34 # Sections with filtered => 0 are fetched without the schema filter
35 # (extensions are cluster-wide, not bound to a single schema).
36 0 my %SECTIONS = (
37 schemas => ['Schemas', 1],
38 extensions => ['Extensions', 0],
39 types => ['Types', 1],
40 tables => ['Tables', 1],
41 columns => ['Columns', 1],
42 indexes => ['Indexes', 1],
43 triggers => ['Triggers', 1],
44 functions => ['Functions', 1],
45 policies => ['Policies', 1],
46 sequences => ['Sequences', 1],
47 foreign_keys => ['ForeignKeys', 1],
48 check_constraints => ['CheckConstraints', 1],
49 );
50  
51 0 my %model;
52 0 for my $key (sort keys %SECTIONS) {
53 0 my ($class, $filtered) = @{ $SECTIONS{$key} };
54 0 my $pkg = "DBIO::PostgreSQL::Introspect::$class";
55 0 ▶ $model{$key} = $pkg->fetch($dbh, $filtered ? $filter : ());
Branch: 1 branch
BranchTrueFalseTotal
$filtered ? : 0 0 0
56 }
57 0 return \%model;
58 }
59  
60  
61 2 ▶sub table_keys {
Subroutine table_keys: called CC=2 SCAR=7.0
Pod documented
62 2 my ($self) = @_;
63 2 my $model = $self->model;
64 return [
65 5 sort grep { $self->_include_table($_) }
66 2 ▶ keys %{ $model->{tables} || {} }
Condition50%1/2
$model->{'tables'} || {}
l!l
20
MC/DC0%0/1
$model->{'tables'} || {}
$model->{'tables'}
Truth table50%1/2
A
A= $model->{'tables'}
Aresult
11
00
67 ];
68 }
69  
70 5 ▶sub _include_table {
Subroutine _include_table: called CC=4 SCAR=14.6
71 5 my ($self, $table_key) = @_;
72 5 my ($schema) = split /\./, $table_key, 2;
73 5 my $filter = $self->schema_filter;
74 5 ▶ return 1 unless $filter && @$filter;
Branch: 1 branch
BranchTrueFalseTotal
unless $filter and @$filter 0 5 5
Condition33%1/3
$filter and @$filter
!ll&&!rl&&r
005
MC/DC0%0/2
$filter and @$filter
$filter @$filter
Truth table33%1/3
A and B
A= $filterB= @$filter
ABresult
0X0
100
111
75 5 ▶ return 1 if grep { $_ eq '%' } @$filter;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "%";} @$filter 0 5 5
76 5 return scalar grep { $_ eq $schema } @$filter;
77 }
78  
79 55 ▶sub _normalize_name {
Subroutine _normalize_name: called CC=1 SCAR=0.0
80 55 return DBIO::PostgreSQL::Introspect::Normalize->name(@_[1..$#_], $_[0]->preserve_case);
81 }
82  
83  
84 13 ▶sub identity_kind {
Subroutine identity_kind: called CC=5 SCAR=16.1
Pod documented
85 13 my ($class, $id) = @_;
86 13 ▶ return 'ALWAYS' if defined $id && $id eq 'a';
Branch: 1 branch
BranchTrueFalseTotal
if defined $id and $id eq "a" 4 9 13
Condition100%3/3
defined $id and $id eq "a"
!ll&&!rl&&r
364
MC/DC100%2/2
defined $id and $id eq "a"
defined $id $id eq "a"
Truth table100%3/3
A and B
A= defined $idB= $id eq "a"
ABresult
0X0
100
111
87 9 ▶ return 'BY DEFAULT' if defined $id && $id eq 'd';
Branch: 1 branch
BranchTrueFalseTotal
if defined $id and $id eq "d" 4 5 9
Condition100%3/3
defined $id and $id eq "d"
!ll&&!rl&&r
324
MC/DC100%2/2
defined $id and $id eq "d"
defined $id $id eq "d"
Truth table100%3/3
A and B
A= defined $idB= $id eq "d"
ABresult
0X0
100
111
88 5 return undef;
89 }
90  
91  
92 0 ▶sub qualified_key {
Subroutine qualified_key: not called CC=7 SCAR=40.3
Pod documented
93 0 my ($class, $a, $b) = @_;
94  
95 # Two-arg form: ($schema, $name).
96 0 ▶ if (ref $a eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq "") 0 0 0
97 0 return "$a.$b";
98 }
99  
100 # Hashref form: pull schema + the first matching name field.
101 0 my $schema = $a->{schema_name};
102 my $name = $a->{table_name}
103 // $a->{type_name}
104 // $a->{function_name}
105 // $a->{sequence_name}
106 // $a->{index_name}
107 0 ▶ // $a->{policy_name};
Condition0%0/15
(((($a->{"table_name"} // $a->{"type_name"}) // $a->{"function_name"}) // $a->{"sequence_name"}) // $a->{"index_name"}) // $a->{"policy_name"}
l!l&&r!l&&!r
000
((($a->{"table_name"} // $a->{"type_name"}) // $a->{"function_name"}) // $a->{"sequence_name"}) // $a->{"index_name"}
l!l&&r!l&&!r
000
(($a->{"table_name"} // $a->{"type_name"}) // $a->{"function_name"}) // $a->{"sequence_name"}
l!l&&r!l&&!r
000
($a->{"table_name"} // $a->{"type_name"}) // $a->{"function_name"}
l!l&&r!l&&!r
000
$a->{"table_name"} // $a->{"type_name"}
l!l&&r!l&&!r
000
MC/DC0%0/6
(((($a->{"table_name"} // $a->{"type_name"}) // $a->{"function_name"}) // $a->{"sequence_name"}) // $a->{"index_name"}) // $a->{"policy_name"}
$a->{"table_name"} $a->{"type_name"} $a->{"function_name"} $a->{"sequence_name"} $a->{"index_name"} $a->{"policy_name"}
Truth table0%0/7
A // B // C // D // E // F
A= $a->{"table_name"}B= $a->{"type_name"}C= $a->{"function_name"}D= $a->{"sequence_name"}E= $a->{"index_name"}F= $a->{"policy_name"}
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
108 0 return "$schema.$name";
109 }
110  
111  
112 0 ▶sub system_schema_filter {
Subroutine system_schema_filter: not called CC=2 SCAR=17.9
Pod documented
113 0 my ($class, $alias) = @_;
114 0 ▶ $alias //= 'n';
Condition0%0/2
$alias //= "n"
l!l
00
MC/DC0%0/1
$alias //= "n"
$alias
Truth table0%0/2
A
A= $alias
Aresult
11
00
115 0 return "AND $alias.nspname !~ '^pg_'\n AND $alias.nspname != 'information_schema'";
116 }
117  
118 14 ▶sub _normalize_data_type {
Subroutine _normalize_data_type: called CC=8 SCAR=22.4
119 14 my ($self, $info, $column) = @_;
120  
121 # Enum columns need the value list from the model, which is the only
122 # stateful piece of the normalization. Resolve it here so the leaf
123 # helper stays pure.
124 14 my $enum_values;
125 14 ▶ if ($column->{type_category} && $column->{type_category} eq 'e' && $column->{enum_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->{"type_category"} and $column->{"type_category"} eq "e" and $column->{"enum_type"}) 1 13 14
Condition66%4/6
$column->{"type_category"} and $column->{"type_category"} eq "e" and $column->{"enum_type"}
!ll&&!rl&&r
1301
$column->{"type_category"} and $column->{"type_category"} eq "e"
!ll&&!rl&&r
0131
MC/DC33%1/3
$column->{"type_category"} and $column->{"type_category"} eq "e" and $column->{"enum_type"}
$column->{"type_category"} $column->{"type_category"} eq "e" $column->{"enum_type"}
Truth table50%2/4
A and B and C
A= $column->{"type_category"}B= $column->{"type_category"} eq "e"C= $column->{"enum_type"}
ABCresult
0XX0
10X0
1100
1111
126 1 ▶ my $ts = $column->{type_schema} || '';
Condition50%1/2
$column->{'type_schema'} || ''
l!l
10
MC/DC0%0/1
$column->{'type_schema'} || ''
$column->{'type_schema'}
Truth table50%1/2
A
A= $column->{'type_schema'}
Aresult
11
00
127 1 ▶ my $qualified = $ts ne 'public' ? "$ts.$column->{enum_type}" : $column->{enum_type};
Branch: 1 branch
BranchTrueFalseTotal
$ts ne 'public' ? : 1 0 1
128 my $type_info = $self->model->{types}{$qualified}
129 1 ▶ || $self->model->{types}{ "$ts.$column->{enum_type}" };
Condition33%1/3
$self->model->{'types'}{$qualified} || $self->model->{'types'}{"$ts.$column->{'enum_type'}"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->model->{'types'}{$qualified} || $self->model->{'types'}{"$ts.$column->{'enum_type'}"}
$self->model->{'types'}{$qualified} $self->model->{'types'}{"$ts.$column->{'enum_type'}"}
Truth table33%1/3
A || B
A= $self->model->{'types'}{$qualified}B= $self->model->{'types'}{"$ts.$column->{'enum_type'}"}
ABresult
1X1
011
000
130 1 ▶ $enum_values = $type_info->{values} if $type_info;
Branch: 1 branch
BranchTrueFalseTotal
if $type_info 1 0 1
131 }
132  
133 14 return DBIO::PostgreSQL::Introspect::Normalize->data_type($info, $column, $enum_values);
134 }
135  
136 14 ▶sub _normalize_default_value {
Subroutine _normalize_default_value: called CC=1 SCAR=0.0
137 14 return DBIO::PostgreSQL::Introspect::Normalize->default_value(@_[1..$#_]);
138 }
139  
140 1 ▶sub normalize_array {
Subroutine normalize_array: called CC=1 SCAR=0.0
Pod undocumented
141 1 return DBIO::PostgreSQL::Introspect::Normalize->array($_[1]);
142 }
143  
144  
145 3 ▶sub table_columns {
Subroutine table_columns: called CC=2 SCAR=7.0
Pod documented
146 3 my ($self, $table_key) = @_;
147 return [
148 14 map { $self->_normalize_name($_->{column_name}) }
149 22 sort { $a->{ordinal} <=> $b->{ordinal} }
150 3 ▶ @{ $self->model->{columns}{$table_key} || [] }
Condition50%1/2
$self->model->{'columns'}{$table_key} || []
l!l
30
MC/DC0%0/1
$self->model->{'columns'}{$table_key} || []
$self->model->{'columns'}{$table_key}
Truth table50%1/2
A
A= $self->model->{'columns'}{$table_key}
Aresult
11
00
151 ];
152 }
153  
154  
155 3 ▶sub table_columns_info {
Subroutine table_columns_info: called CC=9 SCAR=22.0
Pod documented
156 3 my ($self, $table_key) = @_;
157  
158 3 ▶ $self->{_table_columns_info_cache} //= {};
Condition100%2/2
$self->{"_table_columns_info_cache"} //= {}
l!l
12
MC/DC100%1/1
$self->{"_table_columns_info_cache"} //= {}
$self->{"_table_columns_info_cache"}
Truth table100%2/2
A
A= $self->{"_table_columns_info_cache"}
Aresult
11
00
159 3 my $cached = $self->{_table_columns_info_cache}{$table_key};
160 3 ▶ return { %$cached } if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 0 3 3
161  
162 3 my %pk = map { $_ => 1 } @{ $self->table_pk_info($table_key) };
163 3 my %columns;
164  
165 3 for my $column (
166 22 sort { $a->{ordinal} <=> $b->{ordinal} }
167 3 ▶ @{ $self->model->{columns}{$table_key} || [] }
Condition50%1/2
$self->model->{'columns'}{$table_key} || []
l!l
30
MC/DC0%0/1
$self->model->{'columns'}{$table_key} || []
$self->model->{'columns'}{$table_key}
Truth table50%1/2
A
A= $self->model->{'columns'}{$table_key}
Aresult
11
00
168 ) {
169 14 my $name = $self->_normalize_name($column->{column_name});
170 my $info = {
171 14 ▶ is_nullable => $column->{not_null} ? 0 : 1,
Branch: 1 branch
BranchTrueFalseTotal
$column->{'not_null'} ? : 10 4 14
172 };
173  
174 14 $self->_normalize_data_type($info, $column);
175 14 $self->_normalize_default_value($info, $column->{default_value}, $pk{$name});
176  
177 14 ▶ if ($column->{identity}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->{"identity"}) 1 13 14
178 1 $info->{is_auto_increment} = 1;
179 1 $info->{extra}{identity} = $column->{identity};
180 1 ▶ $info->{retrieve_on_insert} = 1 if $pk{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $pk{$name} 0 1 1
181 }
182  
183 14 ▶ if ($column->{generated}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->{"generated"}) 1 13 14
184 1 $info->{extra}{generated} = $column->{generated};
185 }
186  
187 14 $columns{$name} = $info;
188 }
189  
190 3 $self->{_table_columns_info_cache}{$table_key} = \%columns;
191 3 return { %columns };
192 }
193  
194  
195 8 ▶sub table_pk_info {
Subroutine table_pk_info: called CC=5 SCAR=16.5
Pod documented
196 8 my ($self, $table_key) = @_;
197  
198 8 ▶ my $indexes = $self->model->{indexes}{$table_key} || {};
Condition50%1/2
$self->model->{'indexes'}{$table_key} || {}
l!l
80
MC/DC0%0/1
$self->model->{'indexes'}{$table_key} || {}
$self->model->{'indexes'}{$table_key}
Truth table50%1/2
A
A= $self->model->{'indexes'}{$table_key}
Aresult
11
00
199 8 for my $name (sort keys %$indexes) {
200 16 my $index = $indexes->{$name};
201 16 ▶ next unless $index->{is_primary};
Branch: 1 branch
BranchTrueFalseTotal
unless $index->{"is_primary"} 8 8 16
202 8 ▶ return [ map { $self->_normalize_name($_) } @{ $index->{columns} || [] } ];
Condition50%1/2
$index->{'columns'} || []
l!l
80
MC/DC0%0/1
$index->{'columns'} || []
$index->{'columns'}
Truth table50%1/2
A
A= $index->{'columns'}
Aresult
11
00
203 }
204  
205 0 return [];
206 }
207  
208  
209 5 ▶sub table_uniq_info {
Subroutine table_uniq_info: called CC=11 SCAR=24.6
Pod documented
210 5 my ($self, $table_key) = @_;
211  
212 5 ▶ my $indexes = $self->model->{indexes}{$table_key} || {};
Condition50%1/2
$self->model->{'indexes'}{$table_key} || {}
l!l
50
MC/DC0%0/1
$self->model->{'indexes'}{$table_key} || {}
$self->model->{'indexes'}{$table_key}
Truth table50%1/2
A
A= $self->model->{'indexes'}{$table_key}
Aresult
11
00
213 5 my @uniqs;
214  
215 5 for my $name (sort keys %$indexes) {
216 10 my $index = $indexes->{$name};
217 10 ▶ next unless $index->{is_unique};
Branch: 1 branch
BranchTrueFalseTotal
unless $index->{"is_unique"} 3 7 10
218 7 ▶ next if $index->{is_primary};
Branch: 1 branch
BranchTrueFalseTotal
if $index->{"is_primary"} 5 2 7
219 2 ▶ next if $index->{predicate};
Branch: 1 branch
BranchTrueFalseTotal
if $index->{"predicate"} 0 2 2
220 2 ▶ next if $index->{expressions};
Branch: 1 branch
BranchTrueFalseTotal
if $index->{"expressions"} 1 1 2
221 1 ▶ next unless @{ $index->{columns} || [] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$index->{"columns"} or [];} 0 1 1
Condition50%1/2
$index->{'columns'} || []
l!l
10
MC/DC0%0/1
$index->{'columns'} || []
$index->{'columns'}
Truth table50%1/2
A
A= $index->{'columns'}
Aresult
11
00
222 1 ▶ next if ($index->{access_method} || '') ne 'btree';
Branch: 1 branch
BranchTrueFalseTotal
if ($index->{"access_method"} || "") ne "btree" 0 1 1
Condition50%1/2
$index->{'access_method'} || ''
l!l
10
MC/DC0%0/1
$index->{'access_method'} || ''
$index->{'access_method'}
Truth table50%1/2
A
A= $index->{'access_method'}
Aresult
11
00
223  
224 push @uniqs, [
225 1 $name => [ map { $self->_normalize_name($_) } @{ $index->{columns} } ],
226 ];
227 }
228  
229 5 return \@uniqs;
230 }
231  
232  
233 3 ▶sub table_fk_info {
Subroutine table_fk_info: called CC=5 SCAR=16.4
Pod documented
234 3 my ($self, $table_key) = @_;
235  
236 my @fks = map {
237 {
238 constraint_name => $_->{constraint_name},
239 2 ▶ local_columns => [ map { $self->_normalize_name($_) } @{ $_->{local_columns} || [] } ],
Condition50%1/2
$_->{'local_columns'} || []
l!l
20
MC/DC0%0/1
$_->{'local_columns'} || []
$_->{'local_columns'}
Truth table50%1/2
A
A= $_->{'local_columns'}
Aresult
11
00
240 2 ▶ remote_columns => [ map { $self->_normalize_name($_) } @{ $_->{remote_columns} || [] } ],
Branch: 1 branch
BranchTrueFalseTotal
$_->{'is_deferrable'} ? : 0 2 2
Condition50%1/2
$_->{'remote_columns'} || []
l!l
20
MC/DC0%0/1
$_->{'remote_columns'} || []
$_->{'remote_columns'}
Truth table50%1/2
A
A= $_->{'remote_columns'}
Aresult
11
00
241 remote_schema => $_->{remote_schema},
242 remote_table => $_->{remote_table},
243 attrs => {
244 is_deferrable => $_->{is_deferrable} ? 1 : 0,
245 on_delete => $_->{on_delete},
246 on_update => $_->{on_update},
247 },
248 }
249 3 ▶ } @{ $self->model->{foreign_keys}{$table_key} || [] };
Condition100%2/2
$self->model->{'foreign_keys'}{$table_key} || []
l!l
21
MC/DC100%1/1
$self->model->{'foreign_keys'}{$table_key} || []
$self->model->{'foreign_keys'}{$table_key}
Truth table100%2/2
A
A= $self->model->{'foreign_keys'}{$table_key}
Aresult
11
00
250  
251 3 return \@fks;
252 }
253  
254  
255 3 ▶sub table_is_view {
Subroutine table_is_view: called CC=4 SCAR=14.3
Pod documented
256 3 my ($self, $table_key) = @_;
257 3 ▶ my $table = $self->model->{tables}{$table_key} || {};
Condition50%1/2
$self->model->{'tables'}{$table_key} || {}
l!l
30
MC/DC0%0/1
$self->model->{'tables'}{$table_key} || {}
$self->model->{'tables'}{$table_key}
Truth table50%1/2
A
A= $self->model->{'tables'}{$table_key}
Aresult
11
00
258 3 ▶ return ($table->{kind} || '') =~ /^(?:v|m)\z/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($table->{'kind'} || '') =~ /^(?:v|m)\z/ ? : 1 2 3
Condition50%1/2
$table->{'kind'} || ''
l!l
30
MC/DC0%0/1
$table->{'kind'} || ''
$table->{'kind'}
Truth table50%1/2
A
A= $table->{'kind'}
Aresult
11
00
259 }
260  
261  
262 3 ▶sub view_definition {
Subroutine view_definition: called CC=3 SCAR=11.0
Pod documented
263 3 my ($self, $table_key) = @_;
264 3 ▶ my $table = $self->model->{tables}{$table_key} || {};
Condition50%1/2
$self->model->{'tables'}{$table_key} || {}
l!l
30
MC/DC0%0/1
$self->model->{'tables'}{$table_key} || {}
$self->model->{'tables'}{$table_key}
Truth table50%1/2
A
A= $self->model->{'tables'}{$table_key}
Aresult
11
00
265 3 my $def = $table->{view_definition};
266 3 ▶ return undef unless defined $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def 2 1 3
267 1 $def =~ s/^\s+//;
268 1 $def =~ s/\s+\z//;
269 1 $def =~ s/\s*;\s*\z//;
270 1 return $def;
271 }
272  
273  
274 1 ▶sub table_comment {
Subroutine table_comment: called CC=1 SCAR=0.0
Pod documented
275 1 my ($self, $table_key) = @_;
276 1 return $self->model->{tables}{$table_key}{comment};
277 }
278  
279  
280 1 ▶sub column_comment {
Subroutine column_comment: called CC=5 SCAR=16.5
Pod documented
281 1 my ($self, $table_key, $column_name) = @_;
282  
283 1 ▶ $self->{_column_comment_cache} //= {};
Condition50%1/2
$self->{"_column_comment_cache"} //= {}
l!l
01
MC/DC0%0/1
$self->{"_column_comment_cache"} //= {}
$self->{"_column_comment_cache"}
Truth table50%1/2
A
A= $self->{"_column_comment_cache"}
Aresult
11
00
284 1 ▶ unless (exists $self->{_column_comment_cache}{$table_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_column_comment_cache"}{$table_key}) 1 0 1
285 1 my %cache;
286 1 ▶ for my $column (@{ $self->model->{columns}{$table_key} || [] }) {
Condition50%1/2
$self->model->{'columns'}{$table_key} || []
l!l
10
MC/DC0%0/1
$self->model->{'columns'}{$table_key} || []
$self->model->{'columns'}{$table_key}
Truth table50%1/2
A
A= $self->model->{'columns'}{$table_key}
Aresult
11
00
287 9 $cache{ $self->_normalize_name($column->{column_name}) } = $column->{comment};
288 }
289 1 $self->{_column_comment_cache}{$table_key} = \%cache;
290 }
291 1 return $self->{_column_comment_cache}{$table_key}{ $self->_normalize_name($column_name) };
292 }
293  
294  
295 3 ▶sub result_class_extra_statements {
Subroutine result_class_extra_statements: called CC=45 SCAR=41.0
Pod documented
296 3 my ($self, $table_key) = @_;
297  
298 3 my ($schema_name, $table_name) = split /\./, $table_key, 2;
299 3 my @stmts;
300  
301 3 ▶ push @stmts, [pg_schema => $schema_name]
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema_name and length $schema_name 3 0 3
Condition33%1/3
defined $schema_name and length $schema_name
!ll&&!rl&&r
003
MC/DC0%0/2
defined $schema_name and length $schema_name
defined $schema_name length $schema_name
Truth table33%1/3
A and B
A= defined $schema_nameB= length $schema_name
ABresult
0X0
100
111
302 if defined $schema_name && length $schema_name;
303  
304 3 ▶ my $indexes = $self->model->{indexes}{$table_key} || {};
Condition50%1/2
$self->model->{'indexes'}{$table_key} || {}
l!l
30
MC/DC0%0/1
$self->model->{'indexes'}{$table_key} || {}
$self->model->{'indexes'}{$table_key}
Truth table50%1/2
A
A= $self->model->{'indexes'}{$table_key}
Aresult
11
00
305 3 my %pg_indexes;
306 3 for my $name (sort keys %$indexes) {
307 8 my $index = $indexes->{$name};
308 8 ▶ next if $index->{is_primary};
Branch: 1 branch
BranchTrueFalseTotal
if $index->{"is_primary"} 3 5 8
309  
310 my $simple_unique = $index->{is_unique}
311 && !$index->{predicate}
312 && !$index->{expressions}
313 1 ▶ && @{ $index->{columns} || [] }
Condition50%2/4
$index->{'columns'} || []
l!l
10
$index->{'access_method'} || ''
l!l
10
MC/DC0%0/1
$index->{'columns'} || []
$index->{'columns'}
MC/DC0%0/1
$index->{'access_method'} || ''
$index->{'access_method'}
Truth table50%1/2
A
A= $index->{'columns'}
Aresult
11
00
Truth table50%1/2
A
A= $index->{'access_method'}
Aresult
11
00
314 5 ▶ && ($index->{access_method} || '') eq 'btree';
Condition75%9/12
$index->{'is_unique'} && !$index->{'predicate'} && !$index->{'expressions'} && @{$index->{'columns'} or [];} && ($index->{'access_method'} || '') eq 'btree'
!ll&&!rl&&r
401
$index->{"is_unique"} and not $index->{"predicate"} and not $index->{"expressions"} and @{$index->{"columns"} or [];}
!ll&&!rl&&r
401
$index->{"is_unique"} and not $index->{"predicate"} and not $index->{"expressions"}
!ll&&!rl&&r
311
$index->{"is_unique"} and not $index->{"predicate"}
!ll&&!rl&&r
302
MC/DC40%2/5
$index->{'is_unique'} && !$index->{'predicate'} && !$index->{'expressions'} && @{$index->{'columns'} or [];} && ($index->{'access_method'} || '') eq 'btree'
$index->{"is_unique"} not $index->{"predicate"} not $index->{"expressions"} @{$index->{"columns"} or [];} ($index->{'access_method'} || '') eq 'btree'
Truth table50%3/6
A and not(B) and not(C) and D && E
A= $index->{"is_unique"}B= not $index->{"predicate"}C= not $index->{"expressions"}D= @{$index->{"columns"} or [];}E= ($index->{'access_method'} || '') eq 'btree'
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
315  
316 5 ▶ next if $simple_unique;
Branch: 1 branch
BranchTrueFalseTotal
if $simple_unique 1 4 5
317  
318 4 my %def;
319 3 $def{columns} = [ map { $self->_normalize_name($_) } @{ $index->{columns} } ]
320 4 ▶ if @{ $index->{columns} || [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$index->{"columns"} or [];} 3 1 4
Condition50%1/2
$index->{'columns'} || []
l!l
40
MC/DC0%0/1
$index->{'columns'} || []
$index->{'columns'}
Truth table50%1/2
A
A= $index->{'columns'}
Aresult
11
00
321 4 ▶ $def{expression} = $index->{expressions} if $index->{expressions};
Branch: 1 branch
BranchTrueFalseTotal
if $index->{"expressions"} 1 3 4
322 4 ▶ $def{where} = $index->{predicate} if $index->{predicate};
Branch: 1 branch
BranchTrueFalseTotal
if $index->{"predicate"} 1 3 4
323 $def{using} = $index->{access_method}
324 4 ▶ if ($index->{access_method} || '') ne 'btree';
Branch: 1 branch
BranchTrueFalseTotal
if ($index->{"access_method"} || "") ne "btree" 1 3 4
Condition50%1/2
$index->{'access_method'} || ''
l!l
40
MC/DC0%0/1
$index->{'access_method'} || ''
$index->{'access_method'}
Truth table50%1/2
A
A= $index->{'access_method'}
Aresult
11
00
325 4 ▶ $def{unique} = 1 if $index->{is_unique};
Branch: 1 branch
BranchTrueFalseTotal
if $index->{"is_unique"} 1 3 4
326 1 $def{include} = [ map { $self->_normalize_name($_) } @{ $index->{include_columns} } ]
327 4 ▶ if @{ $index->{include_columns} || [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$index->{"include_columns"} or [];} 1 3 4
Condition50%1/2
$index->{'include_columns'} || []
l!l
40
MC/DC0%0/1
$index->{'include_columns'} || []
$index->{'include_columns'}
Truth table50%1/2
A
A= $index->{'include_columns'}
Aresult
11
00
328 $def{with} = $index->{storage_params}
329 4 ▶ if $index->{storage_params} && %{ $index->{storage_params} };
Branch: 1 branch
BranchTrueFalseTotal
if $index->{"storage_params"} and %{$index->{"storage_params"};} 1 3 4
Condition66%2/3
$index->{"storage_params"} and %{$index->{"storage_params"};}
!ll&&!rl&&r
031
MC/DC50%1/2
$index->{"storage_params"} and %{$index->{"storage_params"};}
$index->{"storage_params"} %{$index->{"storage_params"};}
Truth table66%2/3
A and B
A= $index->{"storage_params"}B= %{$index->{"storage_params"};}
ABresult
0X0
100
111
330  
331 4 ▶ $pg_indexes{$name} = \%def if %def;
Branch: 1 branch
BranchTrueFalseTotal
if %def 4 0 4
332 }
333 3 for my $name (sort keys %pg_indexes) {
334 4 push @stmts, [pg_index => $name, $pg_indexes{$name}];
335 }
336  
337 3 ▶ my $triggers = $self->model->{triggers}{$table_key} || {};
Condition100%2/2
$self->model->{'triggers'}{$table_key} || {}
l!l
12
MC/DC100%1/1
$self->model->{'triggers'}{$table_key} || {}
$self->model->{'triggers'}{$table_key}
Truth table100%2/2
A
A= $self->model->{'triggers'}{$table_key}
Aresult
11
00
338 3 for my $name (sort keys %$triggers) {
339 1 my $trigger = $triggers->{$name};
340 1 ▶ my ($execute) = ($trigger->{definition} || '') =~ /\bEXECUTE\s+FUNCTION\s+(.+?)\s*;?\s*\z/i;
Condition50%1/2
$trigger->{'definition'} || ''
l!l
10
MC/DC0%0/1
$trigger->{'definition'} || ''
$trigger->{'definition'}
Truth table50%1/2
A
A= $trigger->{'definition'}
Aresult
11
00
341 push @stmts, [pg_trigger => $name, {
342 when => $trigger->{timing},
343 event => $trigger->{event},
344 for_each => $trigger->{orientation},
345 1 ▶ ($execute ? (execute => $execute) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$execute ? : 1 0 1
346 }];
347 }
348  
349 3 ▶ my $table = $self->model->{tables}{$table_key} || {};
Condition50%1/2
$self->model->{'tables'}{$table_key} || {}
l!l
30
MC/DC0%0/1
$self->model->{'tables'}{$table_key} || {}
$self->model->{'tables'}{$table_key}
Truth table50%1/2
A
A= $self->model->{'tables'}{$table_key}
Aresult
11
00
350 3 ▶ my $policies = $self->model->{policies}{$table_key} || {};
Condition100%2/2
$self->model->{'policies'}{$table_key} || {}
l!l
12
MC/DC100%1/1
$self->model->{'policies'}{$table_key} || {}
$self->model->{'policies'}{$table_key}
Truth table100%2/2
A
A= $self->model->{'policies'}{$table_key}
Aresult
11
00
351 3 ▶ if ($table->{rls_enabled} || $table->{rls_forced} || keys %$policies) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table->{"rls_enabled"} or $table->{"rls_forced"} or keys %$policies) 1 2 3
Condition66%4/6
$table->{"rls_enabled"} or $table->{"rls_forced"} or keys %$policies
l!l&&r!l&&!r
102
$table->{"rls_enabled"} or $table->{"rls_forced"}
l!l&&r!l&&!r
102
MC/DC33%1/3
$table->{"rls_enabled"} or $table->{"rls_forced"} or keys %$policies
$table->{"rls_enabled"} $table->{"rls_forced"} keys %$policies
Truth table50%2/4
A or B or C
A= $table->{"rls_enabled"}B= $table->{"rls_forced"}C= keys %$policies
ABCresult
1XX1
01X1
0011
0000
352 1 my %defs;
353 1 for my $name (sort keys %$policies) {
354 1 my $policy = $policies->{$name};
355 $defs{$name} = {
356 for => $policy->{command} || 'ALL',
357 ($policy->{roles} ? (roles => $self->normalize_array($policy->{roles})) : ()),
358 ($policy->{using_expr} ? (using => $policy->{using_expr}) : ()),
359 1 ▶ ($policy->{check_expr} ? (with_check => $policy->{check_expr}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
$policy->{'roles'} ? : 1 0 1
$policy->{'using_expr'} ? : 1 0 1
$policy->{'check_expr'} ? : 1 0 1
Condition50%1/2
$policy->{'command'} || 'ALL'
l!l
10
MC/DC0%0/1
$policy->{'command'} || 'ALL'
$policy->{'command'}
Truth table50%1/2
A
A= $policy->{'command'}
Aresult
11
00
360 };
361 }
362 push @stmts, [pg_rls => {
363 enable => $table->{rls_enabled} ? 1 : 0,
364 1 ▶ force => $table->{rls_forced} ? 1 : 0,
Branch: 3 branches
BranchTrueFalseTotal
$table->{'rls_enabled'} ? : 1 0 1
$table->{'rls_forced'} ? : 1 0 1
keys %defs ? : 1 0 1
365 (keys %defs ? (policies => \%defs) : ()),
366 }];
367 }
368  
369 3 ▶ my $checks = $self->model->{check_constraints}{$table_key} || {};
Condition100%2/2
$self->model->{'check_constraints'}{$table_key} || {}
l!l
12
MC/DC100%1/1
$self->model->{'check_constraints'}{$table_key} || {}
$self->model->{'check_constraints'}{$table_key}
Truth table100%2/2
A
A= $self->model->{'check_constraints'}{$table_key}
Aresult
11
00
370 3 for my $name (sort keys %$checks) {
371 1 push @stmts, [pg_check_constraint => $name, $checks->{$name}];
372 }
373  
374 3 return @stmts;
375 }
376  
377  
378 15 1;
379  
380 __END__
381  
382 =pod
383  
384 =encoding UTF-8
385  
386 =head1 NAME
387  
388 DBIO::PostgreSQL::Introspect - Introspect a PostgreSQL database via pg_catalog
389  
390 =head1 VERSION
391  
392 version 0.900001
393  
394 =head1 DESCRIPTION
395  
396 C<DBIO::PostgreSQL::Introspect> reads the live state of a PostgreSQL database
397 via C<pg_catalog> and returns a unified model hashref. It is the source side
398 of the test-deploy-and-compare strategy used by L<DBIO::PostgreSQL::Deploy>.
399  
400 my $intro = DBIO::PostgreSQL::Introspect->new(
401 dbh => $dbh,
402 schema_filter => [qw( public auth api )],
403 );
404 my $model = $intro->model;
405 # $model->{schemas}, $model->{tables}, $model->{columns}, ...
406  
407 The model is built lazily on first access and covers schemas, extensions,
408 types (enums/composites/ranges), tables, columns, indexes, triggers,
409 functions, RLS policies, and sequences. The same model shape is consumed by
410 L<DBIO::PostgreSQL::Diff> and by the test-deploy workflow in
411 L<DBIO::PostgreSQL::Deploy>. On top of it, this class implements the
412 normalized generation contract from L<DBIO::Introspect::Base> so it can act
413 as a L<DBIO::Generate> source.
414  
415 =head1 ATTRIBUTES
416  
417 =head2 schema_filter
418  
419 Optional ArrayRef of PostgreSQL schema names to restrict introspection to.
420 When C<undef>, all non-system schemas are introspected.
421  
422 =head1 METHODS
423  
424 =head2 table_keys
425  
426 Returns sorted ArrayRef of schema-qualified table keys (C<schema.name>).
427  
428 =head2 table_columns
429  
430 my \@names = $intro->table_columns($key);
431  
432 Ordered list of column names for C<$key>.
433  
434 =head2 table_columns_info
435  
436 my \%info = %{ $intro->table_columns_info($key) };
437  
438 Hashref of column metadata.
439  
440 =head2 table_pk_info
441  
442 my \@pk_cols = @{ $intro->table_pk_info($key) };
443  
444 Ordered list of primary key column names.
445  
446 =head2 table_uniq_info
447  
448 my \@constraints = @{ $intro->table_uniq_info($key) };
449  
450 List of C<[ $constraint_name, \@col_names ]> pairs.
451  
452 =head2 table_fk_info
453  
454 my \@fks = @{ $intro->table_fk_info($key) };
455  
456 Each FK is a hashref.
457  
458 =head2 table_is_view
459  
460 Returns true if C<$key> is a view.
461  
462 =head2 view_definition
463  
464 SQL text of the view definition, or C<undef>.
465  
466 =head2 table_comment
467  
468 Comment string for the table, or C<undef>.
469  
470 =head2 column_comment
471  
472 Comment string for a column, or C<undef>.
473  
474 =head2 result_class_extra_statements
475  
476 my @stmts = $intro->result_class_extra_statements($key);
477  
478 Driver-specific PostgreSQL statements for the generated Result class.
479 Emits pg_schema, pg_index, pg_trigger, pg_rls, pg_check_constraint.
480  
481 =head1 NORMALIZED CONTRACT
482  
483 These methods implement the generation contract defined in
484 L<DBIO::Introspect::Base>. They build on the native model.
485  
486 =head2 identity_kind
487  
488 my $kind = DBIO::PostgreSQL::Introspect->identity_kind($id);
489  
490 Maps the C<attidentity> opcode from C<pg_catalog.pg_attribute> to the SQL
491 keyword that follows C<GENERATED> in C<CREATE TABLE>: C<'a'> becomes
492 C<'ALWAYS'>, C<'d'> becomes C<'BY DEFAULT'>, anything else (including
493 C<undef> and C<''>) returns C<undef>. The same map is needed by every site
494 that emits identity syntax (Diff::Column, Diff::Table, DDL).
495  
496 =head2 qualified_key
497  
498 my $key = DBIO::PostgreSQL::Introspect->qualified_key($schema, $name);
499 my $key = DBIO::PostgreSQL::Introspect->qualified_key($row);
500  
501 Builds the canonical C<"schema.name"> key used to index introspected
502 models. Accepts either two positional args (C<$schema>, C<$name>) or a
503 single hashref that contains both C<schema_name> and one of C<table_name>,
504 C<type_name>, C<function_name>, C<sequence_name>, C<index_name>, or
505 C<policy_name>. Centralising the construction here keeps every section
506 key in lockstep — if the separator ever changes, only this helper moves.
507  
508 =head2 system_schema_filter
509  
510 my $sql = DBIO::PostgreSQL::Introspect->system_schema_filter($alias);
511  
512 Returns a two-line SQL fragment for the C<pg_catalog> namespace filter
513 that excludes the PostgreSQL reserved namespaces (C<pg_*> and
514 C<information_schema>). The optional C<$alias> is the alias used in the
515 query for the C<pg_namespace> row (defaults to C<n>). Every introspect
516 helper concatenates this fragment into its C<WHERE> clause so the set
517 of excluded namespaces is centralised.
518  
519 =head1 SEE ALSO
520  
521 L<DBIO::PostgreSQL::Deploy>, L<DBIO::PostgreSQL::Diff>.
522  
523 =head1 AUTHOR
524  
525 DBIO & DBIx::Class Authors
526  
527 =head1 COPYRIGHT AND LICENSE
528  
529 Copyright (C) 2026 DBIO Authors
530 Portions Copyright (C) 2005-2025 DBIx::Class Authors
531 Based on DBIx::Class, heavily modified.
532  
533 This is free software; you can redistribute it and/or modify it under
534 the same terms as the Perl 5 programming language system itself.
535  
536 =cut
« PostgreSQL/Introspect/Tables.pm ↑ Summary PostgreSQL/Introspect/Types.pm »