File Coverage

blib/lib/Search/Query/Dialect/Native.pm
Criterion Covered Total %
statement 39 39 100.0
branch 25 28 89.2
condition 15 18 83.3
subroutine 5 5 100.0
pod 2 2 100.0
total 86 92 93.4


line stmt bran cond sub pod time code
1             package Search::Query::Dialect::Native;
2 9     9   40 use Moo;
  9         15  
  9         46  
3             extends 'Search::Query::Dialect';
4 9     9   2383 use Carp;
  9         16  
  9         603  
5 9     9   43 use Data::Dump qw( dump );
  9         61  
  9         5026  
6              
7             our $VERSION = '0.307';
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 60     60 1 77 my $self = shift;
36 60   66     202 my $tree = shift || $self;
37 60   100     179 my $no_prefix = shift || 0;
38              
39 60         59 my @q;
40 60         86 foreach my $prefix ( '+', '', '-' ) {
41 180 100       340 next unless exists $tree->{$prefix};
42 63         58 for my $clause ( @{ $tree->{$prefix} } ) {
  63         143  
43 99 100       272 push @q,
44             ( $no_prefix ? '' : $prefix )
45             . $self->stringify_clause($clause);
46             }
47             }
48              
49 60         468 return join " ", @q;
50             }
51              
52             =head2 stringify_clause( I )
53              
54             Called by stringify() to handle each Clause in the Query tree.
55              
56             =cut
57              
58             sub stringify_clause {
59 99     99 1 93 my $self = shift;
60 99         89 my $clause = shift;
61              
62 99 100       196 if ( $clause->{op} eq '()' ) {
63 20 100 66     53 if ( $clause->has_children and $clause->has_children == 1 ) {
64 4         8 return $self->stringify( $clause->{value}, 1 );
65             }
66             else {
67 16         41 return "(" . $self->stringify( $clause->{value} ) . ")";
68             }
69             }
70              
71 79   100     281 my $quote = $clause->quote || "";
72 79         109 my $value = $clause->value;
73 79   100     224 my $proximity = $clause->proximity || '';
74 79 100       121 if ($proximity) {
75 3         7 $proximity = '~' . $proximity;
76             }
77              
78             # ranges
79 79 100 100     277 if ( ref $value eq 'ARRAY' ) {
    100          
80 3 100 66     19 if ( $value->[0] =~ m/[a-z]/i or $value->[1] =~ m/[a-z]/i ) {
81 1         5 $value = join( qq/$quote$clause->{op}$quote/,
82             ( $value->[0], $value->[1] ) );
83             }
84             else {
85 2         16 $value = join( qq/$quote $quote/, $value->[0] .. $value->[1] );
86             }
87 3 100       13 if ( $clause->{op} eq '!..' ) {
    50          
88             return join( '',
89 1 50       7 ( defined $clause->{field} ? $clause->{field} : "" ),
90             '!=', '(', $quote, $value, $quote, ')' );
91             }
92             elsif ( $clause->{op} eq '..' ) {
93             return join( '',
94 2 50       13 ( defined $clause->{field} ? $clause->{field} : "" ),
95             '=', '(', $quote, $value, $quote, ')' );
96             }
97             }
98              
99             # NULL query
100             elsif ( defined $clause->{field} and !defined $value ) {
101             return sprintf( "%s %s NULL",
102 2 100       15 $clause->{field}, ( $clause->{op} eq '=' ? 'is' : 'is not' ) );
103             }
104             else {
105             return join( '',
106             ( defined $clause->{field} ? $clause->{field} : "" ),
107 74 100       332 $clause->{op}, $quote, $value, $quote, $proximity );
108             }
109             }
110              
111             1;
112              
113             __END__