PostgreSQL/Introspect/Normalize.pm

filter: Statementstmt 98.8% 84 / 85 Branchbran 95.0% 38 / 40 Conditioncond 77.2% 17 / 22 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 92.7% 167 / 180 CC 30 SCAR 22.2
CC
30
Cov
95%
default_value
17.1
data_type
10.0
name
3.0
CRAP 9.2
« PostgreSQL/PgSchema.pm ↑ Summary PostgreSQL/Diff/Trigger.pm »
1 package DBIO::PostgreSQL::Introspect::Normalize;
2 # ABSTRACT: Pure normalization helpers for PostgreSQL introspected metadata
3  
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8  
9 59 ▶sub name {
Subroutine name: called CC=3 SCAR=11.0
Pod documented
10 59 my ($class, $name, $preserve_case) = @_;
11 59 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 2 57 59
12 57 ▶ return $preserve_case ? $name : lc $name;
Branch: 1 branch
BranchTrueFalseTotal
$preserve_case ? : 1 56 57
13 }
14  
15  
16 33 ▶sub data_type {
Subroutine data_type: called CC=10 SCAR=23.0
Pod documented
17 33 my ($class, $info, $column, $enum_values) = @_;
18  
19 33 ▶ 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"}) 4 29 33
Condition83%5/6
$column->{"type_category"} and $column->{"type_category"} eq "e" and $column->{"enum_type"}
!ll&&!rl&&r
2904
$column->{"type_category"} and $column->{"type_category"} eq "e"
!ll&&!rl&&r
16134
MC/DC66%2/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 table75%3/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
20 my $qualified = $column->{type_schema} && $column->{type_schema} ne 'public'
21 ? "$column->{type_schema}.$column->{enum_type}"
22 4 ▶ : $column->{enum_type};
Branch: 1 branch
BranchTrueFalseTotal
$column->{'type_schema'} && $column->{'type_schema'} ne 'public' ? : 2 2 4
Condition66%2/3
$column->{"type_schema"} and $column->{"type_schema"} ne "public"
!ll&&!rl&&r
022
MC/DC50%1/2
$column->{"type_schema"} and $column->{"type_schema"} ne "public"
$column->{"type_schema"} $column->{"type_schema"} ne "public"
Truth table66%2/3
A and B
A= $column->{"type_schema"}B= $column->{"type_schema"} ne "public"
ABresult
0X0
100
111
23  
24 4 $info->{data_type} = 'enum';
25 4 ▶ $info->{extra}{list} = $class->array($enum_values)
Branch: 1 branch
BranchTrueFalseTotal
if $enum_values 2 2 4
26 if $enum_values;
27 4 $info->{extra}{custom_type_name} = $qualified;
28 4 $info->{pg_enum_type} = $qualified;
29 4 return;
30 }
31  
32 29 ▶ my $type = lc($column->{data_type} || '');
Condition50%1/2
$column->{'data_type'} || ''
l!l
290
MC/DC0%0/1
$column->{'data_type'} || ''
$column->{'data_type'}
Truth table50%1/2
A
A= $column->{'data_type'}
Aresult
11
00
33  
34 # Dispatch table: each entry is [pattern, callback]. The callback
35 # mutates $info and returns 1 to claim the match, 0 to fall through.
36 my @DISPATCH = (
37 4 ▶ [qr/^(?:character varying|varchar)\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 4 $info->{data_type} = 'varchar';
39 4 $info->{size} = 0 + $1;
40 }],
41 1 ▶ [qr/^character varying\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 1 $info->{data_type} = 'text';
43 1 $info->{original}{data_type} = 'varchar';
44 }],
45 1 ▶ [qr/^(?:character|char)\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 1 $info->{data_type} = 'char';
47 1 $info->{size} = 0 + $1;
48 }],
49 1 ▶ [qr/^(numeric|decimal)\((\d+),(\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 1 $info->{data_type} = $1;
51 1 $info->{size} = [ 0 + $2, 0 + $3 ];
52 }],
53 1 ▶ [qr/^(?:bit varying|varbit)\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 1 $info->{data_type} = 'varbit';
55 1 $info->{size} = 0 + $1;
56 }],
57 1 ▶ [qr/^bit\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 1 $info->{data_type} = 'bit';
59 1 $info->{size} = 0 + $1;
60 }],
61 2 ▶ [qr/^(vector|halfvec|sparsevec)\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 2 $info->{data_type} = $1;
63 2 $info->{size} = 0 + $2;
64 }],
65 1 ▶ [qr/^(timestamp|time)\((\d+)\) without time zone\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 1 $info->{data_type} = $1;
67 1 $info->{size} = 0 + $2;
68 }],
69 1 ▶ [qr/^timestamp without time zone\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 1 $info->{data_type} = 'timestamp';
71 }],
72 1 ▶ [qr/^time without time zone\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 1 $info->{data_type} = 'time';
74 }],
75 2 ▶ [qr/^(interval|timestamp with time zone|time with time zone)\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 2 $info->{data_type} = $1;
77 2 $info->{size} = 0 + $2;
78 29 }],
79 );
80  
81 29 for my $entry (@DISPATCH) {
82 230 my ($re, $cb) = @$entry;
83 230 ▶ if ($type =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /$re/) 16 214 230
84 16 $cb->();
85 16 return;
86 }
87 }
88  
89 # Fallback aliases (when no size-bearing match wins).
90 13 $type =~ s/^character$/char/;
91 13 $type =~ s/^character varying$/varchar/;
92 13 $type =~ s/^bit varying$/varbit/;
93 13 $info->{data_type} = $type;
94 }
95  
96  
97 27 ▶sub default_value {
Subroutine default_value: called CC=17 SCAR=28.4
Pod documented
98 27 my ($class, $info, $default, $is_primary_key) = @_;
99  
100 27 ▶ return unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 9 18 27
101  
102 18 my $value = $default;
103 18 $value =~ s/^\s+//;
104 18 $value =~ s/\s+\z//;
105  
106 18 ▶ if ($value =~ /\bnextval\('([^']+)'::(?:text|regclass)\)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\bnextval\('([^']+)'::(?:text|regclass)\)/i) 4 14 18
107 4 $info->{is_auto_increment} = 1;
108 4 $info->{sequence} = $1;
109 4 ▶ $info->{retrieve_on_insert} = 1 if $is_primary_key;
Branch: 1 branch
BranchTrueFalseTotal
if $is_primary_key 4 0 4
110 4 return;
111 }
112  
113 14 ▶ if ($value =~ /^["'](.*?)['"](?:::[\w\s\."]+)?\z/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($value =~ /^["'](.*?)['"](?:::[\w\s\."]+)?\z/) { } 6 8 14
elsif ($value =~ /^\((-?\d.*?)\)(?:::[\w\s\."]+)?\z/) { } 1 7 8
elsif ($value =~ /^(-?\d.*?)(?:::[\w\s\."]+)?\z/) { } 4 3 7
elsif ($value =~ /^NULL:?/i) { } 1 2 3
114 6 $info->{default_value} = $1;
115 }
116 elsif ($value =~ /^\((-?\d.*?)\)(?:::[\w\s\."]+)?\z/) {
117 1 $info->{default_value} = $1;
118 }
119 elsif ($value =~ /^(-?\d.*?)(?:::[\w\s\."]+)?\z/) {
120 4 $info->{default_value} = $1;
121 }
122 elsif ($value =~ /^NULL:?/i) {
123 1 my $null = 'null';
124 1 $info->{default_value} = \$null;
125 }
126 else {
127 2 ▶ my $literal = lc($value) eq 'now()' ? 'current_timestamp' : $value;
Branch: 1 branch
BranchTrueFalseTotal
lc $value eq 'now()' ? : 1 1 2
128 2 $literal =~ s/\bCURRENT_TIMESTAMP\b/lc $&/ge;
129 2 $info->{default_value} = \$literal;
130 }
131  
132 14 ▶ if (!$info->{is_auto_increment} && $is_primary_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $info->{"is_auto_increment"} and $is_primary_key) 1 13 14
Condition66%2/3
not $info->{"is_auto_increment"} and $is_primary_key
!ll&&!rl&&r
0131
MC/DC50%1/2
not $info->{"is_auto_increment"} and $is_primary_key
not $info->{"is_auto_increment"} $is_primary_key
Truth table66%2/3
not(A) and B
A= not $info->{"is_auto_increment"}B= $is_primary_key
ABresult
0X0
100
111
133 1 $info->{retrieve_on_insert} = 1;
134 }
135  
136 14 ▶ my $type = $info->{data_type} || '';
Condition100%2/2
$info->{'data_type'} || ''
l!l
68
MC/DC100%1/1
$info->{'data_type'} || ''
$info->{'data_type'}
Truth table100%2/2
A
A= $info->{'data_type'}
Aresult
11
00
137 14 ▶ if ($type =~ /^bool/i && exists $info->{default_value} && !ref $info->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^bool/i and exists $info->{"default_value"} and not ref $info->{"default_value"}) 2 12 14
Condition83%5/6
$type =~ /^bool/i and exists $info->{"default_value"} and not ref $info->{"default_value"}
!ll&&!rl&&r
1112
$type =~ /^bool/i and exists $info->{"default_value"}
!ll&&!rl&&r
1103
MC/DC66%2/3
$type =~ /^bool/i and exists $info->{"default_value"} and not ref $info->{"default_value"}
$type =~ /^bool/i exists $info->{"default_value"} not ref $info->{"default_value"}
Truth table75%3/4
A and B and not(C)
A= $type =~ /^bool/iB= exists $info->{"default_value"}C= not ref $info->{"default_value"}
ABCresult
0XX0
10X0
1100
1111
138 2 ▶ if ($info->{default_value} eq '0') {
Branch: 2 branches
BranchTrueFalseTotal
if ($info->{'default_value'} eq '0') { } 1 1 2
elsif ($info->{'default_value'} eq '1') { } 1 0 1
139 1 my $false = 'false';
140 1 $info->{default_value} = \$false;
141 }
142 elsif ($info->{default_value} eq '1') {
143 1 my $true = 'true';
144 1 $info->{default_value} = \$true;
145 }
146 }
147 }
148  
149  
150 7 ▶sub array {
Subroutine array: called CC=3 SCAR=11.0
Pod documented
151 7 my ($class, $value) = @_;
152 7 ▶ return undef if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 6 7
153 6 ▶ return $value if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 3 3 6
154  
155 3 my $raw = $value;
156 3 $raw =~ s/^\{|\}$//g;
157 3 return [ grep { length $_ } split /,/, $raw ];
158 }
159  
160 16 1;
161  
162 __END__
163  
164 =pod
165  
166 =encoding UTF-8
167  
168 =head1 NAME
169  
170 DBIO::PostgreSQL::Introspect::Normalize - Pure normalization helpers for PostgreSQL introspected metadata
171  
172 =head1 VERSION
173  
174 version 0.900001
175  
176 =head1 DESCRIPTION
177  
178 Leaf module that holds the pure-data normalization helpers used by
179 L<DBIO::PostgreSQL::Introspect> to translate raw C<pg_catalog> rows into
180 the canonical column-info shape consumed by L<DBIO::Generate>. None of
181 these subs talk to the database or hold state; they are class methods that
182 take scalars / hashrefs and return scalar / hashref results, so they are
183 trivially unit-testable in isolation.
184  
185 The helpers are:
186  
187 =over 4
188  
189 =item * L</name> -- lowercase / preserve-case policy
190  
191 =item * L</data_type> -- parses C<pg_catalog.format_type> into a
192 C<data_type>/C<size> pair, handling all the special forms
193 (C<varchar(n)>, C<numeric(p,s)>, C<interval(n)>, pgvector, etc.)
194  
195 =item * L</default_value> -- normalises the C<pg_get_expr(adbin, adrelid)>
196 string into a scalar / SCALAR-ref suitable for round-tripping through
197 DBIO and detects the C<nextval(...)> sequence pattern
198  
199 =item * L</array> -- decodes the C<{a,b,c}> form returned by DBD::Pg for
200 array columns into a Perl ArrayRef
201  
202 =back
203  
204 =head2 name
205  
206 my $normalised = DBIO::PostgreSQL::Introspect::Normalize->name(
207 $name, $preserve_case,
208 );
209  
210 Returns the column / index / constraint name as a Perl string. When
211 C<$preserve_case> is false (the default) the name is lowercased to match
212 DBIx::Class convention. C<undef> stays C<undef>.
213  
214 =head2 data_type
215  
216 DBIO::PostgreSQL::Introspect::Normalize->data_type(
217 $info, $column, $enum_values,
218 );
219  
220 Mutates C<$info> in place to set C<data_type> and (where appropriate)
221 C<size> from the raw C<pg_catalog> row. Handles the size-bearing
222 variants (C<varchar(n)>, C<numeric(p,s)>, C<interval(n)>,
223 pgvector C<vector(n)>, etc.) and the size-stripped aliases (C<character
224 varying> B<-> C<text>). Enum columns are detected via C<type_category>
225 and the canonical C<schema.name> reference is recorded under
226 C<pg_enum_type> / C<extra/custom_type_name> / C<extra/list>. Pass the
227 already-fetched C<values> ArrayRef in C<$enum_values> when normalising a
228 column that references an enum (the caller has to look it up in the
229 model because that lookup is stateful and not the leaf module's job).
230  
231 =head2 default_value
232  
233 DBIO::PostgreSQL::Introspect::Normalize->default_value(
234 $info, $default_expr, $is_primary_key,
235 );
236  
237 Normalises the C<pg_get_expr(adbin, adrelid)> string returned by the
238 introspect query into either a Perl scalar, a SCALAR ref (for SQL
239 expressions like C<now()>), or C<nextval('schema.seq'::regclass)>
240 auto-increment metadata. Mutates C<$info> in place.
241  
242 =head2 array
243  
244 my $arr = DBIO::PostgreSQL::Introspect::Normalize->array($value);
245  
246 Decodes the C<{a,b,c}> string DBD::Pg returns for C<text[]> / C<integer[]>
247 columns into a Perl ArrayRef. Already-an-ArrayRef values pass through
248 unchanged. C<undef> returns C<undef>.
249  
250 =head1 AUTHOR
251  
252 DBIO & DBIx::Class Authors
253  
254 =head1 COPYRIGHT AND LICENSE
255  
256 Copyright (C) 2026 DBIO Authors
257 Portions Copyright (C) 2005-2025 DBIx::Class Authors
258 Based on DBIx::Class, heavily modified.
259  
260 This is free software; you can redistribute it and/or modify it under
261 the same terms as the Perl 5 programming language system itself.
262  
263 =cut
« PostgreSQL/PgSchema.pm ↑ Summary PostgreSQL/Diff/Trigger.pm »