blib/lib/DBIO/PostgreSQL/InflateColumn/TSVector.pm

filter: Statementstmt 100% 62 / 62 Branchbran 75.0% 24 / 32 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 91.2% 104 / 114 CC 19 SCAR 13.7
CC
19
Cov
91%
_inflate_tsvector_value
7.1
_deflate_tsvector_hash
5.2
_deflate_tsvector_value
4.0
CRAP 3.9
« blib/lib/DBIO/PostgreSQL/Adapter.pm ↑ Summary blib/lib/DBIO/PostgreSQL/PgSchema.pm »
1 package DBIO::PostgreSQL::InflateColumn::TSVector;
2 # ABSTRACT: Auto-inflate/deflate PostgreSQL tsvector columns to/from Perl data structures
3  
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 28 our $VERSION = '0.900007';
8  
9 28 ▶use base 'DBIO::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 28 __PACKAGE__->load_components('InflateColumn');
14  
15  
16  
17 518 ▶sub register_column {
Subroutine register_column: called CC=4 SCAR=13.9
Pod documented
18 518 my ($class, $column, $info, @rest) = @_;
19  
20 518 $class->next::method($column, $info, @rest);
21  
22 518 ▶ return unless $class->_tsvector_type_info($info->{data_type});
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_tsvector_type_info($info->{"data_type"}) 513 5 518
23  
24 # this bailout is intentional
25 5 ▶ return if exists $info->{inflate_tsvector} && !$info->{inflate_tsvector};
Branch: 1 branch
BranchTrueFalseTotal
if exists $info->{"inflate_tsvector"} and not $info->{"inflate_tsvector"} 1 4 5
Condition66%2/3
exists $info->{"inflate_tsvector"} and not $info->{"inflate_tsvector"}
!ll&&!rl&&r
401
MC/DC50%1/2
exists $info->{"inflate_tsvector"} and not $info->{"inflate_tsvector"}
exists $info->{"inflate_tsvector"} not $info->{"inflate_tsvector"}
Truth table66%2/3
A and not(B)
A= exists $info->{"inflate_tsvector"}B= not $info->{"inflate_tsvector"}
ABresult
0X0
100
111
26  
27 $class->inflate_column(
28 $column => {
29 3 ▶ inflate => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 3 my ($value, $obj) = @_;
31 3 return $class->_inflate_tsvector_value($value);
32 },
33 4 ▶ deflate => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
34 4 my ($value, $obj) = @_;
35 4 return $class->_deflate_tsvector_value($value);
36 },
37 }
38 4 );
39  
40 4 return;
41 }
42  
43 # True for a tsvector data_type, false for anything else. Whitespace-tolerant,
44 # so ' tsvector ' is recognised (karr k61). tsvector takes no type modifier.
45 518 ▶sub _tsvector_type_info {
Subroutine _tsvector_type_info: called CC=3 SCAR=11.0
46 518 my ($class, $data_type) = @_;
47 518 ▶ return unless defined $data_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_type 0 518 518
48 518 ▶ return unless $data_type =~ /\A\s*tsvector\s*\z/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $data_type =~ /\A\s*tsvector\s*\z/i 513 5 518
49 5 return 'tsvector';
50 }
51  
52 3 ▶sub _inflate_tsvector_value {
Subroutine _inflate_tsvector_value: called CC=7 SCAR=19.6
53 3 my ($class, $value) = @_;
54  
55 3 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 3 3
56 3 ▶ return $value if ref $value; # already a structure — pass through
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 3 3
57  
58 # empty tsvector prints as '' — represent it as an empty lexeme map
59 3 ▶ return {} if $value =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A\s*\z/ 1 2 3
60  
61 2 my %lexemes;
62 2 ▶ while ($value =~ /\G\s*'((?:[^']|'')*)'(?::(\S+))?/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\G\s*'((?:[^']|'')*)'(?::(\S+))?/cg) 3 2 5
63 3 my ($lexeme, $positions) = ($1, $2);
64 3 $lexeme =~ s/''/'/g;
65 3 ▶ $lexemes{$lexeme} = defined $positions ? [ split /,/, $positions ] : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $positions ? : 3 0 3
66 }
67  
68 # if the whole string wasn't consumed it isn't a tsvector literal — pass
69 # it through untouched rather than returning a half-parsed structure
70 2 ▶ return $value unless $value =~ /\G\s*\z/gc;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\G\s*\z/cg 1 1 2
71  
72 1 return \%lexemes;
73 }
74  
75 6 ▶sub _deflate_tsvector_value {
Subroutine _deflate_tsvector_value: called CC=4 SCAR=13.9
76 6 my ($class, $value) = @_;
77  
78 # plain strings pass through verbatim — never double-serialize
79 6 ▶ return $value unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 6 6
80  
81 6 ▶ return $class->_deflate_tsvector_hash($value) if ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 4 2 6
82 2 ▶ return $class->_deflate_tsvector_array($value) if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 1 1 2
83  
84 1 Carp::croak('a tsvector column expects a hashref, an arrayref, or a tsvector string');
85 }
86  
87 4 ▶sub _deflate_tsvector_hash {
Subroutine _deflate_tsvector_hash: called CC=5 SCAR=16.6
88 4 my ($class, $value) = @_;
89  
90 4 my @parts;
91 4 for my $lexeme (sort keys %$value) {
92 10 my $positions = $value->{$lexeme};
93 10 ▶ my @pos = ref $positions eq 'ARRAY' ? @$positions
Branch: 2 branches
BranchTrueFalseTotal
ref $positions eq 'ARRAY' ? : 10 0 10
defined $positions ? : 0 0 0
94 : defined $positions ? ($positions)
95 : ();
96 10 my $quoted = $class->_quote_lexeme($lexeme);
97 10 ▶ push @parts, @pos ? "$quoted:" . join(',', @pos) : $quoted;
Branch: 1 branch
BranchTrueFalseTotal
@pos ? : 7 3 10
98 }
99  
100 4 return join(' ', @parts);
101 }
102  
103 1 ▶sub _deflate_tsvector_array {
Subroutine _deflate_tsvector_array: called CC=1 SCAR=0.0
104 1 my ($class, $value) = @_;
105  
106 1 return join(' ', map { $class->_quote_lexeme($_) } @$value);
107 }
108  
109 12 ▶sub _quote_lexeme {
Subroutine _quote_lexeme: called CC=1 SCAR=0.0
110 12 my ($class, $lexeme) = @_;
111 12 $lexeme =~ s/'/''/g;
112 12 return "'$lexeme'";
113 }
114  
115  
116 28 1;
117  
118 __END__
« blib/lib/DBIO/PostgreSQL/Adapter.pm ↑ Summary blib/lib/DBIO/PostgreSQL/PgSchema.pm »