Query/Dialect/Native.pm

filter: Statementstmt 100% 42 / 42 Branchbran 89.2% 25 / 28 Conditioncond 83.3% 15 / 18 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 90.5% 96 / 106 CC 24 SCAR 26.1
CC
24
Cov
93%
stringify_clause
18.2
stringify
7.0
BEGIN
1.0
CRAP 13.6
« Query/Dialect/SQL.pm ↑ Summary Query/Clause.pm »
1 package Search::Query::Dialect::Native;
2 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 extends 'Search::Query::Dialect';
4 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 our $VERSION = '0.306';
8  
9 =head1 NAME
10  
11 Search::Query::Dialect::Native - the default query dialect
12  
13 =head1 SYNOPSIS
14  
15 my $query = Search::Query->parser->parse('foo');
16 print $query;
17  
18 =head1 DESCRIPTION
19  
20 Search::Query::Dialect::Native is the default query dialect for Query
21 objects returned by a Search::Query::Parser instance.
22  
23 =head1 METHODS
24  
25 This class is a subclass of Search::Query::Dialect. Only new or overridden
26 methods are documented here.
27  
28 =head2 stringify
29  
30 Returns the Query object as a normalized string.
31  
32 =cut
33  
34 sub stringify {
35 59 ▶ my $self = shift;
Subroutine stringify: called CC=7 SCAR=19.5
Pod documented
36 59 ▶ my $tree = shift || $self;
Condition66%2/3
shift() || $self
l!l&&r!l&&!r
20390
MC/DC0%0/2
shift() || $self
shift() $self
Truth table66%2/3
A || B
A= shift()B= $self
ABresult
1X1
011
000
37 59 ▶ my $no_prefix = shift || 0;
Condition100%2/2
shift() || 0
l!l
455
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
38  
39 59 my @q;
40 59 foreach my $prefix ( '+', '', '-' ) {
41 177 ▶ next unless exists $tree->{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tree->{$prefix} 115 62 177
42 62 for my $clause ( @{ $tree->{$prefix} } ) {
43 97 ▶ push @q,
Branch: 1 branch
BranchTrueFalseTotal
$no_prefix ? : 4 93 97
44 ( $no_prefix ? '' : $prefix )
45 . $self->stringify_clause($clause);
46 }
47 }
48  
49 59 return join " ", @q;
50 }
51  
52 =head2 stringify_clause( I<leaf> )
53  
54 Called by stringify() to handle each Clause in the Query tree.
55  
56 =cut
57  
58 sub stringify_clause {
59 97 ▶ my $self = shift;
Subroutine stringify_clause: called CC=18 SCAR=29.0
Pod documented
60 97 my $clause = shift;
61  
62 97 ▶ if ( $clause->{op} eq '()' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause->{"op"} eq "()") 20 77 97
63 20 ▶ if ( $clause->has_children and $clause->has_children == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause->has_children and $clause->has_children == 1) { } 4 16 20
Condition66%2/3
$clause->has_children and $clause->has_children == 1
!ll&&!rl&&r
0164
MC/DC50%1/2
$clause->has_children and $clause->has_children == 1
$clause->has_children $clause->has_children == 1
Truth table66%2/3
A and B
A= $clause->has_childrenB= $clause->has_children == 1
ABresult
0X0
100
111
64 4 return $self->stringify( $clause->{value}, 1 );
65 }
66 else {
67 16 return "(" . $self->stringify( $clause->{value} ) . ")";
68 }
69 }
70  
71 77 ▶ my $quote = $clause->quote || "";
Condition100%2/2
$clause->quote || ''
l!l
968
MC/DC100%1/1
$clause->quote || ''
$clause->quote
Truth table100%2/2
A
A= $clause->quote
Aresult
11
00
72 77 my $value = $clause->value;
73 77 ▶ my $proximity = $clause->proximity || '';
Condition100%2/2
$clause->proximity || ''
l!l
374
MC/DC100%1/1
$clause->proximity || ''
$clause->proximity
Truth table100%2/2
A
A= $clause->proximity
Aresult
11
00
74 77 ▶ if ($proximity) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proximity) 3 74 77
75 3 $proximity = '~' . $proximity;
76 }
77  
78 # ranges
79 77 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 3 74 77
elsif (defined $clause->{'field'} and not defined $value) { } 2 72 74
80 3 ▶ if ( $value->[0] =~ m/[a-z]/i or $value->[1] =~ m/[a-z]/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->[0] =~ /[a-z]/i or $value->[1] =~ /[a-z]/i) { } 1 2 3
Condition66%2/3
$value->[0] =~ /[a-z]/i or $value->[1] =~ /[a-z]/i
l!l&&r!l&&!r
102
MC/DC50%1/2
$value->[0] =~ /[a-z]/i or $value->[1] =~ /[a-z]/i
$value->[0] =~ /[a-z]/i $value->[1] =~ /[a-z]/i
Truth table66%2/3
A or B
A= $value->[0] =~ /[a-z]/iB= $value->[1] =~ /[a-z]/i
ABresult
1X1
011
000
81 1 $value = join( qq/$quote$clause->{op}$quote/,
82 ( $value->[0], $value->[1] ) );
83 }
84 else {
85 2 $value = join( qq/$quote $quote/, $value->[0] .. $value->[1] );
86 }
87 3 ▶ if ( $clause->{op} eq '!..' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($clause->{'op'} eq '!..') { } 1 2 3
elsif ($clause->{'op'} eq '..') { } 2 0 2
88 return join( '',
89 1 ▶ ( defined $clause->{field} ? $clause->{field} : "" ),
Branch: 1 branch
BranchTrueFalseTotal
defined $clause->{'field'} ? : 1 0 1
90 '!=', '(', $quote, $value, $quote, ')' );
91 }
92 elsif ( $clause->{op} eq '..' ) {
93 return join( '',
94 2 ▶ ( defined $clause->{field} ? $clause->{field} : "" ),
Branch: 1 branch
BranchTrueFalseTotal
defined $clause->{'field'} ? : 2 0 2
95 '=', '(', $quote, $value, $quote, ')' );
96 }
97 }
98  
99 # NULL query
100 ▶ elsif ( defined $clause->{field} and !defined $value ) {
Condition100%3/3
defined $clause->{"field"} and not defined $value
!ll&&!rl&&r
25472
MC/DC100%2/2
defined $clause->{"field"} and not defined $value
defined $clause->{"field"} not defined $value
Truth table100%3/3
A and not(B)
A= defined $clause->{"field"}B= not defined $value
ABresult
0X0
100
111
101 return sprintf( "%s %s NULL",
102 2 ▶ $clause->{field}, ( $clause->{op} eq '=' ? 'is' : 'is not' ) );
Branch: 1 branch
BranchTrueFalseTotal
$clause->{'op'} eq '=' ? : 1 1 2
103 }
104 else {
105 return join( '',
106 ( defined $clause->{field} ? $clause->{field} : "" ),
107 72 ▶ $clause->{op}, $quote, $value, $quote, $proximity );
Branch: 1 branch
BranchTrueFalseTotal
defined $clause->{'field'} ? : 47 25 72
108 }
109 }
110  
111 9 1;
112  
113 __END__
« Query/Dialect/SQL.pm ↑ Summary Query/Clause.pm »