Query/Dialect/SQL.pm

filter: Statementstmt 93.0% 121 / 130 Branchbran 81.9% 59 / 72 Conditioncond 64.5% 20 / 31 MC/DCmcdc 31.5% 6 / 19 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 83.0% 226 / 272 CC 51 SCAR 31.1
CC
51
Cov
86%
stringify_clause
40.5
stringify
7.1
BUILD
6.3
CRAP 22.4
« Query/Dialect/SWISH.pm ↑ Summary Query/Dialect/Native.pm »
1 package Search::Query::Dialect::SQL;
2 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 extends 'Search::Query::Dialect';
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Search::Query::Field::SQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 43 ▶has 'wildcard' => ( is => 'rw', default => sub {'%'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
11 40 ▶has 'quote_fields' => ( is => 'rw', default => sub {''} );
Subroutine __ANON__: called CC=1 SCAR=0.0
12 3 has 'fuzzify' => ( is => 'rw' );
13 3 has 'fuzzify2' => ( is => 'rw' );
14 35 ▶has 'like' => ( is => 'rw', default => sub {'ILIKE'}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
15 43 ▶has 'quote_char' => ( is => 'rw', default => sub {q/'/}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
16 43 ▶has 'fuzzy_space' => ( is => 'rw', default => sub {' '}, );
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 3 our $VERSION = '0.306';
19  
20 =head1 NAME
21  
22 Search::Query::Dialect::SQL - SQL query dialect
23  
24 =head1 SYNOPSIS
25  
26 my $query = Search::Query->parser( dialect => 'SQL' )->parse('foo');
27 print $query;
28  
29 =head1 DESCRIPTION
30  
31 Search::Query::Dialect::SQL is a query dialect for Query
32 objects returned by a Search::Query::Parser instance.
33  
34 The SQL dialect class stringifies queries to work as SQL WHERE
35 clauses. This behavior is similar to Search::QueryParser::SQL.
36  
37 =head1 METHODS
38  
39 This class is a subclass of Search::Query::Dialect. Only new or overridden
40 methods are documented here.
41  
42 =cut
43  
44 =head2 BUILD
45  
46 Called by new(). The new() constructor can accept the following params, which
47 are also standard attribute accessors:
48  
49 =over
50  
51 =item wildcard
52  
53 Default value is C<%>.
54  
55 =item quote_fields
56  
57 Default value is "". Set to (for example) C<`> to quote each field name
58 in stringify() as some SQL variants require that syntax (e.g. mysql).
59  
60 =item default_field
61  
62 Override the default field set in Search::Query::Parser.
63  
64 =item fuzzify
65  
66 Append wildcard() to all terms.
67  
68 =item fuzzify2
69  
70 Prepend and append wildcard() to all terms.
71  
72 =item like
73  
74 The SQL reserved word for wildcard comparison. Default value is C<ILIKE>.
75  
76 =item quote_char
77  
78 The string to use for quoting strings. Default is C<'>.
79  
80 =item fuzzy_space
81  
82 The string to use to pad fuzzified terms. Default is a single space C< >.
83  
84 =back
85  
86 =cut
87  
88 sub BUILD {
89 43 ▶ my $self = shift;
Subroutine BUILD: called CC=6 SCAR=18.4
Pod documented
90  
91 #carp dump $self;
92 43 ▶ if ( !defined $self->parser->fields ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->parser->fields) 0 43 43
93 0 croak "You must set fields in the Search::Query::Parser";
94 }
95 $self->{default_field} ||= $self->parser->default_field
96 43 ▶ || [ sort keys %{ $self->parser->fields } ];
Condition80%4/5
$self->{"default_field"} ||= $self->parser->default_field || [sort(keys %{$self->parser->fields;})]
l!l&&r!l&&!r
2410
$self->parser->default_field || [sort(keys %{$self->parser->fields;})]
l!l
2219
MC/DC0%0/2
$self->{"default_field"} ||= $self->parser->default_field || [sort(keys %{$self->parser->fields;})]
$self->{"default_field"} $self->parser->default_field
Truth table66%2/3
A ||= B
A= $self->{"default_field"}B= $self->parser->default_field
ABresult
1X1
011
000
97 43 ▶ if ( $self->{default_field} and !ref( $self->{default_field} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"default_field"} and not ref $self->{"default_field"}) 22 21 43
Condition66%2/3
$self->{"default_field"} and not ref $self->{"default_field"}
!ll&&!rl&&r
02122
MC/DC50%1/2
$self->{"default_field"} and not ref $self->{"default_field"}
$self->{"default_field"} not ref $self->{"default_field"}
Truth table66%2/3
A and not(B)
A= $self->{"default_field"}B= not ref $self->{"default_field"}
ABresult
0X0
100
111
98 22 $self->{default_field} = [ $self->{default_field} ];
99 }
100 43 return $self;
101 }
102  
103 =head2 stringify
104  
105 Returns the Query object as a normalized string.
106  
107 =cut
108  
109 3 my %op_map = (
110 '+' => 'AND',
111 '' => 'OR',
112 '-' => 'AND', # operator is munged
113 );
114  
115 sub stringify {
116 34 ▶ my $self = shift;
Subroutine stringify: called CC=7 SCAR=19.6
Pod documented
117 34 ▶ my $tree = shift || $self;
Condition66%2/3
shift() || $self
l!l&&r!l&&!r
5290
MC/DC0%0/2
shift() || $self
shift() $self
Truth table66%2/3
A || B
A= shift()B= $self
ABresult
1X1
011
000
118  
119 34 my @q;
120 34 foreach my $prefix ( '+', '', '-' ) {
121 102 my @clauses;
122 102 my $joiner = $op_map{$prefix};
123 102 ▶ next unless exists $tree->{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tree->{$prefix} 66 36 102
124 36 for my $clause ( @{ $tree->{$prefix} } ) {
125 46 push( @clauses, $self->stringify_clause( $clause, $prefix ) );
126 }
127 36 ▶ next if !@clauses;
Branch: 1 branch
BranchTrueFalseTotal
unless @clauses 0 36 36
128  
129 36 ▶ push @q, join( " $joiner ", grep { defined and length } @clauses );
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 46 0 46
130 }
131  
132 34 return join " AND ", @q;
133 }
134  
135 sub _doctor_value {
136 41 ▶ my ( $self, $clause ) = @_;
Subroutine _doctor_value: called CC=6 SCAR=17.9
137  
138 41 my $value = $clause->{value};
139  
140 41 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 39 41
141  
142 39 ▶ if ( $self->fuzzify ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->fuzzify) { } 6 33 39
elsif ($self->fuzzify2) { } 2 31 33
143 6 ▶ $value .= '*' unless $value =~ m/[\*\%]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /[\*\%]/ 4 2 6
144 }
145 elsif ( $self->fuzzify2 ) {
146 2 ▶ $value = "*$value*" unless $value =~ m/[\*\%]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /[\*\%]/ 2 0 2
147 }
148  
149 # normalize wildcard
150 39 my $wildcard = $self->wildcard;
151 39 $value =~ s/\*/$wildcard/g;
152  
153 39 return $value;
154 }
155  
156 =head2 stringify_clause( I<leaf>, I<prefix> )
157  
158 Called by stringify() to handle each Clause in the Query tree.
159  
160 =cut
161  
162 sub stringify_clause {
163 46 ▶ my $self = shift;
Subroutine stringify_clause: called CC=33 SCAR=37.0
Pod documented
164 46 my $clause = shift;
165 46 my $prefix = shift;
166  
167 46 ▶ if ( $clause->{op} eq '()' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause->{"op"} eq "()") 5 41 46
168 5 ▶ if ( $clause->has_children and $clause->has_children == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause->has_children and $clause->has_children == 1) { } 2 3 5
Condition66%2/3
$clause->has_children and $clause->has_children == 1
!ll&&!rl&&r
032
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
169  
170 # muck about in the internals because SQL relies on the operator,
171 # not the prefix, to indicate the "NOT"-ness of a clause.
172 2 ▶ if ( $prefix eq '-' and exists $clause->{value}->{'+'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix eq "-" and exists $clause->{"value"}{"+"}) 0 2 2
Condition33%1/3
$prefix eq "-" and exists $clause->{"value"}{"+"}
!ll&&!rl&&r
200
MC/DC0%0/2
$prefix eq "-" and exists $clause->{"value"}{"+"}
$prefix eq "-" exists $clause->{"value"}{"+"}
Truth table33%1/3
A and B
A= $prefix eq "-"B= exists $clause->{"value"}{"+"}
ABresult
0X0
100
111
173 0 $clause->{value}->{'-'} = delete $clause->{value}->{'+'};
174 }
175 2 return '(' . $self->stringify( $clause->{value} ) . ')';
176 }
177 else {
178 return
179 ( $prefix eq '-' ? 'NOT ' : '' ) . "("
180 3 ▶ . $self->stringify( $clause->{value} ) . ")";
Branch: 1 branch
BranchTrueFalseTotal
$prefix eq '-' ? : 0 3 3
181 }
182 }
183  
184 # optional
185 41 my $quote_fields = $self->quote_fields;
186 41 my $fuzzy_space = $self->fuzzy_space;
187  
188 # TODO proximity - anything special and SQL-specific?
189  
190 # make sure we have a field
191 my @fields
192 = $clause->{field}
193 ? ( $clause->{field} )
194 41 ▶ : ( @{ $self->get_default_field } );
Branch: 1 branch
BranchTrueFalseTotal
$clause->{'field'} ? : 33 8 41
195  
196 # what value
197 41 my $value = $self->_doctor_value($clause);
198  
199 # normalize operator
200 41 ▶ my $op = $clause->{op} || "=";
Condition100%2/2
$clause->{'op'} || '='
l!l
338
MC/DC100%1/1
$clause->{'op'} || '='
$clause->{'op'}
Truth table100%2/2
A
A= $clause->{'op'}
Aresult
11
00
201 41 ▶ if ( $op eq ':' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq ":") 18 23 41
202 18 $op = '=';
203 }
204 41 ▶ if ( $prefix eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix eq "-") 4 37 41
205 4 $op = '!' . $op;
206 }
207 41 ▶ if ( defined $value and $value =~ m/\%/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value =~ /\%/) 8 33 41
Condition100%3/3
defined $value and $value =~ /\%/
!ll&&!rl&&r
2318
MC/DC100%2/2
defined $value and $value =~ /\%/
defined $value $value =~ /\%/
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /\%/
ABresult
0X0
100
111
208 8 ▶ $op = $prefix eq '-' ? '!~' : '~';
Branch: 1 branch
BranchTrueFalseTotal
$prefix eq '-' ? : 0 8 8
209 }
210  
211 41 my @buf;
212 41 NAME: for my $name (@fields) {
213 55 my $field = $self->get_field($name);
214 55 ▶ $value =~ s/\%//g if $field->is_int;
Branch: 1 branch
BranchTrueFalseTotal
if $field->is_int 8 47 55
215 55 my $this_op;
216  
217 # whether we quote depends on the field (column) type
218 55 ▶ my $quote = $field->is_int ? "" : $self->quote_char;
Branch: 1 branch
BranchTrueFalseTotal
$field->is_int ? : 8 47 55
219  
220 #warn dump [ $prefix, $field, $value, $op, $quote ];
221  
222 # range
223 55 ▶ if ( $op eq '..' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '..') { } 2 53 55
elsif ($op eq '!..') { } 2 51 53
elsif ($op =~ /\~/) { } 8 43 51
elsif (not defined $value) { } 2 41 43
224 2 ▶ if ( ref $value ne 'ARRAY' or @$value != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "ARRAY" or @$value != 2) 0 2 2
Condition33%1/3
ref $value ne "ARRAY" or @$value != 2
l!l&&r!l&&!r
002
MC/DC0%0/2
ref $value ne "ARRAY" or @$value != 2
ref $value ne "ARRAY" @$value != 2
Truth table33%1/3
A or B
A= ref $value ne "ARRAY"B= @$value != 2
ABresult
1X1
011
000
225 0 croak "range of values must be a 2-element ARRAY";
226 }
227  
228 2 my @range = ( $value->[0] .. $value->[1] );
229 push(
230 @buf,
231 join( '',
232 $quote_fields, $name, $quote_fields, ' IN ', '(',
233 2 join( ', ', map { $quote . $_ . $quote } @range ), ')' )
234 );
235 2 next NAME;
236  
237 }
238  
239 # invert range
240 elsif ( $op eq '!..' ) {
241 2 ▶ if ( ref $value ne 'ARRAY' or @$value != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "ARRAY" or @$value != 2) 0 2 2
Condition33%1/3
ref $value ne "ARRAY" or @$value != 2
l!l&&r!l&&!r
002
MC/DC0%0/2
ref $value ne "ARRAY" or @$value != 2
ref $value ne "ARRAY" @$value != 2
Truth table33%1/3
A or B
A= ref $value ne "ARRAY"B= @$value != 2
ABresult
1X1
011
000
242 0 croak "range of values must be a 2-element ARRAY";
243 }
244  
245 2 my @range = ( $value->[0] .. $value->[1] );
246 push(
247 @buf,
248 join( '',
249 $quote_fields, $name, $quote_fields, ' NOT IN ', '( ',
250 2 join( ', ', map { $quote . $_ . $quote } @range ), ' )' )
251 );
252 2 next NAME;
253 }
254  
255 # fuzzy
256 elsif ( $op =~ m/\~/ ) {
257  
258 # negation
259 8 ▶ if ( $op eq '!~' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '!~') { } 0 8 8
260 0 ▶ if ( $field->is_int ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->is_int) { } 0 0 0
261 0 $this_op = $field->fuzzy_not_op;
262 }
263 else {
264 0 $this_op
265 = $fuzzy_space . $field->fuzzy_not_op . $fuzzy_space;
266 }
267 }
268  
269 # standard fuzzy
270 else {
271 8 ▶ if ( $field->is_int ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->is_int) { } 4 4 8
272 4 $this_op = $field->fuzzy_op;
273 }
274 else {
275 4 $this_op = $fuzzy_space . $field->fuzzy_op . $fuzzy_space;
276 }
277 }
278 }
279  
280 # null
281 elsif ( !defined $value ) {
282 2 ▶ if ( $op eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '=') { } 1 1 2
283 1 $this_op = ' is ';
284 }
285 else {
286 1 $this_op = ' is not ';
287 }
288 2 $value = 'NULL';
289 2 $quote = '';
290 }
291  
292 # default, pass through
293 else {
294 41 $this_op = $op;
295 }
296  
297 51 ▶ if ( defined $field->callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->callback) 0 51 51
298 0 push( @buf, $field->callback->( $field, $this_op, $value ) );
299 0 next NAME;
300 }
301  
302 #warn dump [ $quote_fields, $name, $this_op, $quote, $value ];
303  
304 push(
305 51 @buf,
306 join( '',
307 $quote_fields, $name, $quote_fields, $this_op,
308 $quote, $value, $quote )
309 );
310  
311 }
312 41 ▶ my $joiner = $prefix eq '-' ? ' AND ' : ' OR ';
Branch: 1 branch
BranchTrueFalseTotal
$prefix eq '-' ? : 4 37 41
313 return
314 41 ▶ ( scalar(@buf) > 1 ? '(' : '' )
Branch: 2 branches
BranchTrueFalseTotal
scalar @buf > 1 ? : 8 33 41
scalar @buf > 1 ? : 8 33 41
315 . join( $joiner, @buf )
316 . ( scalar(@buf) > 1 ? ')' : '' );
317 }
318  
319 =head2 get_field
320  
321 Overrides default to set fuzzy_op and fuzzy_not_op.
322  
323 =cut
324  
325 around get_field => sub {
326 55 ▶ my $orig = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
327 55 my $self = shift;
328 55 my $field = $orig->( $self, @_ );
329  
330 # fix up the operator based on our like() setting
331 55 ▶ if ( !$field->is_int and $self->like ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $field->is_int and $self->like) 47 8 55
Condition66%2/3
not $field->is_int and $self->like
!ll&&!rl&&r
8047
MC/DC50%1/2
not $field->is_int and $self->like
not $field->is_int $self->like
Truth table66%2/3
not(A) and B
A= not $field->is_intB= $self->like
ABresult
0X0
100
111
332 47 $field->fuzzy_op( $self->like );
333 47 $field->fuzzy_not_op( 'NOT ' . $self->like );
334 }
335  
336 55 return $field;
337 3 };
338  
339 =head2 field_class
340  
341 Returns "Search::Query::Field::SQL".
342  
343 =cut
344  
345 16 ▶sub field_class {'Search::Query::Field::SQL'}
Subroutine field_class: called CC=1 SCAR=0.0
Pod documented
346  
347 3 1;
348  
349 __END__
« Query/Dialect/SWISH.pm ↑ Summary Query/Dialect/Native.pm »