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 8     8   45 use Moo;
  8         12  
  8         63  
3             extends 'Search::Query::Dialect';
4 8     8   2748 use Carp;
  8         13  
  8         810  
5 8     8   46 use Data::Dump qw( dump );
  8         14  
  8         4811  
6              
7             our $VERSION = '0.305';
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     59 1 94 my $self = shift;
36 59   66     216 my $tree = shift || $self;
37 59   100     172 my $no_prefix = shift || 0;
38              
39 59         55 my @q;
40 59         85 foreach my $prefix ( '+', '', '-' ) {
41 177 100       345 next unless exists $tree->{$prefix};
42 62         55 for my $clause ( @{ $tree->{$prefix} } ) {
  62         108  
43 97 100       242 push @q,
44             ( $no_prefix ? '' : $prefix )
45             . $self->stringify_clause($clause);
46             }
47             }
48              
49 59         536 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 97     97 1 87 my $self = shift;
60 97         87 my $clause = shift;
61              
62 97 100       213 if ( $clause->{op} eq '()' ) {
63 20 100 66     54 if ( $clause->has_children and $clause->has_children == 1 ) {
64 4         11 return $self->stringify( $clause->{value}, 1 );
65             }
66             else {
67 16         42 return "(" . $self->stringify( $clause->{value} ) . ")";
68             }
69             }
70              
71 77   100     261 my $quote = $clause->quote || "";
72 77         107 my $value = $clause->value;
73 77   100     225 my $proximity = $clause->proximity || '';
74 77 100       120 if ($proximity) {
75 3         4 $proximity = '~' . $proximity;
76             }
77              
78             # ranges
79 77 100 100     276 if ( ref $value eq 'ARRAY' ) {
    100          
80 3 100 66     16 if ( $value->[0] =~ m/[a-z]/i or $value->[1] =~ m/[a-z]/i ) {
81 1         4 $value = join( qq/$quote$clause->{op}$quote/,
82             ( $value->[0], $value->[1] ) );
83             }
84             else {
85 2         14 $value = join( qq/$quote $quote/, $value->[0] .. $value->[1] );
86             }
87 3 100       11 if ( $clause->{op} eq '!..' ) {
    50          
88 1 50       6 return join( '',
89             ( defined $clause->{field} ? $clause->{field} : "" ),
90             '!=', '(', $quote, $value, $quote, ')' );
91             }
92             elsif ( $clause->{op} eq '..' ) {
93 2 50       12 return join( '',
94             ( defined $clause->{field} ? $clause->{field} : "" ),
95             '=', '(', $quote, $value, $quote, ')' );
96             }
97             }
98              
99             # NULL query
100             elsif ( defined $clause->{field} and !defined $value ) {
101 2 100       14 return sprintf( "%s %s NULL",
102             $clause->{field}, ( $clause->{op} eq '=' ? 'is' : 'is not' ) );
103             }
104             else {
105 72 100       390 return join( '',
106             ( defined $clause->{field} ? $clause->{field} : "" ),
107             $clause->{op}, $quote, $value, $quote, $proximity );
108             }
109             }
110              
111             1;
112              
113             __END__