Query/Dialect.pm

filter: Statementstmt 88.2% 113 / 128 Branchbran 54.7% 23 / 42 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 89.6% 26 / 29 Podpod 100% 13 / 13 totaltotal 76.6% 180 / 235 CC 31 SCAR 15.1
CC
31
Cov
76%
walk
10.4
tree
8.2
get_field
6.4
CRAP 4.5
« Query/Field/SQL.pm ↑ Summary Query.pm »
1 package Search::Query::Dialect;
2 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use overload
6 114 ▶ '""' => sub { $_[0]->stringify; },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
7 211 ▶ 'bool' => sub {1},
Subroutine __ANON__: called CC=1 SCAR=0.0
8 10 fallback => 1;
9  
10 10 ▶use Data::Transformer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Types::Standard qw( Int Undef );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Type::Utils qw( declare as where coerce inline_as from );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 my $PositiveInt = declare
17 as Int,
18 0 ▶ where { defined($_) and $_ >= 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ >= 0 0 0 0
19 21 ▶ inline_as {"defined($_) and $_ =~ /^[0-9]\$/ and $_ >= 0"};
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 10 coerce $PositiveInt, from Int, q{ abs( $_ || $ENV{PERL_DEBUG} || 0) },
22 from Undef, q{ 0 };
23  
24 10 has default_field => ( is => 'rw' );
25 10 has parser => ( is => 'ro' );
26 10 has debug => (
27 is => 'rw',
28 isa => $PositiveInt,
29 lazy => 1,
30 coerce => $PositiveInt->coercion,
31 builder => '_init_debug',
32 );
33  
34 1 ▶sub _init_debug { $ENV{PERL_DEBUG} || 0 }
Subroutine _init_debug: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"PERL_DEBUG"} or 0 0 1 1
35  
36 10 our $VERSION = '0.307';
37  
38 =head1 NAME
39  
40 Search::Query::Dialect - abstract base class for query language dialects
41  
42 =head1 SYNOPSIS
43  
44 my $query = Search::Query->parser->parse('foo');
45 print $query;
46  
47 =head1 DESCRIPTION
48  
49 Search::Query::Dialect is the base class from which all query dialects
50 inherit.
51  
52 A Dialect subclass must implement at least two methods:
53  
54 =over
55  
56 =item stringify
57  
58 Returns the serialized query tree.
59  
60 =item stringify_clause( I<leaf> )
61  
62 Returns one clause of a serialized query tree.
63  
64 =back
65  
66 See Search::Query::Dialect::Native for a working example.
67  
68 =head1 METHODS
69  
70 =head2 debug
71  
72 Get/set flag.
73  
74 =head2 default_field
75  
76 Standard attribute accessor. Default value is undef.
77  
78 =head2 init
79  
80 B<DEPRECATED>. Use BUILD() instead.
81  
82 =cut
83  
84 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
85 0 my $self = shift;
86 0 confess "Use BUILD() instead of init()";
87 }
88  
89 =head2 stringify
90  
91 All subclasses must override this method. The default behavior is to croak.
92  
93 =cut
94  
95 0 ▶sub stringify { croak "must implement stringify() in $_[0]" }
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
96  
97 =head2 tree
98  
99 Returns the query Dialect instance as a hashref structure, similar
100 to that of Search::QueryParser.
101  
102 =cut
103  
104 14 ▶sub tree {
Subroutine tree: called CC=8 SCAR=21.0
Pod documented
105 14 my $self = shift;
106 14 ▶ my $clause_class
Branch: 1 branch
BranchTrueFalseTotal
$self->parser ? : 13 1 14
107 = $self->parser
108 ? $self->parser->clause_class
109 : 'Search::Query::Clause';
110 14 my $dialect_class = blessed($self);
111 14 my %tree;
112  
113 #warn "before tree: " . dump($self);
114 14 foreach my $prefix ( '+', '', '-' ) {
115 42 ▶ next if !exists $self->{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$prefix} 27 15 42
116 15 my @clauses;
117 15 for my $clause ( @{ $self->{$prefix} } ) {
118  
119 28 ▶ if ( !blessed($clause) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($clause)) 0 28 28
120 0 croak "unblessed clause in Dialect object: " . dump($clause);
121 }
122  
123 28 ▶ if ( $clause->can('tree') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($clause->can('tree')) { } 0 28 28
elsif (builtin::blessed($clause->value)) { } 4 24 28
124  
125 #warn "clause isa Dialect: " . dump($clause);
126 0 push @clauses, $clause->tree;
127 }
128 elsif ( blessed( $clause->value ) ) {
129  
130 #warn "clause->value isa Dialect: " . dump($clause);
131 4 my $clause_ref = {%$clause};
132 4 $clause_ref->{value} = $clause->{value}->tree;
133 4 push @clauses, $clause_ref;
134 }
135 else {
136  
137 #warn "clause isa Clause: " . dump($clause);
138 24 push @clauses, {%$clause};
139 }
140 }
141 15 $tree{$prefix} = \@clauses;
142 }
143  
144 #warn "after tree: " . dump( \%tree );
145  
146 14 return \%tree;
147 }
148  
149 =head2 walk( I<CODE> )
150  
151 Traverse a Dialect object, calling I<CODE> on each Clause.
152 The I<CODE> reference should expect 4 arguments:
153  
154 =over
155  
156 =item
157  
158 The Clause object.
159  
160 =item
161  
162 The Dialect object.
163  
164 =item
165  
166 The I<CODE> reference.
167  
168 =item
169  
170 The prefix ("+", "-", and "") for the Clause.
171  
172 =back
173  
174 =cut
175  
176 287 ▶sub walk {
Subroutine walk: called CC=8 SCAR=23.4
Pod documented
177 287 my $self = shift;
178 287 my $code = shift;
179 287 ▶ if ( !$code or !ref($code) or ref($code) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $code && ref $code or ref $code ne "CODE") 0 287 287
Condition33%2/6
not $code && ref $code or ref $code ne "CODE"
l!l&&r!l&&!r
00287
$code && ref $code
!ll&&!rl&&r
00287
MC/DC0%0/3
not $code && ref $code or ref $code ne "CODE"
$code ref $code ref $code ne "CODE"
Truth table25%1/4
not(A && B) or C
A= $codeB= ref $codeC= ref $code ne "CODE"
ABCresult
0XX1
10X1
1111
1100
180 0 croak "CODE ref required";
181 }
182 287 ▶ my $tree = shift || $self;
Condition33%1/3
shift() || $self
l!l&&r!l&&!r
02870
MC/DC0%0/2
shift() || $self
shift() $self
Truth table33%1/3
A || B
A= shift()B= $self
ABresult
1X1
011
000
183 287 foreach my $prefix ( '+', '', '-' ) {
184 855 ▶ next if !exists $tree->{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tree->{$prefix} 556 299 855
185 299 for my $clause ( @{ $tree->{$prefix} } ) {
186  
187 #warn "clause: " . dump $clause;
188 442 $code->( $clause, $tree, $code, $prefix );
189 }
190 }
191 284 return $tree;
192 }
193  
194 =head2 translate_to( I<dialect> )
195  
196 Translate from one Dialect to another. Returns an object
197 blessed into the I<dialect> class.
198  
199 =cut
200  
201 1 ▶sub translate_to {
Subroutine translate_to: called CC=2 SCAR=7.1
Pod documented
202 1 my $self = shift;
203 1 ▶ my $dialect = shift or croak "Dialect required";
Branch: 1 branch
BranchTrueFalseTotal
my $dialect = shift() or croak("Dialect required") 1 0 1
204 1 my $query_class = Search::Query->get_dialect($dialect);
205 1 my $copy = $self->tree;
206 1 my $new_dialect = bless( $copy, $query_class );
207 1 my $clause_class = $self->parser->clause_class;
208 2 ▶ my $code = sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
209 2 my ( $clause, $dialect, $sub, $prefix ) = @_;
210  
211 #warn "before: " . dump($clause);
212 2 ▶ if ( exists $clause->{field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $clause->{'field'}) { } 2 0 2
213  
214 #warn "clause: " . dump $clause;
215 2 $clause = bless( $clause, $clause_class );
216 }
217 else {
218 0 $clause = bless( $clause, $query_class );
219 0 $clause->walk($sub);
220 }
221  
222 #warn "after : " . dump($clause);
223 1 };
224 1 $new_dialect->walk($code);
225 1 return $new_dialect;
226 }
227  
228 =head2 add_or_clause( I<clause> )
229  
230 Add I<clause> as an "or" leaf to the Dialect object.
231  
232 =cut
233  
234 1 ▶sub add_or_clause {
Subroutine add_or_clause: called CC=2 SCAR=7.0
Pod documented
235  
236 # DO NOT shift
237 1 my $self = $_[0];
238 1 ▶ my $clause = $_[1] or croak "Clause object required";
Branch: 1 branch
BranchTrueFalseTotal
my $clause = $_[1] or croak("Clause object required") 1 0 1
239 1 my $str = "($self) OR ($clause)";
240 1 $_[0] = $self->parser->parse($str);
241 1 return $_[0];
242 }
243  
244 =head2 add_and_clause( I<clause> )
245  
246 Add I<clause> as an "and" leaf to the Dialect object.
247  
248 =cut
249  
250 2 ▶sub add_and_clause {
Subroutine add_and_clause: called CC=2 SCAR=7.0
Pod documented
251  
252 # DO NOT shift
253 2 my $self = $_[0];
254 2 ▶ my $clause = $_[1] or croak "Clause object required";
Branch: 1 branch
BranchTrueFalseTotal
my $clause = $_[1] or croak("Clause object required") 2 0 2
255 2 my $str = "($self) AND ($clause)";
256 2 $_[0] = $self->parser->parse($str);
257 2 return $_[0];
258 }
259  
260 =head2 add_not_clause( I<clause> )
261  
262 Add I<clause> as a "not" leaf to the Dialect object.
263  
264 =cut
265  
266 1 ▶sub add_not_clause {
Subroutine add_not_clause: called CC=2 SCAR=7.0
Pod documented
267  
268 # DO NOT shift
269 1 my $self = $_[0];
270 1 ▶ my $clause = $_[1] or croak "Clause object required";
Branch: 1 branch
BranchTrueFalseTotal
my $clause = $_[1] or croak("Clause object required") 1 0 1
271 1 my $str = "($self) NOT ($clause)";
272 1 $_[0] = $self->parser->parse($str);
273 1 return $_[0];
274 }
275  
276 =head2 add_sub_clause( I<clause> )
277  
278 Add I<clause> as a sub clause to the Dialect object. In this
279 case, I<clause> should also be a Dialect object.
280  
281 =cut
282  
283 1 ▶sub add_sub_clause {
Subroutine add_sub_clause: called CC=4 SCAR=15.2
Pod documented
284  
285 # DO NOT shift
286 1 my $self = $_[0];
287 1 my $self_ref = \$_[0];
288 1 my $clause = $_[1];
289 1 ▶ if ( !$clause
Branch: 1 branch
BranchTrueFalseTotal
unless ($clause and builtin::blessed($clause) and $clause->isa("Search::Query::Dialect")) 0 1 1
Condition33%2/6
$clause and builtin::blessed($clause) and $clause->isa("Search::Query::Dialect")
!ll&&!rl&&r
001
$clause and builtin::blessed($clause)
!ll&&!rl&&r
001
MC/DC0%0/3
$clause and builtin::blessed($clause) and $clause->isa("Search::Query::Dialect")
$clause builtin::blessed($clause) $clause->isa("Search::Query::Dialect")
Truth table25%1/4
A and B and C
A= $clauseB= builtin::blessed($clause)C= $clause->isa("Search::Query::Dialect")
ABCresult
0XX0
10X0
1100
1111
290 or !blessed($clause)
291 or !$clause->isa('Search::Query::Dialect') )
292 {
293 0 croak "Dialect object required";
294 }
295 1 my %methods = (
296 "" => 'add_or_clause',
297 "+" => 'add_and_clause',
298 "-" => 'add_not_clause',
299 );
300 $clause->walk(
301 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
302 1 my ( $subclause, $dialect, $code, $prefix ) = @_;
303 1 my $method = $methods{$prefix};
304 1 $$self_ref = $self->$method($subclause);
305 }
306 1 );
307  
308 }
309  
310 =head2 field_class
311  
312 Should return the name of the Field class associated with the Dialect.
313 Default is 'Search::Query::Field'.
314  
315 =cut
316  
317 10 ▶sub field_class {'Search::Query::Field'}
Subroutine field_class: called CC=1 SCAR=0.0
Pod documented
318  
319 =head2 get_default_field
320  
321 Returns the default field for this Dialect.
322  
323 =cut
324  
325 16 ▶sub get_default_field {
Subroutine get_default_field: called CC=4 SCAR=15.2
Pod documented
326 16 my $self = shift;
327 16 my $field = $self->default_field;
328 16 ▶ $field = $self->parser->default_field unless defined $field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 16 16
329 16 ▶ if ( !defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field) 0 16 16
330 0 confess "must define a default_field";
331 }
332 16 ▶ return ref $field ? $field : [$field];
Branch: 1 branch
BranchTrueFalseTotal
ref $field ? : 16 0 16
333 }
334  
335 =head2 get_field( I<field_name> )
336  
337 Returns a Field object instance for I<field_name>. The object
338 will be an instance of B<field_class>.
339  
340 This is a shorthand wrapper around the method of the same
341 name in the internal B<parser> object.
342  
343 =cut
344  
345 151 ▶sub get_field {
Subroutine get_field: called CC=4 SCAR=18.6
Pod documented
346 151 my $self = shift;
347 151 ▶ my $name = shift or croak "field name required";
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or croak("field name required") 151 0 151
348 151 my $field = $self->parser->get_field($name);
349 151 ▶ if ( !$field ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 151 151
350 0 ▶ if ( $self->parser->croak_on_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parser->croak_on_error) 0 0 0
351 0 confess "invalid field name: $name";
352 }
353 0 my $field_class = $self->field_class;
354 0 $field = $field_class->new( name => $name );
355 }
356 151 return $field;
357 }
358  
359 =head2 preprocess( I<query_string> )
360  
361 Called by Parser in parse() before actually building the Dialect object
362 from I<query_string>.
363  
364 This allows for any "cleaning up" or other munging of I<query_string>
365 to support the official Parser syntax.
366  
367 The default just returns I<query_string> untouched. Subclasses should
368 return a parseable string.
369  
370 =cut
371  
372 84 ▶sub preprocess { return $_[1] }
Subroutine preprocess: called CC=1 SCAR=0.0
Pod documented
373  
374 =head2 parser
375  
376 Returns the Search::Query::Parser object that generated the Dialect
377 object.
378  
379 =cut
380  
381 10 1;
382  
383 __END__
« Query/Field/SQL.pm ↑ Summary Query.pm »