| 1 | package DBIO::PostgreSQL::Introspect::Types; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect PostgreSQL types (enums, composites, ranges) | ||||||||||||||||||||||||||||
| 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=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 0 | my ($class, $dbh, $filter) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | my $types = {}; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | $class->_fetch_enums($dbh, $filter, $types); | |||||||||||||||||||||||||||
| 17 | 0 | $class->_fetch_composites($dbh, $filter, $types); | |||||||||||||||||||||||||||
| 18 | 0 | $class->_fetch_ranges($dbh, $filter, $types); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | return $types; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub _fetch_enums { | ||||||||||||||||||||||||||
|
Subroutine
_fetch_enums: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 24 | 0 | my ($class, $dbh, $filter, $types) = @_; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 27 | SELECT | ||||||||||||||||||||||||||||
| 28 | n.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 29 | t.typname AS type_name, | ||||||||||||||||||||||||||||
| 30 | array_agg(e.enumlabel ORDER BY e.enumsortorder) AS enum_values | ||||||||||||||||||||||||||||
| 31 | FROM pg_catalog.pg_type t | ||||||||||||||||||||||||||||
| 32 | JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace | ||||||||||||||||||||||||||||
| 33 | JOIN pg_catalog.pg_enum e ON e.enumtypid = t.oid | ||||||||||||||||||||||||||||
| 34 | WHERE t.typtype = 'e' | ||||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||
| 36 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n'); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | my @bind; | |||||||||||||||||||||||||||
| 39 | 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
| |||||||||||||||||||||||||||||
| 40 | 0 | $sql .= ' AND n.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 41 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | $sql .= ' GROUP BY n.nspname, t.typname ORDER BY n.nspname, t.typname'; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 47 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | my $key = DBIO::PostgreSQL::Introspect->qualified_key($row); | |||||||||||||||||||||||||||
| 51 | # DBD::Pg returns array_agg as a string like {val1,val2} | ||||||||||||||||||||||||||||
| 52 | 0 | my $values = $row->{enum_values}; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | $values = DBIO::PostgreSQL::Introspect->normalize_array($values) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | if !ref $values; | ||||||||||||||||||||||||||||
| 55 | $types->{$key} = { | ||||||||||||||||||||||||||||
| 56 | schema_name => $row->{schema_name}, | ||||||||||||||||||||||||||||
| 57 | type_name => $row->{type_name}, | ||||||||||||||||||||||||||||
| 58 | 0 | type_kind => 'enum', | |||||||||||||||||||||||||||
| 59 | values => $values, | ||||||||||||||||||||||||||||
| 60 | }; | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub _fetch_composites { | ||||||||||||||||||||||||||
|
Subroutine
_fetch_composites: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 65 | 0 | my ($class, $dbh, $filter, $types) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 68 | SELECT | ||||||||||||||||||||||||||||
| 69 | n.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 70 | t.typname AS type_name, | ||||||||||||||||||||||||||||
| 71 | a.attname AS attr_name, | ||||||||||||||||||||||||||||
| 72 | a.attnum AS ordinal, | ||||||||||||||||||||||||||||
| 73 | pg_catalog.format_type(a.atttypid, a.atttypmod) AS attr_type | ||||||||||||||||||||||||||||
| 74 | FROM pg_catalog.pg_type t | ||||||||||||||||||||||||||||
| 75 | JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace | ||||||||||||||||||||||||||||
| 76 | JOIN pg_catalog.pg_class c ON c.oid = t.typrelid | ||||||||||||||||||||||||||||
| 77 | JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid | ||||||||||||||||||||||||||||
| 78 | WHERE t.typtype = 'c' | ||||||||||||||||||||||||||||
| 79 | AND c.relkind = 'c' | ||||||||||||||||||||||||||||
| 80 | AND a.attnum > 0 | ||||||||||||||||||||||||||||
| 81 | AND NOT a.attisdropped | ||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||
| 83 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n'); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | my @bind; | |||||||||||||||||||||||||||
| 86 | 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
| |||||||||||||||||||||||||||||
| 87 | 0 | $sql .= ' AND n.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 88 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | $sql .= ' ORDER BY n.nspname, t.typname, a.attnum'; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 94 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | my $key = DBIO::PostgreSQL::Introspect->qualified_key($row); | |||||||||||||||||||||||||||
| 98 | $types->{$key} //= { | ||||||||||||||||||||||||||||
| 99 | schema_name => $row->{schema_name}, | ||||||||||||||||||||||||||||
| 100 | type_name => $row->{type_name}, | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | type_kind => 'composite', | ||||||||||||||||||||||||||
|
Condition0%0/2
$types->{$key} //= {"schema_name", $row->{"schema_name"}, "type_name", $row->{"type_name"}, "type_kind", "composite", "attributes", []}
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}
| |||||||||||||||||||||||||||||
| 102 | attributes => [], | ||||||||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||||||||
| 104 | 0 | push @{ $types->{$key}{attributes} }, { | |||||||||||||||||||||||||||
| 105 | name => $row->{attr_name}, | ||||||||||||||||||||||||||||
| 106 | type => $row->{attr_type}, | ||||||||||||||||||||||||||||
| 107 | ordinal => $row->{ordinal}, | ||||||||||||||||||||||||||||
| 108 | 0 | }; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub _fetch_ranges { | ||||||||||||||||||||||||||
|
Subroutine
_fetch_ranges: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 113 | 0 | my ($class, $dbh, $filter, $types) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 116 | SELECT | ||||||||||||||||||||||||||||
| 117 | n.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 118 | t.typname AS type_name, | ||||||||||||||||||||||||||||
| 119 | pg_catalog.format_type(r.rngsubtype, NULL) AS subtype | ||||||||||||||||||||||||||||
| 120 | FROM pg_catalog.pg_type t | ||||||||||||||||||||||||||||
| 121 | JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace | ||||||||||||||||||||||||||||
| 122 | JOIN pg_catalog.pg_range r ON r.rngtypid = t.oid | ||||||||||||||||||||||||||||
| 123 | WHERE 1=1 | ||||||||||||||||||||||||||||
| 124 | }; | ||||||||||||||||||||||||||||
| 125 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n'); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | my @bind; | |||||||||||||||||||||||||||
| 128 | 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
| |||||||||||||||||||||||||||||
| 129 | 0 | $sql .= ' AND n.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 130 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | $sql .= ' ORDER BY n.nspname, t.typname'; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 136 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | my $key = DBIO::PostgreSQL::Introspect->qualified_key($row); | |||||||||||||||||||||||||||
| 140 | $types->{$key} = { | ||||||||||||||||||||||||||||
| 141 | schema_name => $row->{schema_name}, | ||||||||||||||||||||||||||||
| 142 | type_name => $row->{type_name}, | ||||||||||||||||||||||||||||
| 143 | type_kind => 'range', | ||||||||||||||||||||||||||||
| 144 | subtype => $row->{subtype}, | ||||||||||||||||||||||||||||
| 145 | 0 | }; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 15 | 1; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =pod | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 NAME | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | DBIO::PostgreSQL::Introspect::Types - Introspect PostgreSQL types (enums, composites, ranges) | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head1 VERSION | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | version 0.900001 | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Fetches user-defined type metadata from C<pg_catalog>: enum types (with | ||||||||||||||||||||||||||||
| 168 | ordered values), composite types (with attributes), and range types (with | ||||||||||||||||||||||||||||
| 169 | subtype). System types are excluded. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 METHODS | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head2 fetch | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | my $types = DBIO::PostgreSQL::Introspect::Types->fetch($dbh, $filter); | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Returns a hashref keyed by C<schema.type_name>. Each value is a hashref with | ||||||||||||||||||||||||||||
| 178 | C<schema_name>, C<type_name>, C<type_kind> (C<enum>, C<composite>, or | ||||||||||||||||||||||||||||
| 179 | C<range>), and kind-specific fields: | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =over 4 | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =item enum: C<values> (ArrayRef, sorted by C<enumsortorder>) | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item composite: C<attributes> (ArrayRef of C<{ name, type, ordinal }>) | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =item range: C<subtype> (subtype name string) | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =back | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 198 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 199 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 202 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =cut | ||||||||||||||||||||||||||||