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

filter: Statementstmt 98.1% 107 / 109 Branchbran 92.5% 50 / 54 Conditioncond 75.0% 18 / 24 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 19 / 19 Podpod 100% 4 / 4 totaltotal 91.9% 205 / 223 CC 38 SCAR 22.6
CC
38
Cov
94%
default_value
17.1
array
11.1
data_type
10.0
CRAP 9.6
« blib/lib/DBIO/PostgreSQL/Diff/Trigger.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Diff/Sequence.pm »
1 package DBIO::PostgreSQL::Introspect::Normalize;
2 # ABSTRACT: Pure normalization helpers for PostgreSQL introspected metadata
3  
4 172 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 172 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 172 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 172 our $VERSION = '0.900007';
10  
11  
12  
13 76 ▶sub name {
Subroutine name: called CC=3 SCAR=11.0
Pod documented
14 76 my ($class, $name, $preserve_case) = @_;
15 76 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 2 74 76
16 74 ▶ return $preserve_case ? $name : lc $name;
Branch: 1 branch
BranchTrueFalseTotal
$preserve_case ? : 1 73 74
17 }
18  
19  
20 60 ▶sub data_type {
Subroutine data_type: called CC=10 SCAR=23.0
Pod documented
21 60 my ($class, $info, $column, $enum_values) = @_;
22  
23 60 ▶ 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 56 60
Condition83%5/6
$column->{"type_category"} and $column->{"type_category"} eq "e" and $column->{"enum_type"}
!ll&&!rl&&r
5604
$column->{"type_category"} and $column->{"type_category"} eq "e"
!ll&&!rl&&r
42144
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
24 my $qualified = $column->{type_schema} && $column->{type_schema} ne 'public'
25 ? "$column->{type_schema}.$column->{enum_type}"
26 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
27  
28 4 $info->{data_type} = 'enum';
29 4 ▶ $info->{extra}{list} = $class->array($enum_values)
Branch: 1 branch
BranchTrueFalseTotal
if $enum_values 2 2 4
30 if $enum_values;
31 4 $info->{extra}{custom_type_name} = $qualified;
32 4 $info->{pg_enum_type} = $qualified;
33 4 return;
34 }
35  
36 56 ▶ my $type = lc($column->{data_type} || '');
Condition50%1/2
$column->{'data_type'} || ''
l!l
560
MC/DC0%0/1
$column->{'data_type'} || ''
$column->{'data_type'}
Truth table50%1/2
A
A= $column->{'data_type'}
Aresult
11
00
37  
38 # Dispatch table: each entry is [pattern, callback]. The callback
39 # mutates $info and returns 1 to claim the match, 0 to fall through.
40 my @DISPATCH = (
41 5 ▶ [qr/^(?:character varying|varchar)\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 5 $info->{data_type} = 'varchar';
43 5 $info->{size} = 0 + $1;
44 }],
45 1 ▶ [qr/^character varying\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 1 $info->{data_type} = 'text';
47 1 $info->{original}{data_type} = 'varchar';
48 }],
49 2 ▶ [qr/^(?:character|char)\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 2 $info->{data_type} = 'char';
51 2 $info->{size} = 0 + $1;
52 }],
53 3 ▶ [qr/^(numeric|decimal)\((\d+),(\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 3 $info->{data_type} = $1;
55 3 $info->{size} = [ 0 + $2, 0 + $3 ];
56 }],
57 2 ▶ [qr/^(?:bit varying|varbit)\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 2 $info->{data_type} = 'varbit';
59 2 $info->{size} = 0 + $1;
60 }],
61 2 ▶ [qr/^bit\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 2 $info->{data_type} = 'bit';
63 2 $info->{size} = 0 + $1;
64 }],
65 # A model read under an empty search_path has the schema in front of
66 # the type: public.vector(3).
67 8 ▶ [qr/^(?:(?:"[^"]+"|\w+)\.)?(vector|halfvec|sparsevec)\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 8 $info->{data_type} = $1;
69 8 $info->{size} = 0 + $2;
70 }],
71 2 ▶ [qr/^(timestamp|time)\((\d+)\) without time zone\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 2 $info->{data_type} = $1;
73 2 $info->{size} = 0 + $2;
74 }],
75 1 ▶ [qr/^timestamp without time zone\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 1 $info->{data_type} = 'timestamp';
77 }],
78 2 ▶ [qr/^time without time zone\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
79 2 $info->{data_type} = 'time';
80 }],
81 # format_type writes the precision behind the first word:
82 # "timestamp(3) with time zone", never "timestamp with time zone(3)".
83 5 ▶ [qr/^(timestamp|time)\((\d+)\) with time zone\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 5 $info->{data_type} = "$1 with time zone";
85 5 $info->{size} = 0 + $2;
86 }],
87 2 ▶ [qr/^interval\((\d+)\)\z/, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 2 $info->{data_type} = 'interval';
89 2 $info->{size} = 0 + $1;
90 56 }],
91 );
92  
93 56 for my $entry (@DISPATCH) {
94 479 my ($re, $cb) = @$entry;
95 479 ▶ if ($type =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /$re/) 35 444 479
96 35 $cb->();
97 35 return;
98 }
99 }
100  
101 # Fallback aliases (when no size-bearing match wins).
102 21 $type =~ s/^character$/char/;
103 21 $type =~ s/^character varying$/varchar/;
104 21 $type =~ s/^bit varying$/varbit/;
105 21 $info->{data_type} = $type;
106 }
107  
108  
109 28 ▶sub default_value {
Subroutine default_value: called CC=17 SCAR=28.4
Pod documented
110 28 my ($class, $info, $default, $is_primary_key) = @_;
111  
112 28 ▶ return unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 10 18 28
113  
114 18 my $value = $default;
115 18 $value =~ s/^\s+//;
116 18 $value =~ s/\s+\z//;
117  
118 18 ▶ if ($value =~ /\bnextval\('([^']+)'::(?:text|regclass)\)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\bnextval\('([^']+)'::(?:text|regclass)\)/i) 4 14 18
119 4 $info->{is_auto_increment} = 1;
120 4 $info->{sequence} = $1;
121 4 ▶ $info->{retrieve_on_insert} = 1 if $is_primary_key;
Branch: 1 branch
BranchTrueFalseTotal
if $is_primary_key 4 0 4
122 4 return;
123 }
124  
125 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
126 6 $info->{default_value} = $1;
127 }
128 elsif ($value =~ /^\((-?\d.*?)\)(?:::[\w\s\."]+)?\z/) {
129 1 $info->{default_value} = $1;
130 }
131 elsif ($value =~ /^(-?\d.*?)(?:::[\w\s\."]+)?\z/) {
132 4 $info->{default_value} = $1;
133 }
134 elsif ($value =~ /^NULL:?/i) {
135 1 my $null = 'null';
136 1 $info->{default_value} = \$null;
137 }
138 else {
139 2 ▶ my $literal = lc($value) eq 'now()' ? 'current_timestamp' : $value;
Branch: 1 branch
BranchTrueFalseTotal
lc $value eq 'now()' ? : 1 1 2
140 0 $literal =~ s/\bCURRENT_TIMESTAMP\b/lc $&/ge;
141 2 $info->{default_value} = \$literal;
142 }
143  
144 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
145 1 $info->{retrieve_on_insert} = 1;
146 }
147  
148 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
149 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
150 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
151 1 my $false = 'false';
152 1 $info->{default_value} = \$false;
153 }
154 elsif ($info->{default_value} eq '1') {
155 1 my $true = 'true';
156 1 $info->{default_value} = \$true;
157 }
158 }
159 }
160  
161  
162 13 ▶sub array {
Subroutine array: called CC=11 SCAR=24.1
Pod documented
163 13 my ($class, $value) = @_;
164 13 ▶ return undef if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 12 13
165 12 ▶ return $value if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 3 9 12
166  
167 9 my $raw = $value;
168 9 $raw =~ s/\A\s+|\s+\z//g;
169 9 ▶ Carp::croak('not a PostgreSQL array literal')
Branch: 1 branch
BranchTrueFalseTotal
unless $raw =~ /\A\{(.*)\}\z/s 1 8 9
170 unless $raw =~ /\A\{(.*)\}\z/s;
171 8 my $body = $1;
172 8 ▶ return [] if $body =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\A\s*\z/ 2 6 8
173  
174 6 my @items;
175 6 pos($body) = 0;
176 6 while (1) {
177 19 $body =~ /\G\s*/gc;
178 19 ▶ if ($body =~ /\G"((?:[^"\\]|\\.)*)"\s*/gcs) {
Branch: 2 branches
BranchTrueFalseTotal
if ($body =~ /\G"((?:[^"\\]|\\.)*)"\s*/cgs) { } 8 11 19
elsif ($body =~ /\G((?:[^,"\\{}]|\\.)*)/cgs) { } 11 0 11
179 8 (my $item = $1) =~ s/\\(.)/$1/gs;
180 8 push @items, $item;
181 }
182 elsif ($body =~ /\G((?:[^,"\\{}]|\\.)*)/gcs) {
183 11 my $item = $1;
184 11 $item =~ s/\s+\z//;
185 11 my $was_null = $item =~ /\Anull\z/i;
186 11 $item =~ s/\\(.)/$1/gs;
187 11 ▶ push @items, $was_null ? undef : $item;
Branch: 1 branch
BranchTrueFalseTotal
$was_null ? : 2 9 11
188 }
189 19 ▶ if ($body =~ /\G,/gc) { next }
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /\G,/cg) 13 6 19
190 6 ▶ last if (pos($body) // 0) == length $body;
Branch: 1 branch
BranchTrueFalseTotal
if (pos $body // 0) == length $body 6 0 6
Condition50%1/2
pos $body // 0
l!l
60
MC/DC0%0/1
pos $body // 0
pos $body
Truth table50%1/2
A
A= pos $body
Aresult
11
00
191 0 Carp::croak('not a PostgreSQL array literal');
192 }
193 6 return \@items;
194 }
195  
196 172 1;
197  
198 __END__
« blib/lib/DBIO/PostgreSQL/Diff/Trigger.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Diff/Sequence.pm »