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

filter: Statementstmt 98.8% 85 / 86 Branchbran 90.0% 54 / 60 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 95.6% 154 / 161 CC 36 SCAR 21.7
CC
36
Cov
95%
column_opclasses
11.1
_key_column_list
10.0
_split_top_level
10.0
CRAP 8.8
« blib/lib/DBIO/PostgreSQL/Util.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Diff/ForeignKey.pm »
1 package DBIO::PostgreSQL::Introspect::Parse;
2 # ABSTRACT: Pure parsers for raw PostgreSQL metadata strings
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 our $VERSION = '0.900007';
8  
9  
10  
11 4 ▶sub include_columns {
Subroutine include_columns: called CC=3 SCAR=11.0
Pod documented
12 4 my ($class, $definition) = @_;
13 4 ▶ return [] unless defined $definition;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $definition 1 3 4
14 3 ▶ if ($definition =~ /\bINCLUDE\s*\(([^)]+)\)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($definition =~ /\bINCLUDE\s*\(([^)]+)\)/i) 2 1 3
15 2 my $cols = $1;
16 2 return [ map { s/^\s+|\s+$//g; s/^"|"$//g; $_ } split /,/, $cols ];
17 }
18 1 return [];
19 }
20  
21  
22 5 ▶sub storage_params {
Subroutine storage_params: called CC=6 SCAR=17.9
Pod documented
23 5 my ($class, $reloptions) = @_;
24 5 ▶ return {} unless defined $reloptions;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reloptions 1 4 5
25 4 my $raw = $reloptions;
26 4 ▶ $raw =~ s/^\{|\}$//g if !ref $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $raw 3 1 4
27 4 ▶ my @items = ref $raw ? @$raw : split /,/, $raw;
Branch: 1 branch
BranchTrueFalseTotal
ref $raw ? : 1 3 4
28 4 my %params;
29 4 for my $item (@items) {
30 5 $item =~ s/^\s+|\s+$//g;
31 5 ▶ if ($item =~ /^(.+?)=(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /^(.+?)=(.+)$/) 5 0 5
32 5 $params{$1} = $2;
33 }
34 }
35 4 return \%params;
36 }
37  
38  
39 11 ▶sub column_opclasses {
Subroutine column_opclasses: called CC=11 SCAR=24.1
Pod documented
40 11 my ($class, $definition) = @_;
41 11 ▶ return {} unless defined $definition;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $definition 1 10 11
42  
43 10 my $list = _key_column_list($definition);
44 10 ▶ return {} unless defined $list;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list 1 9 10
45  
46 9 my %opclasses;
47 9 for my $item (_split_top_level($list)) {
48 11 $item =~ s/^\s+|\s+$//g;
49 11 ▶ next unless length $item;
Branch: 1 branch
BranchTrueFalseTotal
unless length $item 0 11 11
50 11 ▶ next if $item =~ /^\(/; # expression entry, not a plain column
Branch: 1 branch
BranchTrueFalseTotal
if $item =~ /^\(/ 0 11 11
51  
52 11 my ($column, $rest);
53 11 ▶ if ($item =~ /^"((?:[^"]|"")*)"\s*(.*)$/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^"((?:[^"]|"")*)"\s*(.*)$/s) { } 1 10 11
elsif ($item =~ /^([^\W\d][\w\$]*)\s*(.*)$/s) { } 10 0 10
54 1 ($column, $rest) = ($1, $2);
55 1 $column =~ s/""/"/g;
56 }
57 elsif ($item =~ /^([^\W\d][\w\$]*)\s*(.*)$/s) {
58 10 ($column, $rest) = ($1, $2);
59 }
60 else {
61 0 next;
62 }
63  
64 # A COLLATE clause sits between the column and its operator class.
65 11 $rest =~ s/^COLLATE\s+(?:"(?:[^"]|"")*"|[^\W\d][\w\$.]*)\s*//i;
66  
67 11 ▶ next unless $rest =~ /^([^\W\d][\w\$]*(?:\.[^\W\d][\w\$]*)?)\s*(\([^)]*\))?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ /^([^\W\d][\w\$]*(?:\.[^\W\d][\w\$]*)?)\s*(\([^)]*\))?/ 3 8 11
68 8 my ($name, $params) = ($1, $2);
69 8 ▶ next if $name =~ /^(?:ASC|DESC|NULLS)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(?:ASC|DESC|NULLS)\z/i 1 7 8
70  
71 7 ▶ $opclasses{$column} = $name . (defined $params ? $params : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $params ? : 1 6 7
72 }
73  
74 9 return \%opclasses;
75 }
76  
77 # The key-column list of an index definition: everything between the '('
78 # that follows "USING <access_method>" and its matching ')'. Returns undef
79 # when there is no such balanced list. Scanned character-wise rather than
80 # with a regex because expression entries nest parentheses and may contain
81 # commas -- "USING btree (lower((a || ','::text)))".
82 10 ▶sub _key_column_list {
Subroutine _key_column_list: called CC=10 SCAR=23.0
83 10 my ($definition) = @_;
84 return undef
85 10 ▶ unless $definition =~ /\bUSING\s+(?:"(?:[^"]|"")*"|[^\W\d][\w\$]*)\s*\(/gi;
Branch: 1 branch
BranchTrueFalseTotal
unless $definition =~ /\bUSING\s+(?:"(?:[^"]|"")*"|[^\W\d][\w\$]*)\s*\(/gi 1 9 10
86  
87 9 my $depth = 1;
88 9 my $in_quote = 0;
89 9 my $out = '';
90 9 for my $char (split //, substr($definition, pos $definition)) {
91 251 ▶ if ($in_quote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_quote) 13 238 251
92 13 $out .= $char;
93 13 ▶ $in_quote = 0 if $char eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\"" 2 11 13
94 13 next;
95 }
96 238 ▶ if ($char eq '"') { $in_quote = 1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq '"') { } 2 236 238
elsif ($char eq '(') { } 3 233 236
elsif ($char eq ')') { } 12 221 233
97 3 elsif ($char eq '(') { $depth++ }
98 elsif ($char eq ')') {
99 12 $depth--;
100 12 ▶ last if $depth == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 0 9 3 12
101 }
102 229 $out .= $char;
103 }
104  
105 9 ▶ return $depth == 0 ? $out : undef;
Branch: 1 branch
BranchTrueFalseTotal
$depth == 0 ? : 9 0 9
106 }
107  
108 # Split on the commas that separate index key entries, i.e. those outside
109 # any nested parentheses and outside a quoted identifier.
110 9 ▶sub _split_top_level {
Subroutine _split_top_level: called CC=10 SCAR=23.0
111 9 my ($text) = @_;
112 9 my @parts;
113 9 my $depth = 0;
114 9 my $in_quote = 0;
115 9 my $current = '';
116 9 for my $char (split //, $text) {
117 242 ▶ if ($in_quote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_quote) 13 229 242
118 13 $current .= $char;
119 13 ▶ $in_quote = 0 if $char eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\"" 2 11 13
120 13 next;
121 }
122 229 ▶ if ($char eq '"') { $in_quote = 1 }
Branch: 4 branches
BranchTrueFalseTotal
if ($char eq '"') { } 2 227 229
elsif ($char eq '(') { } 3 224 227
elsif ($char eq ')') { } 3 221 224
elsif ($char eq ',' and $depth == 0) { } 2 437 439
123 3 elsif ($char eq '(') { $depth++ }
124 3 elsif ($char eq ')') { $depth-- }
125 ▶ elsif ($char eq ',' && $depth == 0) {
Condition100%3/3
$char eq "," and $depth == 0
!ll&&!rl&&r
21812
MC/DC100%2/2
$char eq "," and $depth == 0
$char eq "," $depth == 0
Truth table100%3/3
A and B
A= $char eq ","B= $depth == 0
ABresult
0X0
100
111
126 2 push @parts, $current;
127 2 $current = '';
128 2 next;
129 }
130 227 $current .= $char;
131 }
132 9 ▶ push @parts, $current if length $current;
Branch: 1 branch
BranchTrueFalseTotal
if length $current 9 0 9
133 9 return @parts;
134 }
135  
136 172 1;
137  
138 __END__
« blib/lib/DBIO/PostgreSQL/Util.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Diff/ForeignKey.pm »