| 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
MC/DC0%0/2
shift() || $self
shift()
$self
Truth table66%2/3
A || B
A= shift()B= $self
| |||||||||||||||||||||||||||||
| 37 | 59 | ▶ | my $no_prefix = shift || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 59 | my @q; | |||||||||||||||||||||||||||
| 40 | 59 | foreach my $prefix ( '+', '', '-' ) { | |||||||||||||||||||||||||||
| 41 | 177 | ▶ | next unless exists $tree->{$prefix}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 62 | for my $clause ( @{ $tree->{$prefix} } ) { | |||||||||||||||||||||||||||
| 43 | 97 | ▶ | push @q, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 63 | 20 | ▶ | if ( $clause->has_children and $clause->has_children == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$clause->has_children and $clause->has_children == 1
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
| |||||||||||||||||||||||||||||
| 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 || ''
MC/DC100%1/1
$clause->quote || ''
$clause->quote
Truth table100%2/2
A
A= $clause->quote
| |||||||||||||||||||||||||||||
| 72 | 77 | my $value = $clause->value; | |||||||||||||||||||||||||||
| 73 | 77 | ▶ | my $proximity = $clause->proximity || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$clause->proximity || ''
MC/DC100%1/1
$clause->proximity || ''
$clause->proximity
Truth table100%2/2
A
A= $clause->proximity
| |||||||||||||||||||||||||||||
| 74 | 77 | ▶ | if ($proximity) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 3 | $proximity = '~' . $proximity; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # ranges | ||||||||||||||||||||||||||||
| 79 | 77 | ▶ | if ( ref $value eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | if ( $value->[0] =~ m/[a-z]/i or $value->[1] =~ m/[a-z]/i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$value->[0] =~ /[a-z]/i or $value->[1] =~ /[a-z]/i
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 88 | return join( '', | ||||||||||||||||||||||||||||
| 89 | 1 | ▶ | ( defined $clause->{field} ? $clause->{field} : "" ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | '!=', '(', $quote, $value, $quote, ')' ); | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | elsif ( $clause->{op} eq '..' ) { | ||||||||||||||||||||||||||||
| 93 | return join( '', | ||||||||||||||||||||||||||||
| 94 | 2 | ▶ | ( defined $clause->{field} ? $clause->{field} : "" ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 101 | return sprintf( "%s %s NULL", | ||||||||||||||||||||||||||||
| 102 | 2 | ▶ | $clause->{field}, ( $clause->{op} eq '=' ? 'is' : 'is not' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||
| 105 | return join( '', | ||||||||||||||||||||||||||||
| 106 | ( defined $clause->{field} ? $clause->{field} : "" ), | ||||||||||||||||||||||||||||
| 107 | 72 | ▶ | $clause->{op}, $quote, $value, $quote, $proximity ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 9 | 1; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | __END__ | ||||||||||||||||||||||||||||