blib/lib/DBIO/PostgreSQL/Introspect/Types.pm

filter: Statementstmt 16.9% 11 / 65 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 42.8% 3 / 7 Podpod 100% 1 / 1 totaltotal 13.1% 15 / 114 CC 16 SCAR 35.9
CC
16
Cov
11%
_fetch_composites
56.0
_fetch_enums
42.0
_fetch_ranges
30.0
CRAP 36.3
« blib/lib/DBIO/PostgreSQL/Introspect/Functions.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/CheckConstraints.pm »
1 package DBIO::PostgreSQL::Introspect::Types;
2 # ABSTRACT: Introspect PostgreSQL types (enums, composites, ranges)
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=1 SCAR=6.9
Pod documented
14 0 my ($class, $dbh, $filter) = @_;
15  
16 0 my $types = {};
17  
18 0 $class->_fetch_enums($dbh, $filter, $types);
19 0 $class->_fetch_composites($dbh, $filter, $types);
20 0 $class->_fetch_ranges($dbh, $filter, $types);
21  
22 0 return $types;
23 }
24  
25 0 ▶sub _fetch_enums {
Subroutine _fetch_enums: not called CC=6 SCAR=37.4
26 0 my ($class, $dbh, $filter, $types) = @_;
27  
28 0 my $sql = q{
29 SELECT
30 n.nspname AS schema_name,
31 t.typname AS type_name,
32 array_agg(e.enumlabel ORDER BY e.enumsortorder)
33 FILTER (WHERE e.enumlabel IS NOT NULL) AS enum_values
34 FROM pg_catalog.pg_type t
35 JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
36 LEFT JOIN pg_catalog.pg_enum e ON e.enumtypid = t.oid
37 WHERE t.typtype = 'e'
38 };
39 0 $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n');
40 0 $sql .= DBIO::PostgreSQL::Introspect->extension_member_filter('pg_type', 't.oid');
41  
42 0 my @bind;
43 0 ▶ if ($filter && @$filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter and @$filter) 0 0 0
Condition0%0/3
$filter and @$filter
!ll&&!rl&&r
000
MC/DC0%0/2
$filter and @$filter
$filter @$filter
Truth table0%0/3
A and B
A= $filterB= @$filter
ABresult
0X0
100
111
44 0 $sql .= ' AND n.nspname = ANY($1)';
45 0 push @bind, $filter;
46 }
47  
48 0 $sql .= ' GROUP BY n.nspname, t.typname ORDER BY n.nspname, t.typname';
49  
50 0 my $sth = $dbh->prepare($sql);
51 0 $sth->execute(@bind);
52  
53 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
54 0 my $key = DBIO::PostgreSQL::Introspect->qualified_key($row);
55 # DBD::Pg returns array_agg as a string like {val1,val2}
56 0 ▶ my $values = $row->{enum_values} // [];
Condition0%0/2
$row->{"enum_values"} // []
l!l
00
MC/DC0%0/1
$row->{"enum_values"} // []
$row->{"enum_values"}
Truth table0%0/2
A
A= $row->{"enum_values"}
Aresult
11
00
57 0 ▶ $values = DBIO::PostgreSQL::Introspect->normalize_array($values)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values 0 0 0
58 if !ref $values;
59 $types->{$key} = {
60 schema_name => $row->{schema_name},
61 type_name => $row->{type_name},
62 0 type_kind => 'enum',
63 values => $values,
64 };
65 }
66 }
67  
68 0 ▶sub _fetch_composites {
Subroutine _fetch_composites: not called CC=7 SCAR=40.3
69 0 my ($class, $dbh, $filter, $types) = @_;
70  
71 0 my $sql = q{
72 SELECT
73 n.nspname AS schema_name,
74 t.typname AS type_name,
75 a.attname AS attr_name,
76 a.attnum AS ordinal,
77 pg_catalog.format_type(a.atttypid, a.atttypmod) AS attr_type,
78 CASE WHEN a.attcollation <> 0 AND a.attcollation <> att.typcollation THEN (
79 SELECT pg_catalog.quote_ident(cn.nspname) || '.' || pg_catalog.quote_ident(co.collname)
80 FROM pg_catalog.pg_collation co
81 JOIN pg_catalog.pg_namespace cn ON cn.oid = co.collnamespace
82 WHERE co.oid = a.attcollation) END AS attr_collation
83 FROM pg_catalog.pg_type t
84 JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
85 JOIN pg_catalog.pg_class c ON c.oid = t.typrelid
86 LEFT JOIN pg_catalog.pg_attribute a
87 ON a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped
88 LEFT JOIN pg_catalog.pg_type att ON att.oid = a.atttypid
89 WHERE t.typtype = 'c'
90 AND c.relkind = 'c'
91 };
92 0 $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n');
93 0 $sql .= DBIO::PostgreSQL::Introspect->extension_member_filter('pg_type', 't.oid');
94  
95 0 my @bind;
96 0 ▶ if ($filter && @$filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter and @$filter) 0 0 0
Condition0%0/3
$filter and @$filter
!ll&&!rl&&r
000
MC/DC0%0/2
$filter and @$filter
$filter @$filter
Truth table0%0/3
A and B
A= $filterB= @$filter
ABresult
0X0
100
111
97 0 $sql .= ' AND n.nspname = ANY($1)';
98 0 push @bind, $filter;
99 }
100  
101 0 $sql .= ' ORDER BY n.nspname, t.typname, a.attnum';
102  
103 0 my $sth = $dbh->prepare($sql);
104 0 $sth->execute(@bind);
105  
106 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
107 0 my $key = DBIO::PostgreSQL::Introspect->qualified_key($row);
108 $types->{$key} //= {
109 schema_name => $row->{schema_name},
110 type_name => $row->{type_name},
111 0 ▶ type_kind => 'composite',
Condition0%0/2
$types->{$key} //= {"schema_name", $row->{"schema_name"}, "type_name", $row->{"type_name"}, "type_kind", "composite", "attributes", []}
l!l
00
MC/DC0%0/1
$types->{$key} //= {"schema_name", $row->{"schema_name"}, "type_name", $row->{"type_name"}, "type_kind", "composite", "attributes", []}
$types->{$key}
Truth table0%0/2
A
A= $types->{$key}
Aresult
11
00
112 attributes => [],
113 };
114 0 ▶ next unless defined $row->{attr_name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row->{"attr_name"} 0 0 0
115 0 ▶ push @{ $types->{$key}{attributes} }, {
Branch: 1 branch
BranchTrueFalseTotal
defined $row->{'attr_collation'} ? : 0 0 0
116 name => $row->{attr_name},
117 type => $row->{attr_type},
118 ordinal => $row->{ordinal},
119 0 ( defined $row->{attr_collation} ? (collation => $row->{attr_collation}) : () ),
120 };
121 }
122 }
123  
124 0 ▶sub _fetch_ranges {
Subroutine _fetch_ranges: not called CC=5 SCAR=34.0
125 0 my ($class, $dbh, $filter, $types) = @_;
126  
127 0 my $sql = q{
128 SELECT
129 n.nspname AS schema_name,
130 t.typname AS type_name,
131 pg_catalog.format_type(r.rngsubtype, NULL) AS subtype,
132 CASE WHEN r.rngcollation <> 0 AND r.rngcollation <> st.typcollation THEN (
133 SELECT pg_catalog.quote_ident(cn.nspname) || '.' || pg_catalog.quote_ident(co.collname)
134 FROM pg_catalog.pg_collation co
135 JOIN pg_catalog.pg_namespace cn ON cn.oid = co.collnamespace
136 WHERE co.oid = r.rngcollation) END AS collation,
137 CASE WHEN NOT oc.opcdefault THEN (
138 SELECT pg_catalog.quote_ident(opn.nspname) || '.' || pg_catalog.quote_ident(oc.opcname)
139 FROM pg_catalog.pg_namespace opn WHERE opn.oid = oc.opcnamespace) END AS subtype_opclass,
140 CASE WHEN r.rngcanonical <> 0 THEN (
141 SELECT pg_catalog.quote_ident(pn.nspname) || '.' || pg_catalog.quote_ident(pp.proname)
142 FROM pg_catalog.pg_proc pp
143 JOIN pg_catalog.pg_namespace pn ON pn.oid = pp.pronamespace
144 WHERE pp.oid = r.rngcanonical) END AS canonical,
145 CASE WHEN r.rngsubdiff <> 0 THEN (
146 SELECT pg_catalog.quote_ident(pn.nspname) || '.' || pg_catalog.quote_ident(pp.proname)
147 FROM pg_catalog.pg_proc pp
148 JOIN pg_catalog.pg_namespace pn ON pn.oid = pp.pronamespace
149 WHERE pp.oid = r.rngsubdiff) END AS subtype_diff
150 FROM pg_catalog.pg_type t
151 JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
152 JOIN pg_catalog.pg_range r ON r.rngtypid = t.oid
153 JOIN pg_catalog.pg_type st ON st.oid = r.rngsubtype
154 JOIN pg_catalog.pg_opclass oc ON oc.oid = r.rngsubopc
155 WHERE 1=1
156 };
157 0 $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n');
158 0 $sql .= DBIO::PostgreSQL::Introspect->extension_member_filter('pg_type', 't.oid');
159  
160 0 my @bind;
161 0 ▶ if ($filter && @$filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter and @$filter) 0 0 0
Condition0%0/3
$filter and @$filter
!ll&&!rl&&r
000
MC/DC0%0/2
$filter and @$filter
$filter @$filter
Truth table0%0/3
A and B
A= $filterB= @$filter
ABresult
0X0
100
111
162 0 $sql .= ' AND n.nspname = ANY($1)';
163 0 push @bind, $filter;
164 }
165  
166 0 $sql .= ' ORDER BY n.nspname, t.typname';
167  
168 0 my $sth = $dbh->prepare($sql);
169 0 $sth->execute(@bind);
170  
171 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
172 0 my $key = DBIO::PostgreSQL::Introspect->qualified_key($row);
173 $types->{$key} = {
174 schema_name => $row->{schema_name},
175 type_name => $row->{type_name},
176 type_kind => 'range',
177 subtype => $row->{subtype},
178 0 ▶ map { defined $row->{$_} ? ($_ => $row->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $row->{$_} ? : 0 0 0
179 qw( collation subtype_opclass canonical subtype_diff ),
180 };
181 }
182 }
183  
184 171 1;
185  
186 __END__
« blib/lib/DBIO/PostgreSQL/Introspect/Functions.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/CheckConstraints.pm »