PostgreSQL/Introspect/Columns.pm

filter: Statementstmt 38.4% 10 / 26 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 33.3% 14 / 42 CC 5 SCAR 29.5
CC
5
Cov
29%
fetch
30.0
BEGIN
1.0
BEGIN
1.0
CRAP 19.1
« PostgreSQL/Diff/Policy.pm ↑ Summary PostgreSQL/Introspect/Policies.pm »
1 package DBIO::PostgreSQL::Introspect::Columns;
2 # ABSTRACT: Introspect PostgreSQL table columns
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=5 SCAR=34.0
Pod documented
12 0 my ($class, $dbh, $filter) = @_;
13  
14 0 my $sql = q{
15 SELECT
16 n.nspname AS schema_name,
17 c.relname AS table_name,
18 a.attname AS column_name,
19 a.attnum AS ordinal,
20 pg_catalog.format_type(a.atttypid, a.atttypmod) AS data_type,
21 a.attnotnull AS not_null,
22 pg_catalog.pg_get_expr(d.adbin, d.adrelid) AS default_value,
23 a.attidentity AS identity,
24 a.attgenerated AS generated,
25 col_description(c.oid, a.attnum) AS comment,
26 t.typtype AS type_category,
27 CASE WHEN t.typtype = 'e' THEN t.typname END AS enum_type,
28 CASE WHEN t.typtype = 'c' THEN t.typname END AS composite_type,
29 tn.nspname AS type_schema
30 FROM pg_catalog.pg_attribute a
31 JOIN pg_catalog.pg_class c ON c.oid = a.attrelid
32 JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
33 JOIN pg_catalog.pg_type t ON t.oid = a.atttypid
34 JOIN pg_catalog.pg_namespace tn ON tn.oid = t.typnamespace
35 LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid = a.attrelid AND d.adnum = a.attnum
36 WHERE a.attnum > 0
37 AND NOT a.attisdropped
38 AND c.relkind IN ('r', 'v', 'm', 'f', 'p')
39 };
40 0 $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('n');
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 .= ' ORDER BY n.nspname, c.relname, a.attnum';
49  
50 0 my $sth = $dbh->prepare($sql);
51 0 $sth->execute(@bind);
52  
53 0 my %columns;
54 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
55 0 my $table_key = DBIO::PostgreSQL::Introspect->qualified_key($row);
56 0 ▶ push @{ $columns{$table_key} }, {
Branch: 1 branch
BranchTrueFalseTotal
$row->{'not_null'} ? : 0 0 0
57 column_name => $row->{column_name},
58 ordinal => $row->{ordinal},
59 data_type => $row->{data_type},
60 not_null => $row->{not_null} ? 1 : 0,
61 default_value => $row->{default_value},
62 identity => $row->{identity},
63 generated => $row->{generated},
64 comment => $row->{comment},
65 type_category => $row->{type_category},
66 enum_type => $row->{enum_type},
67 composite_type => $row->{composite_type},
68 type_schema => $row->{type_schema},
69 0 };
70 }
71  
72 0 return \%columns;
73 }
74  
75 15 1;
76  
77 __END__
78  
79 =pod
80  
81 =encoding UTF-8
82  
83 =head1 NAME
84  
85 DBIO::PostgreSQL::Introspect::Columns - Introspect PostgreSQL table columns
86  
87 =head1 VERSION
88  
89 version 0.900001
90  
91 =head1 DESCRIPTION
92  
93 Fetches column metadata for all tables from C<pg_catalog.pg_attribute>,
94 including data types (via C<format_type>), nullability, default expressions,
95 identity/generated column markers, and enum/composite type names.
96  
97 =head1 METHODS
98  
99 =head2 fetch
100  
101 my $columns = DBIO::PostgreSQL::Introspect::Columns->fetch($dbh, $filter);
102  
103 Returns a hashref keyed by C<schema.table>. Each value is an ArrayRef of
104 column hashrefs ordered by C<attnum>. Each column hashref has keys:
105 C<column_name>, C<ordinal>, C<data_type>, C<not_null>, C<default_value>,
106 C<identity>, C<generated>, C<comment>, C<type_category>, C<enum_type>,
107 C<composite_type>, C<type_schema>.
108  
109 =head1 AUTHOR
110  
111 DBIO & DBIx::Class Authors
112  
113 =head1 COPYRIGHT AND LICENSE
114  
115 Copyright (C) 2026 DBIO Authors
116 Portions Copyright (C) 2005-2025 DBIx::Class Authors
117 Based on DBIx::Class, heavily modified.
118  
119 This is free software; you can redistribute it and/or modify it under
120 the same terms as the Perl 5 programming language system itself.
121  
122 =cut
« PostgreSQL/Diff/Policy.pm ↑ Summary PostgreSQL/Introspect/Policies.pm »