blib/lib/SQL/YASP.pm

filter: Statementstmt 37.1% 437 / 1178 Branchbran 12.4% 70 / 564 Conditioncond 13.3% 33 / 248 MC/DCmcdc 2.1% 3 / 143 Subroutinesub 38.1% 72 / 189 Podpod 35.7% 10 / 28 totaltotal 26.6% 625 / 2350 CC 390 SCAR 66.5
CC
390
Cov
27%
evalexpr
5112.0
sql_split
987.2
comparetype
552.0
CRAP 771.6
↑ Summary
1 package SQL::YASP;
2 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # debug tools
8 # use Debug::ShowStuff ':all';
9 # use Debug::ShowStuff::ShowVar;
10  
11 # documentation at end of file
12  
13 # globals
14 1 ▶use vars qw[@ISA @EXPORT_OK %EXPORT_TAGS %StdDelimiters $defparser $VERSION $nullchar $wineof $err $errstr];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 $VERSION = '0.12';
16  
17 # export
18 1 @ISA = 'Exporter';
19 1 @EXPORT_OK =
20 qw[
21 arr_split get_ixhash comma_split field_set_list
22 ARG_STRING ARG_NONE ARG_RAW ARG_NUMERIC ARG_SENDNULLS
23 OP_BETWEEN OP_LOGICAL OP_ADD OP_MULT OP_EXP OP_MISC
24 ];
25 1 %EXPORT_TAGS = ('all' => [@EXPORT_OK]);
26  
27  
28 # constants
29 1 ▶use constant SECTION_RETURN => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use constant SECTION_FIELD_SET_LIST => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use constant SECTION_COMMA_SPLIT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use constant SECTION_EXPRESSION => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant SECTION_OBJECT_LIST => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use constant SECTION_ARG_LIST => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use constant SECTION_SINGLE_WORD => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant SECTION_TABLE_LIST => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use constant SECTION_ORDER_BY => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use constant IPOS => 3; # position of the $i argument in sql_split
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # comparison types
41 1 ▶use constant CMP_AGNOSTIC => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use constant CMP_STRING => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use constant CMP_NUMBER => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 # argument types
46 1 ▶use constant ARG_STRING => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant ARG_NONE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant ARG_RAW => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant ARG_NUMERIC => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant ARG_SENDNULLS => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # operator precedence levels
53 1 ▶use constant OP_BETWEEN => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use constant OP_LOGICAL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use constant OP_ADD => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use constant OP_MULT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use constant OP_EXP => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use constant OP_MISC => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 # braces around field names
61 1 ▶use constant FIELD_BRACES_PROHIBIT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use constant FIELD_BRACES_ALLOW => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use constant FIELD_BRACES_REQUIRE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 # misc constants
66 1 ▶use constant OPTSPKG => 'SQL::YASP::Opts';
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68  
69 # special characters
70 1 $nullchar = chr(0);
71 1 $wineof = chr(26);
72  
73  
74 #------------------------------------------------------------------------------
75 # new
76 # OVERRIDE ME
77 #
78 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
79 1 my ($class) = @_;
80 1 my $self = bless({}, $class);
81
82 # always call after_new just before returning parser object
83 1 $self->after_new;
84
85 1 return $self;
86 }
87 #
88 # new
89 #------------------------------------------------------------------------------
90  
91  
92 #------------------------------------------------------------------------------
93 # build_tree
94 # OVERRIDE ME
95 #
96 1 ▶sub build_tree {
Subroutine build_tree: called CC=12 SCAR=43.1
Pod documented
97 1 my ($self, $stmt, $tokens, %opts) = @_;
98 1 my ($cmd);
99
100 # always set $stmt->{'command'}
101 1 $cmd = $stmt->{'command'} = shift @$tokens;
102
103 # create
104 1 ▶ if ($cmd eq 'create')
Branch: 6 branches
BranchTrueFalseTotal
if ($cmd eq 'create') { } 0 1 1
elsif ($cmd eq 'select') { } 1 0 1
elsif ($cmd eq 'insert') { } 0 0 0
elsif ($cmd eq 'update') { } 0 0 0
elsif ($cmd eq 'delete') { } 0 0 0
elsif ($opts{'allow_unknown_command'}) { } 0 0 0
105 0 ▶ {$self->tree_create($stmt, @$tokens) or return undef}
Branch: 1 branch
BranchTrueFalseTotal
$self->tree_create($stmt, @$tokens) or return undef 0 0 0
106
107 # select
108 elsif ($cmd eq 'select')
109 1 ▶ {$self->tree_select($stmt, @$tokens) or return undef}
Branch: 1 branch
BranchTrueFalseTotal
$self->tree_select($stmt, @$tokens) or return undef 1 0 1
110
111 # insert
112 elsif ($cmd eq 'insert')
113 0 ▶ {$self->tree_insert($stmt, @$tokens) or return undef}
Branch: 1 branch
BranchTrueFalseTotal
$self->tree_insert($stmt, @$tokens) or return undef 0 0 0
114
115 # update
116 elsif ($cmd eq 'update')
117 0 ▶ {$self->tree_update($stmt, @$tokens) or return undef}
Branch: 1 branch
BranchTrueFalseTotal
$self->tree_update($stmt, @$tokens) or return undef 0 0 0
118
119 # delete
120 elsif ($cmd eq 'delete')
121 0 ▶ {$self->tree_delete($stmt, @$tokens) or return undef}
Branch: 1 branch
BranchTrueFalseTotal
$self->tree_delete($stmt, @$tokens) or return undef 0 0 0
122
123 # if allow unknown command
124 elsif ($opts{'allow_unknown_command'})
125 0 { return undef }
126
127 # else don't recognize command
128 else
129 0 {croak "[1] Do not recognize command: [$stmt->{'command'}]"}
130 }
131 #
132 # build_tree
133 #------------------------------------------------------------------------------
134  
135  
136 #------------------------------------------------------------------------------
137 # tree_create
138 #
139 # OVERRIDE ME
140 #
141 0 ▶sub tree_create {
Subroutine tree_create: not called CC=2 SCAR=17.9
Pod documented
142 0 my ($self, $stmt, @els) = @_;
143
144 # hold on to create type
145 0 $stmt->{'create_type'} = shift(@els);
146
147 # create table
148 0 ▶ if ($stmt->{'create_type'} eq 'table')
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt->{"create_type"} eq "table") 0 0 0
149 0 {return $self->tree_create_table($stmt, @els)}
150
151 # else don't know this type of object
152 0 croak "do not know how to create this type of object: $self->{'create_type'}";
153 }
154 #
155 # tree_create
156 #------------------------------------------------------------------------------
157  
158  
159 #------------------------------------------------------------------------------
160 # tree_create_table
161 # OVERRIDE ME
162 #
163 0 ▶sub tree_create_table {
Subroutine tree_create_table: not called CC=7 SCAR=40.3
Pod documented
164 0 my ($self, $stmt, @els) = @_;
165 0 my ($fields);
166
167 0 $stmt->{'table_name'} = shift @els;
168 0 $stmt->{'fields'} = $fields = get_ixhash();
169
170 FIELDLOOP:
171 0 foreach my $field_def (comma_split(\@els)) {
172 0 my @fieldargs = @$field_def;
173 0 my ($field_name, $field);
174
175 # if this is a command, not a field definition
176 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"non_fields"}{"create"} and exists $self->{"non_fields"}{"create"}{$fieldargs[0]}) 0 0 0
Condition0%0/3
exists $self->{"non_fields"}{"create"} and exists $self->{"non_fields"}{"create"}{$fieldargs[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"non_fields"}{"create"} and exists $self->{"non_fields"}{"create"}{$fieldargs[0]}
exists $self->{"non_fields"}{"create"} exists $self->{"non_fields"}{"create"}{$fieldargs[0]}
Truth table0%0/3
A and B
A= exists $self->{"non_fields"}{"create"}B= exists $self->{"non_fields"}{"create"}{$fieldargs[0]}
ABresult
0X0
100
111
177 exists($self->{'non_fields'}->{'create'}) &&
178 exists($self->{'non_fields'}->{'create'}->{$fieldargs[0]})
179 ) {
180 0 ▶ $stmt->{'arguments'} ||= [];
Condition0%0/2
$stmt->{"arguments"} ||= []
l!l
00
MC/DC0%0/1
$stmt->{"arguments"} ||= []
$stmt->{"arguments"}
Truth table0%0/2
A
A= $stmt->{"arguments"}
Aresult
11
00
181 0 push @{$stmt->{'arguments'}}, @fieldargs;
182 0 next FIELDLOOP;
183 }
184
185 # get data type
186 0 $field = {};
187 0 $field_name = shift @fieldargs;
188 0 $field->{'data_type'} = {name=>shift @fieldargs};
189 0 $field->{'modifiers'} = [];
190
191 # add arguments to data type
192 0 add_args($field->{'data_type'}, \@fieldargs);
193
194 # loop through remaining arguments
195 0 ▶ while (@fieldargs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fieldargs) 0 0 0
196 0 my $arg = shift @fieldargs;
197 0 my $setting = {};
198
199 # if the word is "not", then use the following word
200 # as the arg name
201 0 ▶ if ($arg eq 'not') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq "not") 0 0 0
202 0 $setting->{'not'} = 1;
203 0 $arg = shift @fieldargs;
204 }
205
206 0 add_args($setting, \@fieldargs);
207 0 $setting->{'name'} = $arg;
208 0 push @{$field->{'modifiers'}}, $setting;
209 }
210
211 # store in fields hash
212 0 $fields->{$field_name} = $field;
213 }
214
215 0 return 1;
216 }
217 #
218 # tree_create_table
219 #------------------------------------------------------------------------------
220  
221  
222 #------------------------------------------------------------------------------
223 # tree_select
224 #
225 # OVERRIDE ME
226 #
227 1 ▶sub tree_select {
Subroutine tree_select: called CC=2 SCAR=7.0
Pod documented
228 1 my ($self, $stmt, @els) = @_;
229 1 my ($unset);
230
231 1 $unset = $self->get_sections(
232 $stmt, \@els,
233 'from' => SECTION_TABLE_LIST,
234 'order by' => SECTION_ORDER_BY,
235 'where' => SECTION_EXPRESSION,
236 'having' => SECTION_EXPRESSION,
237 'group by' => SECTION_COMMA_SPLIT,
238 'into' => SECTION_TABLE_LIST,
239 );
240
241 1 ▶ defined($unset) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $unset or return undef 1 0 1
242
243 1 $stmt->{'fields'} = $self->tree_select_fields($stmt, $unset->{':open'});
244 }
245 #
246 # tree_select
247 #------------------------------------------------------------------------------
248  
249  
250 #------------------------------------------------------------------------------
251 # tree_delete
252 #
253 # OVERRIDE ME
254 #
255 0 ▶sub tree_delete {
Subroutine tree_delete: not called CC=1 SCAR=6.9
Pod documented
256 0 my ($self, $stmt, @els) = @_;
257 0 my ($unset);
258
259 0 $unset = $self->get_sections($stmt, \@els,
260 'from' => SECTION_TABLE_LIST,
261 'where' => SECTION_EXPRESSION,
262 );
263 }
264 #
265 # tree_delete
266 #------------------------------------------------------------------------------
267  
268  
269 #------------------------------------------------------------------------------
270 # tree_insert
271 #
272 # OVERRIDE ME
273 #
274 0 ▶sub tree_insert {
Subroutine tree_insert: not called CC=3 SCAR=24.8
Pod documented
275 0 my ($self, $stmt, @els) = @_;
276 0 my ($unset);
277
278 0 $unset = $self->get_sections($stmt, \@els,
279 'into' => SECTION_RETURN,
280 'values' => SECTION_RETURN,
281 'set' => SECTION_FIELD_SET_LIST,
282 );
283
284 # into
285 0 ▶ if ($unset->{'into'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unset->{"into"}) 0 0 0
286 0 $stmt->{'table_name'} = shift @{$unset->{'into'}};
287 0 ▶ get_set_fields($stmt, $unset->{'into'}, $unset)
Branch: 1 branch
BranchTrueFalseTotal
get_set_fields($stmt, $unset->{"into"}, $unset) or return undef 0 0 0
288 or return undef;
289 }
290 }
291 #
292 # tree_insert
293 #------------------------------------------------------------------------------
294  
295  
296 #------------------------------------------------------------------------------
297 # tree_update
298 #
299 # OVERRIDE ME
300 #
301 0 ▶sub tree_update {
Subroutine tree_update: not called CC=2 SCAR=17.9
Pod documented
302 0 my ($self, $stmt, @els) = @_;
303 0 my ($unset, $opener);
304
305 0 $unset = $self->get_sections($stmt, \@els,
306 'values' => SECTION_RETURN,
307 'where' => SECTION_EXPRESSION,
308 'set' => SECTION_FIELD_SET_LIST,
309 );
310
311 0 $opener = $unset->{':open'};
312 0 $stmt->{'table_name'} = shift @{$opener};
313
314 # set "set" clause
315 0 ▶ get_set_fields($stmt, $opener, $unset)
Branch: 1 branch
BranchTrueFalseTotal
get_set_fields($stmt, $opener, $unset) or return undef 0 0 0
316 or return undef;
317 }
318 #
319 # tree_update
320 #------------------------------------------------------------------------------
321  
322  
323 #------------------------------------------------------------------------------
324 # get_set_fields
325 #
326 0 ▶sub get_set_fields {
Subroutine get_set_fields: not called CC=5 SCAR=34.0
Pod undocumented
327 0 my ($stmt, $fieldlist, $unset) = @_;
328
329 # if a SET clause wasn't sent, and a VALUES clause was,
330 # set "set" using values
331 0 ▶ if ( (! $stmt->{'set'}) && $unset->{'values'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $stmt->{"set"} and $unset->{"values"}) 0 0 0
Condition0%0/3
not $stmt->{"set"} and $unset->{"values"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $stmt->{"set"} and $unset->{"values"}
not $stmt->{"set"} $unset->{"values"}
Truth table0%0/3
not(A) and B
A= not $stmt->{"set"}B= $unset->{"values"}
ABresult
0X0
100
111
332 0 my (%set, @fields, @exprs, $i);
333
334 0 @fields = comma_split([deref_args($fieldlist)]);
335 0 @exprs = comma_split( [deref_args($unset->{'values'})] );
336 0 $i = 0;
337
338 0 ▶ if (@fields != @exprs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields != @exprs) 0 0 0
339 0 SQL::YASP::Expr::set_err('invalid syntax: field list and expression list must have same number of elements');
340 0 return undef;
341 }
342
343 0 ▶ while ($i <= $#fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#fields) 0 0 0
344 0 $set{$fields[$i]->[0]} = SQL::YASP::Expr->new($stmt, $exprs[$i]);
345 0 $i++;
346 }
347
348 0 $stmt->{'set'} = \%set;
349 }
350
351 0 return $stmt->{'set'};
352 }
353 #
354 # get_set_fields
355 #------------------------------------------------------------------------------
356  
357  
358 #------------------------------------------------------------------------------
359 # tree_select_fields
360 #
361 1 ▶sub tree_select_fields {
Subroutine tree_select_fields: called CC=8 SCAR=31.2
Pod undocumented
362 1 my ($self, $stmt, $clause) = @_;
363 1 my $cc = ref($self) . '::Expr'; # clause class
364 1 my $rv = get_ixhash();
365
366 # get field list
367 1 foreach my $fielddef (arr_split([','], $clause)) {
368 1 my @def = @$fielddef;
369
370 # single field
371 1 ▶ if (@def == 1){
Branch: 2 branches
BranchTrueFalseTotal
if (@def == 1) { } 1 0 1
elsif (@def >= 3 and $def[-2] eq 'as') { } 0 0 0
372 # TODO: need to address possibility of format tablename.*
373 # For now we assume that the select is from just one table.
374 #
375 # If that single field is '*', and if we got a table definition hash.
376 #
377 1 ▶ if ( ($def[0] eq '*') && $stmt->{'table_definitions'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def[0] eq '*' and $stmt->{'table_definitions'}) { } 0 1 1
Condition33%1/3
$def[0] eq "*" and $stmt->{"table_definitions"}
!ll&&!rl&&r
100
MC/DC0%0/2
$def[0] eq "*" and $stmt->{"table_definitions"}
$def[0] eq "*" $stmt->{"table_definitions"}
Truth table33%1/3
A and B
A= $def[0] eq "*"B= $stmt->{"table_definitions"}
ABresult
0X0
100
111
378 # Get the name of the first table. See note above
379 # for why we do this little cop-out.
380 0 my $tablename = $stmt->{'from'}->{(keys(%{$stmt->{'from'}}))[0]};
381 0 my $col_defs = $stmt->{'table_definitions'}->{$tablename}->{'col_defs'};
382
383 0 foreach my $fieldname (keys %{$col_defs})
384 0 {$rv->{$fieldname} = $cc->new($stmt, $fieldname)}
385 }
386
387 # else it's just the name of a table
388 else
389 1 {$rv->{$def[0]} = $cc->new($stmt, @def)}
390 }
391
392 # else if in format "expression as fieldname"
393 ▶ elsif ( (@def >= 3) && ($def[-2] eq 'as') ) {
Condition0%0/3
@def >= 3 and $def[-2] eq "as"
!ll&&!rl&&r
000
MC/DC0%0/2
@def >= 3 and $def[-2] eq "as"
@def >= 3 $def[-2] eq "as"
Truth table0%0/3
A and B
A= @def >= 3B= $def[-2] eq "as"
ABresult
0X0
100
111
394 0 my $name = pop @def;
395 0 pop @def;
396 0 $rv->{$name} = $cc->new($stmt, @def);
397 }
398
399 # else use entire string as field name
400 else
401 0 {$rv->{restring(@def)} = $cc->new($stmt, @def)}
402 }
403
404 1 return $rv;
405 }
406 #
407 # tree_select_fields
408 #------------------------------------------------------------------------------
409  
410  
411 ###############################################################################
412 # IT IS NOT RECOMMENDED THAT YOU OVERRIDE ANY OF THE METHODS FROM HERE DOWN #
413 ###############################################################################
414  
415  
416 #------------------------------------------------------------------------------
417 # after_new
418 #
419 1 ▶sub after_new {
Subroutine after_new: called CC=23 SCAR=41.1
Pod undocumented
420 1 my ($self) = @_;
421 1 my (%quotes, %allops);
422
423 # set which characters are quotes
424 1 ▶ $self->{'quotes'} ||= ['"', "'"];
Condition50%1/2
$self->{"quotes"} ||= ["\"", "'"]
l!l
01
MC/DC0%0/1
$self->{"quotes"} ||= ["\"", "'"]
$self->{"quotes"}
Truth table50%1/2
A
A= $self->{"quotes"}
Aresult
11
00
425 1 $quotes{$_} = 1 for @{$self->{'quotes'}};
426 1 $self->{'quotes'} = \%quotes;
427
428 # tokenizer properties
429 1 ▶ exists($self->{'lukas'}) or $self->{'lukas'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"lukas"} or $self->{"lukas"} = 1 0 1 1
430 1 ▶ exists($self->{'type_fix'}) or $self->{'type_fix'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"type_fix"} or $self->{"type_fix"} = 1 0 1 1
431 1 ▶ exists($self->{'perl_regex'}) or $self->{'perl_regex'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"perl_regex"} or $self->{"perl_regex"} = 1 0 1 1
432 1 ▶ exists($self->{'star_comments'}) or $self->{'star_comments'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"star_comments"} or $self->{"star_comments"} = 1 0 1 1
433 1 ▶ exists($self->{'dash_comments'}) or $self->{'dash_comments'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"dash_comments"} or $self->{"dash_comments"} = 1 0 1 1
434 1 ▶ exists($self->{'pound_comments'}) or $self->{'pound_comments'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"pound_comments"} or $self->{"pound_comments"} = 1 0 1 1
435 1 ▶ exists($self->{'!_is_not'}) or $self->{'!_is_not'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"!_is_not"} or $self->{"!_is_not"} = 1 0 1 1
436 1 ▶ exists($self->{'backslash_escape'}) or $self->{'backslash_escape'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"backslash_escape"} or $self->{"backslash_escape"} = 1 0 1 1
437 1 ▶ exists($self->{'dquote_escape'}) or $self->{'dquote_escape'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"dquote_escape"} or $self->{"dquote_escape"} = 1 0 1 1
438 1 ▶ exists($self->{'field_braces'}) or $self->{'field_braces'} = FIELD_BRACES_PROHIBIT;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"field_braces"} or $self->{"field_braces"} = 0 0 1 1
439
440 # double word tokens
441 1 ▶ $self->{'double_word_tokens'} ||= {
Condition50%1/2
$self->{"double_word_tokens"} ||= {"primary", {"key", 1}, "current", {"date", 1}, "order", {"by", 1}, "group", {"by", 1}}
l!l
01
MC/DC0%0/1
$self->{"double_word_tokens"} ||= {"primary", {"key", 1}, "current", {"date", 1}, "order", {"by", 1}, "group", {"by", 1}}
$self->{"double_word_tokens"}
Truth table50%1/2
A
A= $self->{"double_word_tokens"}
Aresult
11
00
442 primary => {key=>1},
443 current => {date=>1},
444 order => {by=>1},
445 group => {by=>1},
446 };
447
448 # operators
449 1 ▶ $self->{'ops'} ||= \@SQL::YASP::Expr::dbin;
Condition50%1/2
$self->{"ops"} ||= \@SQL::YASP::Expr::dbin
l!l
01
MC/DC0%0/1
$self->{"ops"} ||= \@SQL::YASP::Expr::dbin
$self->{"ops"}
Truth table50%1/2
A
A= $self->{"ops"}
Aresult
11
00
450
451 # functions
452 1 ▶ $self->{'functions'} ||= \%SQL::YASP::Expr::dfuncs;
Condition50%1/2
$self->{"functions"} ||= \%SQL::YASP::Expr::dfuncs
l!l
01
MC/DC0%0/1
$self->{"functions"} ||= \%SQL::YASP::Expr::dfuncs
$self->{"functions"}
Truth table50%1/2
A
A= $self->{"functions"}
Aresult
11
00
453
454 # hash of all operators
455 1 foreach my $level (@{$self->{'ops'}})
456 6 {@allops{keys %{$level}} = ()}
457 1 $self->{'allops'} = \%allops;
458
459 # operator regex
460 1 $self->{'opregex'} = join('|', sort {length($b) <=> length($a)} map {$_=quotemeta($_)} keys %allops);
461
462 # This hash of words indicates words that are not field names,
463 # they are some other type of modifier. This property is mainly
464 # used by 'create table'.
465 1 ▶ $self->{'non_fields'} ||= {
Condition50%1/2
$self->{"non_fields"} ||= {"create", {"constraint", 1, "unique", 1}}
l!l
01
MC/DC0%0/1
$self->{"non_fields"} ||= {"create", {"constraint", 1, "unique", 1}}
$self->{"non_fields"}
Truth table50%1/2
A
A= $self->{"non_fields"}
Aresult
11
00
466 create => {
467 constraint => 1,
468 unique => 1,
469 },
470 };
471
472
473 #---------------------------------------------------------------
474 # extend Statement and Expr packages if they don't already exist
475 #
476 1 ▶ unless ( (my $class = ref($self)) eq 'SQL::YASP') {
Branch: 1 branch
BranchTrueFalseTotal
unless ((my $class = ref $self) eq "SQL::YASP") 0 1 1
477 0 my @isa;
478
479 0 eval "\@isa = \@${class}::Statement::ISA";
480 0 ▶ @isa or eval "\@isa = \@${class}::Statement::ISA = 'SQL::YASP::Statement'";
Branch: 1 branch
BranchTrueFalseTotal
@isa or eval "\@isa = \@${class}::Statement::ISA = 'SQL::YASP::Statement'" 0 0 0
481 0 ▶ @isa or croak 'did not set @isa';
Branch: 1 branch
BranchTrueFalseTotal
@isa or croak("did not set \@isa") 0 0 0
482
483 0 @isa = ();
484 0 eval "\@isa = \@${class}::Expr::ISA";
485 0 ▶ @isa or eval "\@isa = \@${class}::Expr::ISA = 'SQL::YASP::Expr'";
Branch: 1 branch
BranchTrueFalseTotal
@isa or eval "\@isa = \@${class}::Expr::ISA = 'SQL::YASP::Expr'" 0 0 0
486 0 ▶ @isa or croak 'did not set @isa';
Branch: 1 branch
BranchTrueFalseTotal
@isa or croak("did not set \@isa") 0 0 0
487 }
488 #
489 # extend Statement and Expr packages if they don't already exist
490 #---------------------------------------------------------------
491
492  
493 }
494 #
495 # after_new
496 #------------------------------------------------------------------------------
497  
498  
499 #------------------------------------------------------------------------------
500 # parse
501 #
502 1 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.4
Pod undocumented
503 1 my ($self, $sql, %opts) = @_;
504 1 my ($rv, @tokens, $carry);
505
506 # create parser if one wasn't passed
507 1 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 1 0 1
508 1 ▶ $self::defparser ||= $self->new;
Condition33%1/3
$self::defparser ||= $self->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$self::defparser ||= $self->new
$self::defparser $self->new
Truth table33%1/3
A ||= B
A= $self::defparserB= $self->new
ABresult
1X1
011
000
509 1 $self = $self::defparser;
510 }
511
512 # instantiate statement object to be returned
513 1 $rv = SQL::YASP::Statement->new();
514
515 # hold on to original SQL if requested to do so
516 1 ▶ $self->{'keep_org_sql'} and $rv->{'org_sql'} = $sql;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"keep_org_sql"} and $rv->{"org_sql"} = $sql 0 1 1
517
518 # remove trailing semicolon
519 1 $sql =~ s|\s*\;\s*$||s;
520
521 # tokenize statement
522 1 $carry = {placeholders=>[]};
523 1 @tokens = $self->sql_split($sql, $carry);
524
525 # get the command for this statement
526 1 $rv->{'placeholders'} = $carry->{'placeholders'};
527 1 $rv->{'placeholder_count'} = @{$carry->{'placeholders'}};
528 1 $rv->{'parser'} = $self;
529 1 $rv->{'table_definitions'} = $opts{'table_definitions'};
530
531 # build statement tree
532 1 ▶ $self->build_tree($rv, \@tokens, %opts) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->build_tree($rv, \@tokens, %opts) or return undef 1 0 1
533
534 # return statement object
535 1 return $rv;
536 }
537 #
538 # parse
539 #------------------------------------------------------------------------------
540  
541  
542 #------------------------------------------------------------------------------
543 # get_sections
544 #
545 1 ▶sub get_sections {
Subroutine get_sections: called CC=19 SCAR=39.0
Pod documented
546 1 my ($self, $stmt, $els, %opts) = @_;
547 1 my @clauses = arr_split([keys %opts], $els, keep_del_front=>1);
548 1 my $rv = {};
549
550 # if the first element is not a recognized command
551 1 ▶ unless (exists $opts{$els->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts{$els->[0]}) 1 0 1
552 1 my $open = shift @clauses;
553 1 $rv->{':open'} = $open;
554 }
555
556 # loop through sections assigning to statement
557 CLAUSELOOP:
558 1 foreach my $clause (@clauses) {
559 2 my $sname = shift @$clause;
560
561 # field set list
562 2 ▶ if ($opts{$sname} == SECTION_FIELD_SET_LIST)
Branch: 8 branches
BranchTrueFalseTotal
if ($opts{$sname} == 1) { } 0 2 2
elsif ($opts{$sname} == 6) { } 0 2 2
elsif ($opts{$sname} == 7) { } 1 1 2
elsif ($opts{$sname} == 2) { } 0 1 1
elsif ($opts{$sname} == 8) { } 0 1 1
elsif ($opts{$sname} == 3) { } 1 0 1
elsif ($opts{$sname} == 4) { } 0 0 0
elsif ($opts{$sname} == 5) { } 0 0 0
563 0 {$stmt->{$sname} = field_set_list($stmt, $clause)}
564
565 # single word
566 elsif ($opts{$sname} == SECTION_SINGLE_WORD)
567 0 {$stmt->{$sname} = $clause->[0]}
568
569 # from clause
570 # for now, just returns a single hash element
571 elsif ($opts{$sname} == SECTION_TABLE_LIST) {
572 1 my $tdefs = get_ixhash();
573
574 1 foreach my $table_def (comma_split($clause)) {
575 1 my ($key, $name);
576
577 # check for expression-as-table, which is
578 # out of scope
579 1 foreach my $def (@$table_def)
580 1 ▶ { ref($def) and return undef }
Branch: 1 branch
BranchTrueFalseTotal
ref $def and return undef 0 1 1
581
582 # get name
583 1 $name = lc(shift(@{$table_def}));
584
585 # if alias
586 1 ▶ if (@{$table_def})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$table_def;}) { } 0 1 1
587 0 {$key = $table_def->[0]}
588 else
589 1 {$key = $name}
590
591 1 $tdefs->{$key} = $name;
592 }
593
594 # default $stmt->{'table_name'} to empty string
595 1 $stmt->{'table_name'} = '';
596
597 # if 'from' clause contains exactly one table,
598 # put that single table into the {'table_name'} element
599 1 ▶ if (keys(%$tdefs) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$tdefs == 1) 1 0 1
600 1 my ($key) = keys(%$tdefs);
601 1 my ($val) = values(%$tdefs);
602
603 1 ▶ if ($key eq $val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq $val) 1 0 1
604 1 $stmt->{'table_name'} = $val;
605 }
606 }
607
608 1 $stmt->{$sname} = $tdefs;
609 }
610
611 # comma delimited list
612 elsif ($opts{$sname} == SECTION_COMMA_SPLIT)
613 0 {$stmt->{$sname} = comma_split($clause)}
614
615 # comma delimited list, build into expression objects
616 elsif ($opts{$sname} == SECTION_ORDER_BY){
617 0 my $exprs = comma_split($clause);
618
619 0 foreach my $expr (@$exprs) {
620 0 my ($desc);
621
622 0 ▶ if ($expr->[-1] eq 'desc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->[-1] eq "desc") 0 0 0
623 0 $desc = 1;
624 0 pop @$expr;
625 }
626
627 0 $expr = SQL::YASP::Expr->new($stmt, $expr);
628 0 $expr->{'desc'} = $desc;
629 }
630
631 0 $stmt->{$sname} = $exprs;
632 }
633
634 # expression
635 elsif ($opts{$sname} == SECTION_EXPRESSION)
636 1 {$stmt->{$sname} = SQL::YASP::Expr->new($stmt, $clause)}
637
638 # object list
639 elsif ($opts{$sname} == SECTION_OBJECT_LIST)
640 0 {$stmt->{$sname} = object_list($clause)}
641
642 # argument list
643 elsif ($opts{$sname} == SECTION_ARG_LIST)
644 0 {$stmt->{$sname} = comma_split([deref_args($clause)])}
645
646 # else return
647 else
648 0 {$rv->{$sname} = $clause}
649 }
650
651 1 return $rv;
652 }
653 #
654 # get_sections
655 #------------------------------------------------------------------------------
656  
657  
658 #------------------------------------------------------------------------------
659 # field_set_list
660 #
661 0 ▶sub field_set_list {
Subroutine field_set_list: not called CC=2 SCAR=17.9
Pod documented
662 0 my ($stmt, $allsets) = @_;
663 0 my $rv = {};
664
665 0 foreach my $set (comma_split($allsets)) {
666 0 my ($name, $expr) = arr_split(['='], $set, max=>2);
667 # $rv->{$name->[0]} = $expr;
668 0 $rv->{$name->[0]} = SQL::YASP::Expr->new($stmt, $expr);
669 }
670
671 0 return $rv;
672 }
673 #
674 # field_set_list
675 #------------------------------------------------------------------------------
676  
677  
678 #------------------------------------------------------------------------------
679 # add_args
680 #
681 0 ▶sub add_args {
Subroutine add_args: not called CC=4 SCAR=30.0
Pod undocumented
682 0 my ($field, $arr, %opts) = @_;
683 0 my ($args);
684
685 # early exit
686 0 ▶ @{$arr} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$arr;} or return 0 0 0 0
687 0 ▶ ref($arr->[0]) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $arr->[0] or return 0 0 0 0
688
689 # default property name for arguments
690 0 ▶ defined($opts{'arg_name'}) or $opts{'arg_name'} = 'arguments';
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{"arg_name"} or $opts{"arg_name"} = "arguments" 0 0 0
691
692 # add arguments property
693 0 $args = shift @{$arr};
694 0 $field->{$opts{'arg_name'}} = $args;
695 0 return 1;
696 }
697 #
698 # add_args
699 #------------------------------------------------------------------------------
700  
701  
702 #------------------------------------------------------------------------------
703 # sql_split
704 #
705 1 ▶sub sql_split {
Subroutine sql_split: called CC=57 SCAR=68.9
Pod undocumented
706 1 my ($self, $sql, $carry, $i) = @_;
707 1 my (@rv, @major, $inquote, $inlinecomment, $instar, $allspaces, @chars, @field, $dtokens, $lastwasnum, $inregex);
708 1 my %quotes = %{$self->{'quotes'}};
709 1 my $opregex = $self->{'opregex'};
710
711 # values that are carried through recursions
712 1 ▶ $carry ||= {};
Condition50%1/2
$carry ||= {}
l!l
10
MC/DC0%0/1
$carry ||= {}
$carry
Truth table50%1/2
A
A= $carry
Aresult
11
00
713 1 ▶ $carry->{'placeholders'} ||= [];
Condition50%1/2
$carry->{"placeholders"} ||= []
l!l
10
MC/DC0%0/1
$carry->{"placeholders"} ||= []
$carry->{"placeholders"}
Truth table50%1/2
A
A= $carry->{"placeholders"}
Aresult
11
00
714
715 # split entire string into single characters
716 1 ▶ @chars = ref($sql) ? @$sql : split('', $sql);
Branch: 1 branch
BranchTrueFalseTotal
ref $sql ? : 0 1 1
717 1 ▶ defined($i) or $i=0;
Branch: 1 branch
BranchTrueFalseTotal
defined $i or $i = 0 0 1 1
718
719 # loop through characters
720 CHARLOOP:
721 1 ▶ while ($i <= $#chars) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#chars) 41 1 42
722 41 my $char = $chars[$i++];
723
724 # if in quote
725 41 ▶ if (defined $inquote) {
Branch: 11 branches
BranchTrueFalseTotal
if (defined $inquote) { } 0 41 41
elsif ($inlinecomment) { } 0 41 41
elsif ($instar) { } 0 41 41
elsif ($char eq '-' and $self->{'dash_comments'} and $chars[$i] eq '-') { } 0 41 41
elsif ($char eq '#' and $self->{'pound_comments'}) { } 0 41 41
elsif ($char eq '/' and $self->{'star_comments'} and $chars[$i] eq '*') { } 0 41 41
elsif ($self->{'field_braces'} and $char eq '[') { } 0 41 41
elsif ($quotes{$char}) { } 0 41 41
elsif ($char eq '=' and $self->{'perl_regex'} and $chars[$i] and $chars[$i] eq '~') { } 0 41 41
elsif ($char eq '(') { } 0 41 41
elsif ($char eq ')') { } 0 41 41
726 # escape next character
727 0 ▶ if ( ($char eq '\\') && ($self->{'backslash_escape'}) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq '\\' and $self->{'backslash_escape'}) { } 0 0 0
elsif ($char eq $inquote) { } 0 0 0
Condition0%0/3
$char eq "\\" and $self->{"backslash_escape"}
!ll&&!rl&&r
000
MC/DC0%0/2
$char eq "\\" and $self->{"backslash_escape"}
$char eq "\\" $self->{"backslash_escape"}
Truth table0%0/3
A and B
A= $char eq "\\"B= $self->{"backslash_escape"}
ABresult
0X0
100
111
728 0 push @field, $char, splice(@chars, $i, 1);
729 0 next CHARLOOP;
730 }
731
732 elsif ($char eq $inquote) {
733 # if the next character is also a quote,
734 # then remove it and don't go out of inquote mode
735 0 ▶ if ( (! $inregex) && defined($chars[$i]) && ($chars[$i] eq $inquote) && $self->{'dquote_escape'} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $inregex and defined $chars[$i] and $chars[$i] eq $inquote and $self->{'dquote_escape'}) { } 0 0 0
Condition0%0/9
not $inregex and defined $chars[$i] and $chars[$i] eq $inquote and $self->{"dquote_escape"}
!ll&&!rl&&r
000
not $inregex and defined $chars[$i] and $chars[$i] eq $inquote
!ll&&!rl&&r
000
not $inregex and defined $chars[$i]
!ll&&!rl&&r
000
MC/DC0%0/4
not $inregex and defined $chars[$i] and $chars[$i] eq $inquote and $self->{"dquote_escape"}
not $inregex defined $chars[$i] $chars[$i] eq $inquote $self->{"dquote_escape"}
Truth table0%0/5
not(A) and B and C and D
A= not $inregexB= defined $chars[$i]C= $chars[$i] eq $inquoteD= $self->{"dquote_escape"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
736 0 {push @field, splice(@chars, $i, 1)}
737
738 # else end the quote
739 else {
740 0 my ($field);
741 0 undef $inquote;
742
743 # if in regex
744 0 ▶ if ($inregex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inregex) { } 0 0 0
745 0 my @params;
746 0 $field = {rx => join('', @field)};
747
748 # get trailing characters
749 0 ▶ while ( ($i <= $#chars) && ($chars[$i] =~ m|[a-z]|i) )
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#chars and $chars[$i] =~ /[a-z]/i) 0 0 0
Condition0%0/3
$i <= $#chars and $chars[$i] =~ /[a-z]/i
!ll&&!rl&&r
000
MC/DC0%0/2
$i <= $#chars and $chars[$i] =~ /[a-z]/i
$i <= $#chars $chars[$i] =~ /[a-z]/i
Truth table0%0/3
A and B
A= $i <= $#charsB= $chars[$i] =~ /[a-z]/i
ABresult
0X0
100
111
750 0 {push @params, splice@chars, $i, 1}
751
752 0 $field->{'params'} = join('', @params);
753 0 undef $inregex;
754 }
755
756 # else regular quote
757 else
758 0 {$field = joinfield(@field, $char)}
759
760 0 push @major, $field;
761 0 @field = ();
762 0 next CHARLOOP;
763 }
764 }
765 }
766
767 # in line comment
768 elsif ($inlinecomment) {
769 0 ▶ $char =~ m|[\n\r]| or next CHARLOOP;
Branch: 1 branch
BranchTrueFalseTotal
$char =~ /[\n\r]/ or next CHARLOOP 0 0 0
770 0 undef $inlinecomment;
771 }
772
773 # in star comment
774 elsif ($instar) {
775 0 ▶ if ( ($char eq '*') && ($chars[$i] eq '/')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "*" and $chars[$i] eq "/") 0 0 0
Condition0%0/3
$char eq "*" and $chars[$i] eq "/"
!ll&&!rl&&r
000
MC/DC0%0/2
$char eq "*" and $chars[$i] eq "/"
$char eq "*" $chars[$i] eq "/"
Truth table0%0/3
A and B
A= $char eq "*"B= $chars[$i] eq "/"
ABresult
0X0
100
111
776 0 splice(@chars, $i, 1);
777 0 undef $instar;
778 }
779
780 0 next CHARLOOP;
781 }
782
783 # if char is a -, and the next character is also a -
784 ▶ elsif ( ($char eq '-') && $self->{'dash_comments'} && ($chars[$i] eq '-')) {
Condition33%2/6
$char eq "-" and $self->{"dash_comments"} and $chars[$i] eq "-"
!ll&&!rl&&r
4100
$char eq "-" and $self->{"dash_comments"}
!ll&&!rl&&r
4100
MC/DC0%0/3
$char eq "-" and $self->{"dash_comments"} and $chars[$i] eq "-"
$char eq "-" $self->{"dash_comments"} $chars[$i] eq "-"
Truth table25%1/4
A and B and C
A= $char eq "-"B= $self->{"dash_comments"}C= $chars[$i] eq "-"
ABCresult
0XX0
10X0
1100
1111
785 0 splice(@chars, $i, 1);
786 0 $inlinecomment = 1;
787 0 next CHARLOOP;
788 }
789
790 # if char is a #
791 ▶ elsif ( ($char eq '#') && $self->{'pound_comments'}) {
Condition33%1/3
$char eq "#" and $self->{"pound_comments"}
!ll&&!rl&&r
4100
MC/DC0%0/2
$char eq "#" and $self->{"pound_comments"}
$char eq "#" $self->{"pound_comments"}
Truth table33%1/3
A and B
A= $char eq "#"B= $self->{"pound_comments"}
ABresult
0X0
100
111
792 0 $inlinecomment = 1;
793 0 next CHARLOOP;
794 }
795
796 # opening /* comment
797 ▶ elsif ( ($char eq '/') && $self->{'star_comments'} && ($chars[$i] eq '*')) {
Condition33%2/6
$char eq "/" and $self->{"star_comments"} and $chars[$i] eq "*"
!ll&&!rl&&r
4100
$char eq "/" and $self->{"star_comments"}
!ll&&!rl&&r
4100
MC/DC0%0/3
$char eq "/" and $self->{"star_comments"} and $chars[$i] eq "*"
$char eq "/" $self->{"star_comments"} $chars[$i] eq "*"
Truth table25%1/4
A and B and C
A= $char eq "/"B= $self->{"star_comments"}C= $chars[$i] eq "*"
ABCresult
0XX0
10X0
1100
1111
798 0 splice(@chars, $i, 1);
799 0 $instar = 1;
800 0 next CHARLOOP;
801 }
802
803 # square brace
804 ▶ elsif ( $self->{'field_braces'} && ($char eq '[') ) {
Condition33%1/3
$self->{"field_braces"} and $char eq "["
!ll&&!rl&&r
4100
MC/DC0%0/2
$self->{"field_braces"} and $char eq "["
$self->{"field_braces"} $char eq "["
Truth table33%1/3
A and B
A= $self->{"field_braces"}B= $char eq "["
ABresult
0X0
100
111
805 0 push @major, joinfield(@field);
806 0 @field = ();
807 0 $inquote = ']';
808 }
809
810 # quote
811 elsif ($quotes{$char}) {
812 0 push @major, joinfield(@field);
813 0 @field = ();
814 0 $inquote = $char;
815 }
816
817 # regex
818 ▶ elsif ( ($char eq '=') && $self->{'perl_regex'} && $chars[$i] && ($chars[$i] eq '~')) {
Condition66%6/9
$char eq "=" and $self->{"perl_regex"} and $chars[$i] and $chars[$i] eq "~"
!ll&&!rl&&r
4010
$char eq "=" and $self->{"perl_regex"} and $chars[$i]
!ll&&!rl&&r
4001
$char eq "=" and $self->{"perl_regex"}
!ll&&!rl&&r
4001
MC/DC0%0/4
$char eq "=" and $self->{"perl_regex"} and $chars[$i] and $chars[$i] eq "~"
$char eq "=" $self->{"perl_regex"} $chars[$i] $chars[$i] eq "~"
Truth table40%2/5
A and B and C and D
A= $char eq "="B= $self->{"perl_regex"}C= $chars[$i]D= $chars[$i] eq "~"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
819 # purge everything up to here
820 0 push @field, $char, splice(@chars, $i, 1);
821 0 push @major, joinfield(@field);
822 0 @field = ();
823
824 # remove leading spaces and alphas
825 0 ▶ while ( ($i <= $#chars) && ($chars[$i] =~ m|[\sa-z]|i) )
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#chars and $chars[$i] =~ /[\sa-z]/i) 0 0 0
Condition0%0/3
$i <= $#chars and $chars[$i] =~ /[\sa-z]/i
!ll&&!rl&&r
000
MC/DC0%0/2
$i <= $#chars and $chars[$i] =~ /[\sa-z]/i
$i <= $#chars $chars[$i] =~ /[\sa-z]/i
Truth table0%0/3
A and B
A= $i <= $#charsB= $chars[$i] =~ /[\sa-z]/i
ABresult
0X0
100
111
826 0 {splice @chars, $i, 1}
827
828 # get closing character
829 0 $inquote = splice @chars, $i, 1;
830 0 $inquote =~ tr/\[\{\(/\]\}\)/;
831 0 $inregex = 1;
832 0 next CHARLOOP;
833 }
834
835 # opening paren
836 elsif ($char eq '(') {
837 0 push @major, joinfield(@field), [sql_split($self, \@chars, $carry, $i)];
838 0 @field = ();
839 0 next CHARLOOP;
840 }
841
842 # else if this is a closing paren
843 elsif ($char eq ')')
844 0 {last CHARLOOP}
845
846 # add the character to the field
847 41 push @field, $char;
848 }
849
850 # get last field
851 1 push @major, joinfield(@field);
852
853
854 # pass position back to caller
855 1 $_[IPOS] = $i;
856  
857
858 #-------------------------------------------------
859 # split by delimiters
860 #
861 1 foreach my $el (@major) {
862 # quoted strings and references don't get split
863 1 ▶ if (
Branch: 3 branches
BranchTrueFalseTotal
if (ref $el or $self->{'field_braces'} ? $el =~ /^['"\[]/ : $el =~ /^['"]/) { } 0 1 1
elsif (length $el) { } 1 0 1
$self->{'field_braces'} ? : 0 1 1
Condition33%1/3
ref $el or $self->{"field_braces"} ? $el =~ /^['"\[]/ : $el =~ /^['"]/
l!l&&r!l&&!r
001
MC/DC0%0/2
ref $el or $self->{"field_braces"} ? $el =~ /^['"\[]/ : $el =~ /^['"]/
ref $el $self->{"field_braces"} ? $el =~ /^['"\[]/ : $el =~ /^['"]/
Truth table33%1/3
A or B
A= ref $elB= $self->{"field_braces"} ? $el =~ /^['"\[]/ : $el =~ /^['"]/
ABresult
1X1
011
000
864 ref($el) ||
865 ($self->{'field_braces'} ? ($el =~ m|^['"\[]|) : ($el =~ m|^['"]|) )
866 )
867 0 {push @rv, $el}
868
869
870 elsif(length $el)
871 1 {push @rv, grep {m|\S|s} split(m/(\s|[\!\?\,]|[\d\.]+|[a-z0-9_]+|$opregex|\S+)\s*/soi, $el);}
872 }
873 #
874 # split by delimiters
875 #-------------------------------------------------
876
877
878 #-------------------------------------------------
879 # change placeholders to references
880 # lowercase elements that aren't quoted or references
881 # unquote elements
882 #
883 1 foreach my $el (@rv) {
884 8 ▶ unless ( ref($el) || ($el =~ m|^['"]|) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $el or $el =~ /^['"]/) 8 0 8
Condition33%1/3
ref $el or $el =~ /^['"]/
l!l&&r!l&&!r
008
MC/DC0%0/2
ref $el or $el =~ /^['"]/
ref $el $el =~ /^['"]/
Truth table33%1/3
A or B
A= ref $elB= $el =~ /^['"]/
ABresult
1X1
011
000
885 8 ▶ if ($el eq '?') {
Branch: 2 branches
BranchTrueFalseTotal
if ($el eq '?') { } 0 8 8
elsif ($el eq '!' and $self->{'!_is_not'}) { } 0 8 8
886 $el = {
887 placeholder=>1,
888 0 index=>scalar @{$carry->{'placeholders'}}
889 };
890
891 0 push @{$carry->{'placeholders'}}, $el;
892 }
893
894 # alias ! to not
895 ▶ elsif ( ($el eq '!') and $self->{'!_is_not'} )
Condition33%1/3
$el eq "!" and $self->{"!_is_not"}
!ll&&!rl&&r
800
MC/DC0%0/2
$el eq "!" and $self->{"!_is_not"}
$el eq "!" $self->{"!_is_not"}
Truth table33%1/3
A and B
A= $el eq "!"B= $self->{"!_is_not"}
ABresult
0X0
100
111
896 0 {$el = 'not'}
897 else
898 8 {$el =~ tr/A-Z/a-z/}
899 }
900 }
901 #
902 # change placeholders to references
903 # lowercase elements that aren't quoted or references
904 #-------------------------------------------------
905
906
907 #-------------------------------------------------
908 # compact double word tokens
909 #
910 1 $i = 0;
911 1 $dtokens = $self->{'double_word_tokens'};
912
913 1 ▶ while ($i < $#rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#rv) 7 1 8
914 7 my $el = $rv[$i];
915
916 # if this is a double_word token
917 7 ▶ unless (ref $el) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $el) 7 0 7
918 7 ▶ if ($dtokens->{$el}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dtokens->{$el}) 0 7 7
919 0 ▶ if ( (! ref($rv[$i+1])) && exists $dtokens->{$el}->{$rv[$i+1]} )
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $rv[$i + 1] and exists $dtokens->{$el}{$rv[$i + 1]}) 0 0 0
Condition0%0/3
not ref $rv[$i + 1] and exists $dtokens->{$el}{$rv[$i + 1]}
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $rv[$i + 1] and exists $dtokens->{$el}{$rv[$i + 1]}
not ref $rv[$i + 1] exists $dtokens->{$el}{$rv[$i + 1]}
Truth table0%0/3
not(A) and B
A= not ref $rv[$i + 1]B= exists $dtokens->{$el}{$rv[$i + 1]}
ABresult
0X0
100
111
920 0 {$rv[$i] = $rv[$i] . ' ' . splice(@rv, $i+1, 1)}
921 }
922 }
923
924 7 $i++;
925 }
926 #
927 # compact double word tokens
928 #-------------------------------------------------
929
930
931 # unquote if necessary
932 #if ($opts{'unquote'}) {
933 # foreach my $el (@rv)
934 # {$el = unquote($el)}
935 #}
936
937 # remove empty elements
938 1 @rv = grep {length($_)} @rv;
939
940
941 1 return @rv;
942 }
943 #
944 # sql_split
945 #------------------------------------------------------------------------------
946  
947  
948 #------------------------------------------------------------------------------
949 # misc short subs
950 #
951 1 ▶sub joinfield {
Subroutine joinfield: called CC=1 SCAR=0.0
Pod undocumented
952 1 my($val) = join('', @_);
953 1 $val =~ s|^\s+||s;
954 1 $val =~ s|\s+$||s;
955 1 return $val;
956 }
957  
958 # this could probably be done a lot more efficiently
959 0 ▶sub unquote {
Subroutine unquote: not called CC=3 SCAR=24.8
Pod undocumented
960 0 my ($rv) = @_;
961
962 # remove outer quotes
963 0 ▶ if ($rv =~ s|^'(.*)'$|$1|s)
Branch: 2 branches
BranchTrueFalseTotal
if ($rv =~ s/^'(.*)'$/$1/s) { } 0 0 0
elsif ($rv =~ s/^"(.*)"$/$1/s) { } 0 0 0
964 0 {$rv =~ s|''|'|sg}
965 elsif ($rv =~ s|^"(.*)"$|$1|s)
966 0 {$rv =~ s|""|"|sg}
967
968 # escapes
969 0 my @sets = split m|(\\.)|, $rv;
970
971 grep {
972 0 s|\\0|$nullchar|o;
973 0 s|\\z|$wineof|o;
974 0 s|\\t|\t|;
975 0 s|\\r|\r|;
976 0 s|\\n|\n|;
977 0 s|\\b|\b|;
978 0 s|\\(.)|$1|;
979 } @sets;
980
981 0 return join('', @sets);
982 }
983  
984 0 ▶sub count_ops {
Subroutine count_ops: not called CC=1 SCAR=6.9
Pod undocumented
985 return
986 0 keys(%SQL::YASP::Expr::bin) +
987 keys(%SQL::YASP::Expr::functions);
988 }
989  
990 0 ▶sub default_ops {
Subroutine default_ops: not called CC=1 SCAR=6.9
Pod undocumented
991 0 return [@SQL::YASP::Expr::dbin];
992 }
993  
994 0 ▶sub default_functions {
Subroutine default_functions: not called CC=1 SCAR=6.9
Pod undocumented
995 0 return {%SQL::YASP::Expr::dfuncs};
996 }
997  
998  
999 #
1000 # misc short subs
1001 #------------------------------------------------------------------------------
1002  
1003  
1004 #------------------------------------------------------------------------------
1005 # arr_split
1006 #
1007 # splits an array into an array of arrays
1008 #
1009 3 ▶sub arr_split {
Subroutine arr_split: called CC=13 SCAR=28.0
Pod undocumented
1010 3 my ($del_arr, $outer, %opts) = @_;
1011 3 my (@current, @rv, %dels, $firstdone);
1012 3 ▶ ref($outer) or return $outer;
Branch: 1 branch
BranchTrueFalseTotal
ref $outer or return $outer 3 0 3
1013 3 ▶ $opts{'max'} and $opts{'max'}--;
Branch: 1 branch
BranchTrueFalseTotal
$opts{"max"} and --$opts{"max"} 0 3 3
1014 3 @dels{@$del_arr} = ();
1015
1016 3 foreach my $el (@$outer) {
1017  
1018 9 ▶ if ( (! ref $el) && exists($dels{$el}) && ($opts{'max'} ? @rv<$opts{'max'} : 1) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $el and exists $dels{$el} and $opts{'max'} ? @rv < $opts{'max'} : 1) { } 2 7 9
$opts{'max'} ? : 0 2 2
Condition66%4/6
not ref $el and exists $dels{$el} and $opts{"max"} ? @rv < $opts{"max"} : 1
!ll&&!rl&&r
702
not ref $el and exists $dels{$el}
!ll&&!rl&&r
072
MC/DC33%1/3
not ref $el and exists $dels{$el} and $opts{"max"} ? @rv < $opts{"max"} : 1
not ref $el exists $dels{$el} $opts{"max"} ? @rv < $opts{"max"} : 1
Truth table50%2/4
not(A) and B and C
A= not ref $elB= exists $dels{$el}C= $opts{"max"} ? @rv < $opts{"max"} : 1
ABCresult
0XX0
10X0
1100
1111
1019 2 ▶ if ($opts{'keep_del_back'})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"keep_del_back"}) 0 2 2
1020 0 {push @current, $el}
1021
1022 2 ▶ if ($firstdone || @current)
Branch: 1 branch
BranchTrueFalseTotal
if ($firstdone or @current) 2 0 2
Condition66%2/3
$firstdone or @current
l!l&&r!l&&!r
110
MC/DC0%0/2
$firstdone or @current
$firstdone @current
Truth table66%2/3
A or B
A= $firstdoneB= @current
ABresult
1X1
011
000
1023 2 {push @rv, [@current]}
1024 2 $firstdone = 1;
1025
1026 2 @current = ();
1027
1028 2 ▶ if ($opts{'keep_del_front'})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"keep_del_front"}) 2 0 2
1029 2 {push @current, $el}
1030 }
1031 else
1032 7 {push @current, $el}
1033 }
1034
1035 # add last element
1036 3 push @rv, [@current];
1037
1038 3 ▶ wantarray and return @rv;
Branch: 1 branch
BranchTrueFalseTotal
wantarray and return @rv 3 0 3
1039 0 return \@rv;
1040 }
1041  
1042 # comma_split
1043 1 ▶sub comma_split {arr_split([','], @_)}
Subroutine comma_split: called CC=1 SCAR=0.0
Pod undocumented
1044  
1045 #
1046 # arr_split
1047 #------------------------------------------------------------------------------
1048  
1049  
1050 #------------------------------------------------------------------------------
1051 # object_list
1052 #
1053 # used for situations where the argument list is a comma delimited
1054 # list of single objects, e.g. table names
1055 #
1056 0 ▶sub object_list {
Subroutine object_list: not called CC=2 SCAR=17.9
Pod undocumented
1057 0 my @list = deref_args($_[0]);
1058
1059 0 my @rv = grep {$_ ne ','} @list;
1060 0 ▶ wantarray and return @rv;
Branch: 1 branch
BranchTrueFalseTotal
wantarray and return @rv 0 0 0
1061 0 return \@rv;
1062 }
1063 #
1064 # object_list
1065 #------------------------------------------------------------------------------
1066  
1067  
1068 #------------------------------------------------------------------------------
1069 # get_ixhash
1070 #
1071 2 ▶sub get_ixhash {
Subroutine get_ixhash: called CC=2 SCAR=7.1
Pod undocumented
1072 2 my(%hash);
1073 2 ▶ tie(%hash, 'Tie::IxHash')
Branch: 1 branch
BranchTrueFalseTotal
tie %hash, "Tie::IxHash" or die "unable to tie hash: $!" 2 0 2
1074 or die "unable to tie hash: $!";
1075 2 return \%hash;
1076 }
1077 #
1078 # get_ixhash
1079 #------------------------------------------------------------------------------
1080  
1081  
1082 #------------------------------------------------------------------------------
1083 # deref_args
1084 #
1085 2 ▶sub deref_args {
Subroutine deref_args: called CC=3 SCAR=11.0
Pod undocumented
1086 2 my @args = @_;
1087
1088 # dereference arguments
1089 2 ▶ while ( (@args == 1) && (UNIVERSAL::isa($args[0], 'ARRAY')) )
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and UNIVERSAL::isa($args[0], "ARRAY")) 1 2 3
Condition100%3/3
@args == 1 and UNIVERSAL::isa($args[0], "ARRAY")
!ll&&!rl&&r
111
MC/DC100%2/2
@args == 1 and UNIVERSAL::isa($args[0], "ARRAY")
@args == 1 UNIVERSAL::isa($args[0], "ARRAY")
Truth table100%3/3
A and B
A= @args == 1B= UNIVERSAL::isa($args[0], "ARRAY")
ABresult
0X0
100
111
1090 1 {@args = @{$args[0]}}
1091
1092 2 return @args;
1093 }
1094 #
1095 # deref_args
1096 #------------------------------------------------------------------------------
1097  
1098  
1099 #------------------------------------------------------------------------------
1100 # restring
1101 #
1102 0 ▶sub restring {
Subroutine restring: not called CC=7 SCAR=40.3
Pod undocumented
1103 0 my @args = deref_args(@_);
1104 0 my (@rv);
1105
1106 # loop through arguments
1107 0 foreach my $arg (@args) {
1108 0 ▶ if (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 0 0 0
1109 # if the arg is a placeholder
1110 0 ▶ if (UNIVERSAL::isa($arg, 'HASH') && $arg->{'placeholder'})
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, 'HASH') and $arg->{'placeholder'}) { } 0 0 0
Condition0%0/3
UNIVERSAL::isa($arg, "HASH") and $arg->{"placeholder"}
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($arg, "HASH") and $arg->{"placeholder"}
UNIVERSAL::isa($arg, "HASH") $arg->{"placeholder"}
Truth table0%0/3
A and B
A= UNIVERSAL::isa($arg, "HASH")B= $arg->{"placeholder"}
ABresult
0X0
100
111
1111 0 {push @rv, ' ?'}
1112 else
1113 0 {push @rv, '(', restring($arg), ')'}
1114 }
1115
1116 else {
1117 0 ▶ if (@rv && ($arg ne ',') )
Branch: 1 branch
BranchTrueFalseTotal
if (@rv and $arg ne ",") 0 0 0
Condition0%0/3
@rv and $arg ne ","
!ll&&!rl&&r
000
MC/DC0%0/2
@rv and $arg ne ","
@rv $arg ne ","
Truth table0%0/3
A and B
A= @rvB= $arg ne ","
ABresult
0X0
100
111
1118 0 {push @rv, ' '}
1119 0 push @rv, $arg;
1120 }
1121 }
1122
1123 0 return join('', @rv);
1124 }
1125 #
1126 # restring
1127 #------------------------------------------------------------------------------
1128  
1129  
1130 # optsref
1131 # turns option hash into anonymous hash
1132 0 ▶sub optsref{return ref($_[0]) ? $_[0] : {@_}}
Subroutine optsref: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
1133  
1134  
1135  
1136 ###############################################################################
1137 # SQL::YASP::Statement
1138 #
1139 package SQL::YASP::Statement;
1140 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1141 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
1142  
1143 #------------------------------------------------------------------------------
1144 # new
1145 #
1146 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
1147 1 my ($class, $sql) = @_;
1148
1149 1 ▶ if (defined $sql)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sql) 0 1 1
1150 0 {return SQL::YASP->parse($sql)}
1151
1152 1 return bless({}, $class);
1153 }
1154 #
1155 # new
1156 #------------------------------------------------------------------------------
1157  
1158  
1159 #------------------------------------------------------------------------------
1160 # select_fields
1161 #
1162 0 ▶sub select_fields {
Subroutine select_fields: not called CC=9 SCAR=45.0
1163 0 my ($self, %opts) = @_;
1164 0 my $rv = {};
1165 0 my $sendopts = {%opts};
1166 0 delete $sendopts->{'set'};
1167
1168 # error checking
1169 0 ▶ $opts{'db_record'} or croak 'select_fields requires a db_record argument';
Branch: 1 branch
BranchTrueFalseTotal
$opts{"db_record"} or croak("select_fields requires a db_record argument") 0 0 0
1170
1171 # loop through fields
1172 0 ▶ while (my($n, $v) = each(%{$self->{'fields'}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"fields"};}) 0 0 0
1173 0 ▶ if ($n eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n eq '*') { } 0 0 0
1174 0 ▶ while (my($on, $ov) = each(%{$opts{'db_record'}}) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$opts{"db_record"};}) 0 0 0
1175 0 {$rv->{$on} = $ov}
1176 }
1177
1178 else {
1179 0 my ($val);
1180 0 ▶ $v->evalexpr($sendopts, $val) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$v->evalexpr($sendopts, $val) or return undef 0 0 0
1181 0 $rv->{$n} = $val;
1182 }
1183 }
1184
1185 # store results
1186 0 ▶ if (exists $opts{'set'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"set"}) 0 0 0
1187 0 my $i = 1;
1188
1189 0 ▶ while ($i < @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 0 0 0
1190 0 ▶ if ($_[$i] eq 'set') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$i] eq "set") 0 0 0
1191 0 $_[$i+1] = $rv;
1192 0 return 1;
1193 }
1194
1195 0 $i+=2;
1196 }
1197 }
1198
1199 0 return $rv;
1200 }
1201 #
1202 # select_fields
1203 #------------------------------------------------------------------------------
1204  
1205  
1206 #
1207 # SQL::YASP::Statement
1208 ###############################################################################
1209  
1210  
1211  
1212 ###############################################################################
1213 # SQL::YASP::Expr
1214 #
1215 package SQL::YASP::Expr;
1216 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1217 1 ▶use Carp 'croak', 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
1218 1 ▶use vars qw[@dbin %dfuncs];
Subroutine BEGIN: called CC=1 SCAR=0.0
1219  
1220 # debug tools
1221 # use Debug::ShowStuff ':all';
1222  
1223 # comparison types
1224 1 ▶use constant CMP_AGNOSTIC => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
1225 1 ▶use constant CMP_STRING => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1226 1 ▶use constant CMP_NUMBER => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
1227  
1228 # operator precedence levels
1229 1 ▶use constant OP_BETWEEN => SQL::YASP::OP_BETWEEN;
Subroutine BEGIN: called CC=1 SCAR=0.0
1230 1 ▶use constant OP_LOGICAL => SQL::YASP::OP_LOGICAL;
Subroutine BEGIN: called CC=1 SCAR=0.0
1231 1 ▶use constant OP_ADD => SQL::YASP::OP_ADD;
Subroutine BEGIN: called CC=1 SCAR=0.0
1232 1 ▶use constant OP_MULT => SQL::YASP::OP_MULT;
Subroutine BEGIN: called CC=1 SCAR=0.0
1233 1 ▶use constant OP_EXP => SQL::YASP::OP_EXP;
Subroutine BEGIN: called CC=1 SCAR=0.0
1234 1 ▶use constant OP_MISC => SQL::YASP::OP_MISC;
Subroutine BEGIN: called CC=1 SCAR=0.0
1235  
1236 # ARGUMENT TYPES
1237 1 ▶use constant ARG_STRING => SQL::YASP::ARG_STRING;
Subroutine BEGIN: called CC=1 SCAR=0.0
1238 1 ▶use constant ARG_NONE => SQL::YASP::ARG_NONE;
Subroutine BEGIN: called CC=1 SCAR=0.0
1239 1 ▶use constant ARG_RAW => SQL::YASP::ARG_RAW;
Subroutine BEGIN: called CC=1 SCAR=0.0
1240 1 ▶use constant ARG_NUMERIC => SQL::YASP::ARG_NUMERIC;
Subroutine BEGIN: called CC=1 SCAR=0.0
1241 1 ▶use constant ARG_SENDNULLS => SQL::YASP::ARG_SENDNULLS;
Subroutine BEGIN: called CC=1 SCAR=0.0
1242  
1243 # RETURN TYPES
1244 1 ▶use constant RV_LOOSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
1245 1 ▶use constant RV_BOOL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1246  
1247 # misc constants
1248 1 ▶use constant EE_BYVAL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1249 1 ▶use constant EE_STARTARGS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
1250  
1251 # alias some subs from main class
1252 2 ▶sub deref_args{SQL::YASP::deref_args(@_)}
Subroutine deref_args: called CC=1 SCAR=0.0
1253 0 ▶sub arr_split{SQL::YASP::arr_split(@_)}
Subroutine arr_split: not called CC=1 SCAR=6.9
1254 0 ▶sub comma_split{SQL::YASP::comma_split(@_)}
Subroutine comma_split: not called CC=1 SCAR=6.9
1255 0 ▶sub unquote{SQL::YASP::unquote(@_)}
Subroutine unquote: not called CC=1 SCAR=6.9
1256 0 ▶sub restring{SQL::YASP::restring(@_)}
Subroutine restring: not called CC=1 SCAR=6.9
1257  
1258  
1259 #------------------------------------------------------------------------------
1260 # new
1261 #
1262 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
1263 2 my $class = shift;
1264 2 my $stmt = shift;
1265 2 my $self = bless({}, $class);
1266
1267 2 $self->{'parser'} = $stmt->{'parser'};
1268 2 $self->{'expr'} = [deref_args(@_)];
1269
1270 2 return $self;
1271 }
1272 #
1273 # new
1274 #------------------------------------------------------------------------------
1275  
1276  
1277 #------------------------------------------------------------------------------
1278 # evalexpr
1279 #
1280 0 ▶sub evalexpr {
Subroutine evalexpr: not called CC=71 SCAR=85.4
1281 0 my ($setval, $org_args, $opts, @args, %allops, $funcs, @oplevels, $rv, $typefix, $parser, $lukas);
1282
1283 # get first argument, which is either an array ref or an Expr object
1284 0 $org_args = $_[0];
1285
1286 # if second arg is a ref
1287 # it's the options hash ref
1288 # and the third is the value to set
1289 0 ▶ if (ref $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1]) { } 0 0 0
1290 0 $opts = $_[1];
1291 0 $setval = 2;
1292 }
1293
1294 # get the options, and find out if one of them is the set value
1295 else {
1296 0 $opts = {@_[1..$#_]};
1297
1298 0 ▶ if (exists $opts->{'set'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"set"}) 0 0 0
1299 0 my $i = 1;
1300
1301 SETLOOP:
1302 0 ▶ while ($i < @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 0 0 0
1303 0 ▶ if ($_[$i] eq 'set') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$i] eq "set") 0 0 0
1304 0 $setval = $i+1;
1305 0 last SETLOOP;
1306 }
1307
1308 0 $i+=2;
1309 }
1310 }
1311 }
1312
1313 # if first arg is a hash, then this is being done as a method call
1314 0 ▶ if (UNIVERSAL::isa($org_args, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($org_args, "HASH")) 0 0 0
1315 0 $opts->{'exprob'} = $org_args;
1316 0 $opts->{'parser'} = $org_args->{'parser'};
1317 0 $org_args = $org_args->{'expr'};
1318 }
1319
1320 # get arguments
1321 0 ▶ if (UNIVERSAL::isa($org_args, 'ARRAY'))
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($org_args, 'ARRAY')) { } 0 0 0
elsif (ref $org_args) { } 0 0 0
1322 0 {@args = $org_args}
1323 elsif (ref $org_args)
1324 0 {@args = $org_args->{'expr'}}
1325 else
1326 0 {@args = $org_args}
1327
1328 # dereference arguments
1329 0 @args = deref_args(@args);
1330
1331 # get stuff from options
1332 0 $parser = $opts->{'exprob'}->{'parser'};
1333 0 $typefix = $parser->{'type_fix'};
1334 0 $lukas = $parser->{'lukas'};
1335 0 $funcs = $parser->{'functions'};
1336 0 %allops = %{$parser->{'allops'}};
1337 0 @oplevels = @{$parser->{'ops'}};
1338
1339
1340
1341 #--------------------------------------------------------------------------
1342 # evaluate expression
1343 #
1344 EVALEXPR:
1345 {
1346 # if expression is zero items long, that's a syntax error
1347 0 ▶ if (! @args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 0 0 0
1348 0 set_err('invalid syntax: no arguments');
1349 0 last EVALEXPR;
1350 }
1351
1352 # if expression is one item long
1353 0 ▶ if (@args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) 0 0 0
1354 0 my $arg = $args[0];
1355 0 ▶ defined($arg) or die 'no $arg';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg or die "no \$arg" 0 0 0
1356
1357 # if it's a hash
1358 0 ▶ if (UNIVERSAL::isa($arg, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, "HASH")) 0 0 0
1359 # placeholder
1360 0 ▶ if ($arg->{'placeholder'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{'placeholder'}) { } 0 0 0
1361 0 ▶ if ( $opts->{'params'} && @{$opts->{'params'}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'params'} and @{$opts->{'params'};}) { } 0 0 0
Condition0%0/3
$opts->{"params"} and @{$opts->{"params"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"params"} and @{$opts->{"params"};}
$opts->{"params"} @{$opts->{"params"};}
Truth table0%0/3
A and B
A= $opts->{"params"}B= @{$opts->{"params"};}
ABresult
0X0
100
111
1362 # make sure we have a placeholder for this index
1363 0 ▶ if ($arg->{'index'} > $#{$opts->{'params'}})
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"index"} > $#{$$opts{"params"};}) 0 0 0
1364 0 {set_err('More placeholders than params')}
1365
1366 0 $rv = $opts->{'params'}->[$arg->{'index'}];
1367 }
1368
1369 else {
1370 0 set_err('Do not have any params to match placeholders');
1371 }
1372 }
1373
1374 # else just return it
1375 else
1376 0 {$rv = $arg}
1377
1378 0 last EVALEXPR;
1379 }
1380
1381 # if it's an array: should never reach this point
1382 0 ▶ if (UNIVERSAL::isa($arg, 'ARRAY'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, "ARRAY")) 0 0 0
1383 0 {croak 'got single array ref'}
1384
1385 # field name with braces
1386 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($parser->{"field_braces"} and $arg =~ /^\[.+\]$/s) 0 0 0
Condition0%0/3
$parser->{"field_braces"} and $arg =~ /^\[.+\]$/s
!ll&&!rl&&r
000
MC/DC0%0/2
$parser->{"field_braces"} and $arg =~ /^\[.+\]$/s
$parser->{"field_braces"} $arg =~ /^\[.+\]$/s
Truth table0%0/3
A and B
A= $parser->{"field_braces"}B= $arg =~ /^\[.+\]$/s
ABresult
0X0
100
111
1387 $parser->{'field_braces'} &&
1388 ($arg =~ m|^\[.+\]$|s)
1389 ) {
1390 # if no db record was sent, that's an error
1391 0 ▶ if (! $opts->{'db_record'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"db_record"}) 0 0 0
1392 0 set_err('Cannot evaluate field expression w/o database record');
1393 0 last EVALEXPR;
1394 }
1395
1396 # get field name
1397 0 my $field_name = $arg;
1398 0 $field_name =~ s|^\[(.+)\]$|$1|s;
1399
1400 # normalize
1401 0 ▶ if ($parser->{'normalize_fields'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser->{"normalize_fields"}) 0 0 0
1402 0 $field_name =~ s|^\s+||s;
1403 0 $field_name =~ s|\s+$||s;
1404 0 $field_name =~ s|\s+| |gs;
1405 0 $field_name = lc($field_name);
1406 }
1407
1408 # if the field is in the database record OR
1409 # if we can assume that any field is in the
1410 # record
1411 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'assume_fields'} or exists $opts->{'db_record'}{$field_name}) { } 0 0 0
Condition0%0/3
$opts->{"assume_fields"} or exists $opts->{"db_record"}{$field_name}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"assume_fields"} or exists $opts->{"db_record"}{$field_name}
$opts->{"assume_fields"} exists $opts->{"db_record"}{$field_name}
Truth table0%0/3
A or B
A= $opts->{"assume_fields"}B= exists $opts->{"db_record"}{$field_name}
ABresult
1X1
011
000
1412 $opts->{'assume_fields'} ||
1413 exists($opts->{'db_record'}->{$field_name})
1414 ) {
1415 0 $rv = $opts->{'db_record'}->{$field_name};
1416 }
1417
1418 # else give error that no such field is found
1419 else {
1420 0 set_err('Do not have field named ' . $field_name);
1421 }
1422
1423 0 last EVALEXPR;
1424 }
1425
1426 # function
1427 0 ▶ if ($funcs->{$arg}) {
Branch: 5 branches
BranchTrueFalseTotal
if ($funcs->{$arg}) { } 0 0 0
elsif ($opts->{'db_record'} and $parser->{'field_braces'} != 2 and exists $opts->{'db_record'}{$arg}) { } 0 0 0
elsif ($opts->{'const'} and exists $opts->{'const'}{$arg}) { } 0 0 0
elsif ($arg =~ /^['"]/) { } 0 0 0
elsif (is_numeric($arg)) { } 0 0 0
1428 0 $rv = &{$funcs->{$arg}->{'s'}}($opts);
1429 0 sbool($funcs->{$arg}, $rv);
1430 }
1431
1432 # field name w/o braces
1433 # TODO: normalize non-braced field names, mainly in terms of upper/lowercase
1434 ▶ elsif (
Condition0%0/6
$opts->{"db_record"} and $parser->{"field_braces"} != 2 and exists $opts->{"db_record"}{$arg}
!ll&&!rl&&r
000
$opts->{"db_record"} and $parser->{"field_braces"} != 2
!ll&&!rl&&r
000
MC/DC0%0/3
$opts->{"db_record"} and $parser->{"field_braces"} != 2 and exists $opts->{"db_record"}{$arg}
$opts->{"db_record"} $parser->{"field_braces"} != 2 exists $opts->{"db_record"}{$arg}
Truth table0%0/4
A and B and C
A= $opts->{"db_record"}B= $parser->{"field_braces"} != 2C= exists $opts->{"db_record"}{$arg}
ABCresult
0XX0
10X0
1100
1111
1435 $opts->{'db_record'} &&
1436 ($parser->{'field_braces'} != SQL::YASP::FIELD_BRACES_REQUIRE) &&
1437 exists($opts->{'db_record'}->{$arg})
1438 ){
1439 0 $rv = $opts->{'db_record'}->{$arg};
1440 }
1441
1442 # constant
1443 ▶ elsif ($opts->{'const'} && exists($opts->{'const'}->{$arg}))
Condition0%0/3
$opts->{"const"} and exists $opts->{"const"}{$arg}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"const"} and exists $opts->{"const"}{$arg}
$opts->{"const"} exists $opts->{"const"}{$arg}
Truth table0%0/3
A and B
A= $opts->{"const"}B= exists $opts->{"const"}{$arg}
ABresult
0X0
100
111
1444 0 {$rv = $opts->{'const'}->{$arg}}
1445
1446 # literal expression
1447 elsif ($arg =~ m|^['"]|)
1448 0 {$rv = unquote($arg)}
1449
1450 # number
1451 elsif (is_numeric($arg))
1452 0 {$rv = $arg + 0}
1453
1454 # else don't know what it is
1455 else
1456 0 {set_err('cannot interpret expression: ' . $arg)}
1457
1458 0 last EVALEXPR;
1459 }
1460
1461 # evaluate expression based on binary operators
1462 # search for loosest bound first
1463 0 foreach my $bg (@oplevels) {
1464 0 my $i = $#args - 1;
1465
1466 OPLOOP:
1467 0 ▶ while ($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 0 0 0
1468 0 my $carg = $args[$i];
1469 0 my ($not);
1470
1471 # if the current argument is a binary operator in this precedence level
1472 0 ▶ if ( (! ref $carg) && $bg->{$carg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $carg and $bg->{$carg}) 0 0 0
Condition0%0/3
not ref $carg and $bg->{$carg}
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $carg and $bg->{$carg}
not ref $carg $bg->{$carg}
Truth table0%0/3
not(A) and B
A= not ref $cargB= $bg->{$carg}
ABresult
0X0
100
111
1473
1474 # KLUDGE: if this operator is ALSO a function, and if the next
1475 # token back is an operator, then skip this operator
1476 # typical scenerio where this kludge comes into play:
1477 # rank/-2
1478 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($funcs->{$carg} and $i > 1 and exists $allops{$args[$i - 1]}) 0 0 0
Condition0%0/6
$funcs->{$carg} and $i > 1 and exists $allops{$args[$i - 1]}
!ll&&!rl&&r
000
$funcs->{$carg} and $i > 1
!ll&&!rl&&r
000
MC/DC0%0/3
$funcs->{$carg} and $i > 1 and exists $allops{$args[$i - 1]}
$funcs->{$carg} $i > 1 exists $allops{$args[$i - 1]}
Truth table0%0/4
A and B and C
A= $funcs->{$carg}B= $i > 1C= exists $allops{$args[$i - 1]}
ABCresult
0XX0
10X0
1100
1111
1479 $funcs->{$carg} &&
1480 ($i > 1) &&
1481 (exists $allops{$args[$i-1]})
1482 ) {
1483 0 $i--;
1484 0 next OPLOOP;
1485 }
1486
1487 0 my @left = @args[0..($i-1)];
1488 0 ▶ my $argtype = $bg->{$carg}->{'args'} || 0;
Condition0%0/2
$bg->{$carg}{'args'} || 0
l!l
00
MC/DC0%0/1
$bg->{$carg}{'args'} || 0
$bg->{$carg}{'args'}
Truth table0%0/2
A
A= $bg->{$carg}{'args'}
Aresult
11
00
1489 0 ▶ my $rettype = $bg->{$carg}->{'rv'} || 0;
Condition0%0/2
$bg->{$carg}{'rv'} || 0
l!l
00
MC/DC0%0/1
$bg->{$carg}{'rv'} || 0
$bg->{$carg}{'rv'}
Truth table0%0/2
A
A= $bg->{$carg}{'rv'}
Aresult
11
00
1490 0 my $sub = $bg->{$carg}->{'s'};
1491
1492 # determine if we should reverse the logical sense of the expression
1493 0 ▶ if ($left[-1] eq 'not') {
Branch: 1 branch
BranchTrueFalseTotal
if ($left[-1] eq "not") 0 0 0
1494 0 $not = 1;
1495 0 pop @left;
1496 }
1497
1498 # ARG_RAW
1499 0 ▶ if ($argtype == ARG_RAW)
Branch: 1 branch
BranchTrueFalseTotal
if ($argtype == 2) { } 0 0 0
1500 0 {$rv = &{$sub}($opts, [@left], [@args[($i+1)..$#args]])}
1501
1502 # else evaluate and send
1503 else {
1504 0 my ($a, $b);
1505
1506 0 ▶ evalexpr(\@left, $opts, $a) or last EVALEXPR;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr(\@left, $opts, $a) or last EVALEXPR 0 0 0
1507 0 ▶ evalexpr([@args[($i+1)..$#args]], $opts, $b) or last EVALEXPR;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr([@args[$i + 1 .. $#args]], $opts, $b) or last EVALEXPR 0 0 0
1508
1509 # if lukas, refuse to send nulls to operators
1510 # that don't handle them
1511 0 ▶ if ($lukas) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lukas) { } 0 0 0
elsif ($typefix) { } 0 0 0
1512 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($argtype == 4 or defined $a and defined $b) 0 0 0
Condition0%0/6
$argtype == 4 or defined $a and defined $b
l!l&&r!l&&!r
000
defined $a and defined $b
!ll&&!rl&&r
000
MC/DC0%0/3
$argtype == 4 or defined $a and defined $b
$argtype == 4 defined $a defined $b
Truth table0%0/4
A or B and C
A= $argtype == 4B= defined $aC= defined $b
ABCresult
1XX1
0111
00X0
0100
1513 ($argtype == ARG_SENDNULLS) ||
1514 (defined($a) && defined($b))
1515 ) {
1516 0 undef $rv;
1517 0 last EVALEXPR;
1518 }
1519 }
1520
1521 # elsif fix_types
1522 elsif ($typefix) {
1523 0 ▶ if ($argtype == ARG_NUMERIC) {
Branch: 2 branches
BranchTrueFalseTotal
if ($argtype == 3) { } 0 0 0
elsif ($argtype == 0) { } 0 0 0
1524 0 as_number($a);
1525 0 as_number($b);
1526 }
1527
1528 elsif ($argtype == ARG_STRING) {
1529 0 ▶ defined($a) or $a = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $a or $a = "" 0 0 0
1530 0 ▶ defined($b) or $b = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $b or $b = "" 0 0 0
1531 }
1532 }
1533
1534 # call operator subroutine
1535 0 $rv = &{$sub}($opts, $a, $b);
1536 }
1537
1538 0 ▶ ($rettype == RV_BOOL) and $rv = $rv ?1:0;
Branch: 2 branches
BranchTrueFalseTotal
$rettype == 1 and $rv = $rv ? 1 : 0 0 0 0
$rv ? : 0 0 0
1539 0 ▶ $not and $rv = lnot($rv);
Branch: 1 branch
BranchTrueFalseTotal
$not and $rv = lnot($rv) 0 0 0
1540 0 last EVALEXPR;
1541 }
1542
1543 0 $i--;
1544 }
1545 }
1546
1547 # if the first arg is a function name
1548 0 ▶ if (my $function = $funcs->{$args[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $function = $$funcs{$args[0]}) 0 0 0
1549 0 ▶ my $argtype = $function->{'args'} || 0;
Condition0%0/2
$function->{'args'} || 0
l!l
00
MC/DC0%0/1
$function->{'args'} || 0
$function->{'args'}
Truth table0%0/2
A
A= $function->{'args'}
Aresult
11
00
1550
1551 # no arguments
1552 0 ▶ if ($argtype == ARG_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argtype == 1) 0 0 0
1553 0 $rv = &{$function->{'s'}}($opts);
1554 0 last EVALEXPR;
1555 }
1556
1557 # remove first argument (which is the function name)
1558 # and deref the rest
1559 0 shift @args;
1560 0 @args = deref_args(@args);
1561
1562 # send arguments raw
1563 0 ▶ if ($argtype == ARG_RAW) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argtype == 2) 0 0 0
1564 0 $rv = &{$function->{'s'}}($opts, @args);
1565 0 last EVALEXPR;
1566 }
1567
1568 # split on commas
1569 0 @args = comma_split(\@args);
1570
1571 # evaluate arguments
1572 0 foreach my $arg (@args)
1573 0 ▶ {evalexpr($arg, $opts, $arg) or last EVALEXPR}
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($arg, $opts, $arg) or last EVALEXPR 0 0 0
1574
1575 # evaluate the arguments
1576 0 ▶ if (! $argtype) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $argtype) { } 0 0 0
elsif ($argtype == 4) { } 0 0 0
elsif ($argtype == 3) { } 0 0 0
1577 0 ▶ $typefix and grep {defined($_) or $_ = ''} @args;
Branch: 1 branch
BranchTrueFalseTotal
$typefix and grep {defined $_ or $_ = "";} @args 0 0 0
Condition0%0/2
defined $_ || ($_ = '')
l!l
00
MC/DC0%0/1
defined $_ || ($_ = '')
defined $_
Truth table0%0/2
A
A= defined $_
Aresult
11
00
1578 0 $rv = &{$function->{'s'}}($opts, @args);
1579 }
1580
1581 # same as ARG_STRING, but let undef be undef
1582 elsif ($argtype == ARG_SENDNULLS) {
1583 0 $rv = &{$function->{'s'}}($opts, @args);
1584 }
1585
1586 # same as ARG_STRING, but numify everything
1587 elsif ($argtype == ARG_NUMERIC) {
1588 0 ▶ $typefix and grep {as_number($_)} @args;
Branch: 1 branch
BranchTrueFalseTotal
$typefix and grep {as_number($_);} @args 0 0 0
1589 0 $rv = &{$function->{'s'}}($opts, @args);
1590 }
1591
1592 # else don't know argument type
1593 else
1594 0 {croak 'do not know argument type: ' . $argtype}
1595
1596
1597 0 sbool($function, $rv);
1598 0 last EVALEXPR;
1599 }
1600
1601 0 set_err('could not evaluate expression: ' . restring(@args));
1602 0 last EVALEXPR;
1603 }
1604 #
1605 # evaluate expression
1606 #--------------------------------------------------------------------------
1607
1608
1609 # if error, return undef
1610 0 ▶ if ($SQL::YASP::err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SQL::YASP::err) 0 0 0
1611 0 ▶ if (! $setval)
Branch: 1 branch
BranchTrueFalseTotal
unless ($setval) 0 0 0
1612 0 {croak 'SQL error: ' . $SQL::YASP::errstr}
1613 0 $_[$setval] = undef;
1614 0 return undef;
1615 }
1616
1617 # set byval
1618 0 ▶ if ($setval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setval) 0 0 0
1619 0 $_[$setval] = $rv;
1620 0 return 1;
1621 }
1622
1623 # return the value
1624 0 return $rv;
1625 }
1626 #
1627 # evalexpr
1628 #------------------------------------------------------------------------------
1629  
1630  
1631 #------------------------------------------------------------------------------
1632 # comparetype
1633 #
1634 0 ▶sub comparetype {
Subroutine comparetype: not called CC=23 SCAR=63.1
1635 0 my ($self, %opts) = @_;
1636
1637 # quick exit
1638 0 ▶ exists($self->{'comparetype'}) and return $self->{'comparetype'};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"comparetype"} and return $self->{"comparetype"} 0 0 0
1639
1640 0 my ($args_ref, $defs);
1641 0 my ($parser, $typefix, $lukas, $funcs, %allops, @oplevels, @args);
1642
1643 # dereference arguments
1644 0 ▶ $args_ref = $opts{'args'} || $self->{'expr'};
Condition0%0/3
$opts{'args'} || $self->{'expr'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'args'} || $self->{'expr'}
$opts{'args'} $self->{'expr'}
Truth table0%0/3
A || B
A= $opts{'args'}B= $self->{'expr'}
ABresult
1X1
011
000
1645 0 @args = deref_args($args_ref);
1646
1647 # get field definitions
1648 0 ▶ $defs = $opts{'defs'} or croak 'did not get field definitions';
Branch: 1 branch
BranchTrueFalseTotal
$defs = $opts{"defs"} or croak("did not get field definitions") 0 0 0
1649
1650 # get stuff from options
1651 0 $parser = $self->{'parser'};
1652 0 $typefix = $parser->{'type_fix'};
1653 0 $funcs = $parser->{'functions'};
1654 0 %allops = %{$parser->{'allops'}};
1655 0 @oplevels = @{$parser->{'ops'}};
1656
1657
1658 # if expression is zero items long, that's a syntax error
1659 0 ▶ if (! @args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 0 0 0
1660 0 set_err('invalid syntax: no arguments');
1661 0 last EVALEXPR;
1662 }
1663
1664 # if expression is one item long
1665 0 ▶ if (@args == 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) 0 0 0
1666 0 my $arg = $args[0];
1667 0 ▶ defined($arg) or die 'no $arg';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg or die "no \$arg" 0 0 0
1668
1669 # if it's a hash
1670 0 ▶ if (UNIVERSAL::isa($arg, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, "HASH")) 0 0 0
1671 0 return CMP_AGNOSTIC;
1672 }
1673
1674 # if it's an array: should never reach this point
1675 0 ▶ if (UNIVERSAL::isa($arg, 'ARRAY'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, "ARRAY")) 0 0 0
1676 0 {croak 'got single array ref'}
1677
1678 # function
1679 0 ▶ if ($funcs->{$arg}) {
Branch: 5 branches
BranchTrueFalseTotal
if ($funcs->{$arg}) { } 0 0 0
elsif (exists $defs->{$arg}) { } 0 0 0
elsif ($opts{'const'} and exists $opts{'const'}{$arg}) { } 0 0 0
elsif ($arg =~ /^['"]/) { } 0 0 0
elsif (is_numeric($arg)) { } 0 0 0
1680 0 my $func = $funcs->{$arg};
1681
1682 0 ▶ defined($func->{'c'}) and return $func->{'c'};
Branch: 1 branch
BranchTrueFalseTotal
defined $func->{"c"} and return $func->{"c"} 0 0 0
1683 0 return CMP_STRING;
1684 }
1685
1686 # field name
1687 elsif ( exists $defs->{$arg} )
1688 0 { return $defs->{$arg} }
1689
1690 # constant
1691 ▶ elsif ($opts{'const'} && exists($opts{'const'}->{$arg}))
Condition0%0/3
$opts{"const"} and exists $opts{"const"}{$arg}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"const"} and exists $opts{"const"}{$arg}
$opts{"const"} exists $opts{"const"}{$arg}
Truth table0%0/3
A and B
A= $opts{"const"}B= exists $opts{"const"}{$arg}
ABresult
0X0
100
111
1692 0 {return CMP_AGNOSTIC}
1693
1694 # literal expression
1695 elsif ($arg =~ m|^['"]|)
1696 0 {return CMP_AGNOSTIC}
1697
1698 # number
1699 elsif (is_numeric($arg))
1700 0 {return CMP_NUMBER}
1701
1702 # else don't know what it is
1703 else
1704 0 {set_err('cannot interpret expression: ' . $arg)}
1705
1706 0 last EVALEXPR;
1707 }
1708
1709 # evaluate expression based on binary operators
1710 # search for loosest bound first
1711 0 foreach my $bg (@oplevels) {
1712 0 my $i = $#args - 1;
1713
1714 OPLOOP:
1715 0 ▶ while ($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 0 0 0
1716 0 my $carg = $args[$i];
1717 0 my ($not);
1718
1719 # if the current argument is a binary operator in this precedence level
1720 0 ▶ if ( (! ref $carg) && $bg->{$carg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $carg and $bg->{$carg}) 0 0 0
Condition0%0/3
not ref $carg and $bg->{$carg}
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $carg and $bg->{$carg}
not ref $carg $bg->{$carg}
Truth table0%0/3
not(A) and B
A= not ref $cargB= $bg->{$carg}
ABresult
0X0
100
111
1721 0 my $subdef = $bg->{$carg};
1722 0 ▶ defined($subdef->{'c'}) and return $subdef->{'c'};
Branch: 1 branch
BranchTrueFalseTotal
defined $subdef->{"c"} and return $subdef->{"c"} 0 0 0
1723 0 return CMP_STRING;
1724 }
1725
1726 0 $i--;
1727 }
1728 }
1729
1730
1731 # if the first arg is a function name
1732 0 ▶ if (my $function = $funcs->{$args[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $function = $$funcs{$args[0]}) 0 0 0
1733 0 ▶ $function->{'c'} and return $function->{'c'};
Branch: 1 branch
BranchTrueFalseTotal
$function->{"c"} and return $function->{"c"} 0 0 0
1734 0 die 'have not implemented recursing if the function is compare type agnostic';
1735 }
1736
1737 0 set_err('could not evaluate expression: ' . restring(@args));
1738 }
1739 #
1740 # comparetype
1741 #------------------------------------------------------------------------------
1742  
1743  
1744 #------------------------------------------------------------------------------
1745 # sbool
1746 #
1747 0 ▶sub sbool {
Subroutine sbool: not called CC=4 SCAR=30.0
1748 0 my $rv = $_[0]->{'rv'};
1749 0 ▶ ($rv and ($rv==RV_BOOL)) or return;
Branch: 1 branch
BranchTrueFalseTotal
$rv and $rv == 1 or return 0 0 0
Condition0%0/3
$rv and $rv == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$rv and $rv == 1
$rv $rv == 1
Truth table0%0/3
A and B
A= $rvB= $rv == 1
ABresult
0X0
100
111
1750 0 ▶ $_[1] = $_[1] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
1751 }
1752 #
1753 # sbool
1754 #------------------------------------------------------------------------------
1755  
1756  
1757 #------------------------------------------------------------------------------
1758 # set_err
1759 #
1760 0 ▶sub set_err {
Subroutine set_err: not called CC=1 SCAR=6.9
1761 0 $SQL::YASP::err = 1;
1762 0 $SQL::YASP::errstr = $_[0];
1763 0 return undef;
1764 }
1765 #
1766 # set_err
1767 #------------------------------------------------------------------------------
1768  
1769  
1770 #------------------------------------------------------------------------------
1771 # numeric checking and conversion
1772 #
1773 0 ▶sub is_numeric {
Subroutine is_numeric: not called CC=4 SCAR=30.0
1774 0 ▶ defined($_[0]) and
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] and $_[0] =~ /^[\+\-]?\d+\.?$/s || $_[0] =~ /^[\+\-]?\d*\.\d+$/s 0 0 0
Condition0%0/9
defined $_[0] && !ref($_[0]) && ($_[0] =~ /^[\+\-]?\d+\.?$/s || $_[0] =~ /^[\+\-]?\d*\.\d+$/s)
!ll&&!rl&&r
000
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
000
$_[0] =~ /^[\+\-]?\d+\.?$/s or $_[0] =~ /^[\+\-]?\d*\.\d+$/s
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $_[0] && !ref($_[0]) && ($_[0] =~ /^[\+\-]?\d+\.?$/s || $_[0] =~ /^[\+\-]?\d*\.\d+$/s)
defined $_[0] not ref $_[0] $_[0] =~ /^[\+\-]?\d+\.?$/s $_[0] =~ /^[\+\-]?\d*\.\d+$/s
Truth table0%0/5
A and not(B) && C or D
A= defined $_[0]B= not ref $_[0]C= $_[0] =~ /^[\+\-]?\d+\.?$/sD= $_[0] =~ /^[\+\-]?\d*\.\d+$/s
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1775 (! ref $_[0]) and
1776 $_[0] =~ m|^[\+\-]?\d+\.?$|s
1777 ||
1778 $_[0] =~ m|^[\+\-]?\d*\.\d+$|s;
1779 }
1780  
1781 0 ▶sub as_number {
Subroutine as_number: not called CC=2 SCAR=17.9
1782 0 ▶ is_numeric($_[0]) or $_[0]=0;
Branch: 1 branch
BranchTrueFalseTotal
is_numeric($_[0]) or $_[0] = 0 0 0 0
1783 }
1784 #
1785 # numeric checking and conversion
1786 #------------------------------------------------------------------------------
1787  
1788  
1789  
1790  
1791 # NUM_BETWEEN
1792 1 $dbin[OP_BETWEEN]{'between'} = {s=>\&num_between, args=>ARG_RAW, c=>CMP_NUMBER};
1793 0 ▶sub num_between {
Subroutine num_between: not called CC=7 SCAR=40.3
1794 0 my ($opts, $expr, $args) = @_;
1795 0 my ($min, $max) = arr_split(['and'], $args, max=>2);
1796
1797 # $and_str must be "and"
1798 0 ▶ unless (defined($min) && defined($max))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $min and defined $max) 0 0 0
Condition0%0/3
defined $min and defined $max
!ll&&!rl&&r
000
MC/DC0%0/2
defined $min and defined $max
defined $min defined $max
Truth table0%0/3
A and B
A= defined $minB= defined $max
ABresult
0X0
100
111
1799 0 {croak 'syntax for BETWEEN: $expr BETWEEN $min AND $max'}
1800
1801 0 ▶ evalexpr($expr, $opts, $expr) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($expr, $opts, $expr) or return 0 0 0
1802 0 ▶ evalexpr($min, $opts, $min) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($min, $opts, $min) or return 0 0 0
1803 0 ▶ evalexpr($max, $opts, $max) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($max, $opts, $max) or return 0 0 0
1804 0 ($min, $max) = sort($min, $max);
1805
1806 0 ▶ return ($expr >= $min) && ($expr <= $max);
Condition0%0/3
$expr >= $min && $expr <= $max
!ll&&!rl&&r
000
MC/DC0%0/2
$expr >= $min && $expr <= $max
$expr >= $min $expr <= $max
Truth table0%0/3
A && B
A= $expr >= $minB= $expr <= $max
ABresult
0X0
100
111
1807 }
1808  
1809  
1810 # LOGICAL AND
1811 1 $dbin[OP_LOGICAL]{'and'} = {args=>ARG_RAW, s=>\&land, c=>CMP_NUMBER};
1812 0 ▶sub land {
Subroutine land: not called CC=8 SCAR=42.8
1813 0 my ($opts, $left, $right) = @_;
1814
1815 0 ▶ evalexpr($left, $opts, $left) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($left, $opts, $left) or return 0 0 0
1816
1817 0 ▶ if (defined($left) or (! $opts->{'parser'}->{'lukas'}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left or not $opts->{"parser"}{"lukas"}) 0 0 0
Condition0%0/3
defined $left or not $opts->{"parser"}{"lukas"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $left or not $opts->{"parser"}{"lukas"}
defined $left not $opts->{"parser"}{"lukas"}
Truth table0%0/3
A or not(B)
A= defined $leftB= not $opts->{"parser"}{"lukas"}
ABresult
1X1
011
000
1818 0 ▶ {$left or return $left}
Branch: 1 branch
BranchTrueFalseTotal
$left or return $left 0 0 0
1819
1820 0 ▶ evalexpr($right, $opts, $right) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($right, $opts, $right) or return 0 0 0
1821 0 ▶ $right and (! defined $left) and return undef;
Branch: 1 branch
BranchTrueFalseTotal
$right and not defined $left and return undef 0 0 0
Condition0%0/3
$right and not defined $left
!ll&&!rl&&r
000
MC/DC0%0/2
$right and not defined $left
$right not defined $left
Truth table0%0/3
A and not(B)
A= $rightB= not defined $left
ABresult
0X0
100
111
1822 0 return $right;
1823 }
1824  
1825 # LOGICAL OR
1826 0 ▶$dbin[OP_LOGICAL]{'or'} = {args=>ARG_RAW, c=>CMP_NUMBER, s=>sub{
Subroutine __ANON__: not called CC=8 SCAR=42.8
1827 0 my ($opts, $left, $right) = @_;
1828
1829 0 ▶ evalexpr($left, $opts, $left) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($left, $opts, $left) or return 0 0 0
1830 0 ▶ $left and return $left;
Branch: 1 branch
BranchTrueFalseTotal
$left and return $left 0 0 0
1831
1832 0 ▶ evalexpr($right, $opts, $right) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($right, $opts, $right) or return 0 0 0
1833 0 ▶ ($right or (! $opts->{'parser'}->{'lukas'})) and return $right;
Branch: 1 branch
BranchTrueFalseTotal
$right || !$opts->{"parser"}{"lukas"} and return $right 0 0 0
Condition0%0/3
$right or not $opts->{"parser"}{"lukas"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$right or not $opts->{"parser"}{"lukas"}
$right not $opts->{"parser"}{"lukas"}
Truth table0%0/3
A or not(B)
A= $rightB= not $opts->{"parser"}{"lukas"}
ABresult
1X1
011
000
1834  
1835 0 ▶ defined($left) and defined($right) and return $right;
Branch: 1 branch
BranchTrueFalseTotal
defined $left and defined $right and return $right 0 0 0
Condition0%0/3
defined $left and defined $right
!ll&&!rl&&r
000
MC/DC0%0/2
defined $left and defined $right
defined $left defined $right
Truth table0%0/3
A and B
A= defined $leftB= defined $right
ABresult
0X0
100
111
1836 0 return undef;
1837 1 }};
1838  
1839 # LOGICAL NAND
1840 # equivalent to "not and"
1841 0 ▶$dbin[OP_LOGICAL]{'nand'} = {args=>ARG_RAW, c=>CMP_NUMBER, s=>sub{return lnot($_[0], land(@_))}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1842  
1843 # LOGICAL NOR
1844 # returns true if both arguments are false
1845 0 ▶$dbin[OP_LOGICAL]{'nor'} = {args=>ARG_RAW, c=>CMP_NUMBER, s=>sub{
Subroutine __ANON__: not called CC=8 SCAR=42.8
1846 0 my ($opts, $left, $right) = @_;
1847
1848 0 ▶ evalexpr($left, $opts, $left) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($left, $opts, $left) or return 0 0 0
1849 0 ▶ $left and return 0;
Branch: 1 branch
BranchTrueFalseTotal
$left and return 0 0 0 0
1850
1851 0 ▶ evalexpr($right, $opts, $right) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($right, $opts, $right) or return 0 0 0
1852 0 ▶ $right and return 0;
Branch: 1 branch
BranchTrueFalseTotal
$right and return 0 0 0 0
1853
1854 0 ▶ $opts->{'parser'}->{'lukas'} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{"parser"}{"lukas"} or return 1 0 0 0
1855 0 ▶ defined($left) and defined($right) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $left and defined $right and return 1 0 0 0
Condition0%0/3
defined $left and defined $right
!ll&&!rl&&r
000
MC/DC0%0/2
defined $left and defined $right
defined $left defined $right
Truth table0%0/3
A and B
A= defined $leftB= defined $right
ABresult
0X0
100
111
1856 0 return undef;
1857 1 }};
1858  
1859 # LOGICAL XOR
1860 # returns true if truth of arguments are different
1861 0 ▶$dbin[OP_LOGICAL]{'xor'} = {s=>sub{$_[1] xor $_[2]}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=2 SCAR=14.6
Condition0%0/4
$_[1] ^^ $_[2]
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$_[1] ^^ $_[2]
$_[1] $_[2]
Truth table0%0/4
A ^^ B
A= $_[1]B= $_[2]
ABresult
110
101
011
000
1862  
1863 # LOGICAL XNOR
1864 # returns true if truth of arguments are the same
1865 0 ▶$dbin[OP_LOGICAL]{'xnor'} = {s=>sub{( $_[1] && $_[2] ) || ( (! $_[1]) && (! $_[2]) )}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=4 SCAR=28.1
Branch: 1 branch
BranchTrueFalseTotal
$_[1] and $_[2] or not $_[1] || $_[2] 0 0 0
Condition0%0/9
$_[1] && $_[2] || !($_[1] || $_[2])
l!l&&r!l&&!r
000
$_[1] and $_[2]
!ll&&!rl&&r
000
$_[1] || $_[2]
l!l&&r!l&&!r
000
MC/DC0%0/4
$_[1] && $_[2] || !($_[1] || $_[2])
$_[1] $_[2] $_[1] $_[2]
Truth table0%0/7
A and B || not(C || D)
A= $_[1]B= $_[2]C= $_[1]D= $_[2]
ABCDresult
11XX1
0X001
10001
0X1X0
0X010
101X0
10010
1866  
1867 # LIKE
1868 1 $dbin[OP_MISC]{'like'} = {s=>\&string_like, args=>ARG_RAW, rv=>RV_BOOL, c=>CMP_NUMBER};
1869 0 ▶sub string_like {
Subroutine string_like: not called CC=7 SCAR=40.3
1870 0 my ($opts, $arga, $argb, %bonusopts) = @_;
1871 0 my $esc = '\\';
1872 0 my $i = 1;
1873
1874 # evaluate $arga
1875 0 ▶ evalexpr($arga, $opts, $arga) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($arga, $opts, $arga) or return 0 0 0
1876
1877 # look for escape clause
1878 ESCAPELOOP:
1879 0 ▶ while ($i < $#{$argb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#{$argb;}) 0 0 0
1880 0 ▶ if ($argb->[$i] eq 'escape') {
Branch: 1 branch
BranchTrueFalseTotal
if ($argb->[$i] eq "escape") 0 0 0
1881 0 my @clause = splice(@{$argb}, $i+1);
1882 0 pop @{$argb};
1883 0 ▶ evalexpr(\@clause, $opts, $esc) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr(\@clause, $opts, $esc) or return 0 0 0
1884 0 last ESCAPELOOP;
1885 }
1886
1887 0 $i++;
1888 }
1889
1890 # get value of second argument
1891 0 ▶ evalexpr($argb, $opts, $argb) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($argb, $opts, $argb) or return 0 0 0
1892
1893 # substitute * for % and . for _
1894 # use Abigail's fake-look-behind technique
1895 0 $argb = reverse $argb;
1896 0 $esc = quotemeta(reverse $esc);
1897 0 $argb =~ s|\%(?!$esc)|\*\.|sg;
1898 0 $argb =~ s|_|\.|sg;
1899 0 $argb = reverse $argb;
1900
1901 # if case insensitive
1902 0 ▶ $bonusopts{'i'} and return $arga =~ m/$argb/i;
Branch: 1 branch
BranchTrueFalseTotal
$bonusopts{"i"} and return $arga =~ /$argb/i 0 0 0
1903
1904 # case sensitive
1905 0 return $arga =~ m/$argb/;
1906 }
1907  
1908  
1909 # ILIKE: case insensitive LIKE
1910 0 ▶$dbin[OP_MISC]{'ilike'} = {s=>sub{string_like(@_, i=>1)}, args=>ARG_RAW, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1911  
1912 # IS
1913 # This one's a little funky. The rules go like this:
1914 # The second batch of arguments are NOT evaluated.
1915 # There are only two possibilities of what may be
1916 # in the second array of arguments: "null", or "not null"
1917 # NULL is synonymous with UNDEF
1918 1 $dbin[OP_MISC]{'is'} = {s=>\&string_is, args=>ARG_RAW, rv=>RV_BOOL, c=>CMP_NUMBER};
1919 0 ▶sub string_is {
Subroutine string_is: not called CC=7 SCAR=40.3
1920 0 my ($opts, $arg1, $arg2_ref) = @_;
1921 0 my @arg2 = @{$arg2_ref};
1922
1923 0 ▶ evalexpr($arg1, $opts, $arg1) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($arg1, $opts, $arg1) or return 0 0 0
1924
1925 # set arg1 to true for defined and has a length
1926 0 $arg1 = defined($arg1);
1927
1928 0 ▶ if ( (@arg2 == 1) && ($arg2[0] eq 'null') )
Branch: 1 branch
BranchTrueFalseTotal
if (@arg2 == 1 and $arg2[0] eq "null") 0 0 0
Condition0%0/3
@arg2 == 1 and $arg2[0] eq "null"
!ll&&!rl&&r
000
MC/DC0%0/2
@arg2 == 1 and $arg2[0] eq "null"
@arg2 == 1 $arg2[0] eq "null"
Truth table0%0/3
A and B
A= @arg2 == 1B= $arg2[0] eq "null"
ABresult
0X0
100
111
1929 0 {return ! $arg1}
1930 0 ▶ if ( (@arg2 == 2) && ($arg2[0] eq 'not') && ($arg2[1] eq 'null') )
Branch: 1 branch
BranchTrueFalseTotal
if (@arg2 == 2 and $arg2[0] eq "not" and $arg2[1] eq "null") 0 0 0
Condition0%0/6
@arg2 == 2 and $arg2[0] eq "not" and $arg2[1] eq "null"
!ll&&!rl&&r
000
@arg2 == 2 and $arg2[0] eq "not"
!ll&&!rl&&r
000
MC/DC0%0/3
@arg2 == 2 and $arg2[0] eq "not" and $arg2[1] eq "null"
@arg2 == 2 $arg2[0] eq "not" $arg2[1] eq "null"
Truth table0%0/4
A and B and C
A= @arg2 == 2B= $arg2[0] eq "not"C= $arg2[1] eq "null"
ABCresult
0XX0
10X0
1100
1111
1931 0 {return $arg1}
1932
1933 0 croak 'syntax error: the only arguments for "is" are "null" or "not null"';
1934 }
1935  
1936  
1937 # STRING COMPARISON
1938 0 ▶$dbin[OP_MISC]{'regexp'} = {s=>sub{$_[1] =~ m/$_[2]/s}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1939 0 ▶$dbin[OP_MISC]{'iregexp'} = {s=>sub{$_[1] =~ m/$_[2]/si}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1940 0 ▶$dbin[OP_MISC]{'<=>'} = $dbin[OP_MISC]{'='} = $dbin[OP_MISC]{'eq'} = {s=>sub{$_[1] eq $_[2]}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1941 0 ▶$dbin[OP_MISC]{'ne'} = {s=>sub{$_[1] ne $_[2]}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1942 0 ▶$dbin[OP_MISC]{'lt'} = {s=>sub{$_[1] lt $_[2]}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1943 0 ▶$dbin[OP_MISC]{'gt'} = {s=>sub{$_[1] gt $_[2]}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1944 0 ▶$dbin[OP_MISC]{'eqi'} = {s=>sub{lc($_[1]) eq lc($_[2])}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1945 0 ▶$dbin[OP_MISC]{'nei'} = {s=>sub{lc($_[1]) ne lc($_[2])}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1946 0 ▶$dbin[OP_MISC]{'lti'} = {s=>sub{lc($_[1]) lt lc($_[2])}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1947 0 ▶$dbin[OP_MISC]{'gti'} = {s=>sub{lc($_[1]) gt lc($_[2])}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1948  
1949  
1950 # regular expression
1951 1 $dbin[OP_MISC]{'=~'} = {s=>\&rxmatch, rv=>RV_BOOL, c=>CMP_NUMBER};
1952 0 ▶sub rxmatch {
Subroutine rxmatch: not called CC=2 SCAR=17.9
1953 0 my ($opts, $str, $rx) = @_;
1954 0 my $not = 'xism';
1955
1956 0 ▶ $rx->{'params'} and $not =~ s|[$rx->{'params'}]||g;
Branch: 1 branch
BranchTrueFalseTotal
$rx->{"params"} and $not =~ s/[$rx->{"params"}]//g 0 0 0
1957 0 $rx = "(?$rx->{'params'}-$not:$rx->{'rx'})";
1958 0 $rx =~ s|^(\(\?[xism]{4})-|$1|s;
1959 0 $str =~ /$rx/;
1960 }
1961  
1962  
1963 # IN
1964 1 $dbin[OP_MISC]{'in'} = {s=>\&string_in, args=>ARG_RAW, rv=>RV_BOOL, c=>CMP_NUMBER};
1965 0 ▶sub string_in {
Subroutine string_in: not called CC=7 SCAR=40.3
1966 0 my ($opts, $arg1, $arg2, %bonusopts) = @_;
1967 0 my $ci = $bonusopts{'i'};
1968
1969 # get string value for argument 1
1970 0 ▶ evalexpr($arg1, $opts, $arg1) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($arg1, $opts, $arg1) or return 0 0 0
1971 0 ▶ $ci and $arg1 =~ tr/A-Z/a-z/;
Branch: 1 branch
BranchTrueFalseTotal
$ci and $arg1 =~ tr/A-Z/a-z/ 0 0 0
1972
1973 # loop through arg2 values
1974 0 foreach my $choice (comma_split([deref_args($arg2)])) {
1975 0 ▶ evalexpr($choice, $opts, $choice) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($choice, $opts, $choice) or return 0 0 0
1976 0 ▶ $ci and $choice =~ tr/A-Z/a-z/;
Branch: 1 branch
BranchTrueFalseTotal
$ci and $choice =~ tr/A-Z/a-z/ 0 0 0
1977 0 ▶ ($arg1 eq $choice) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$arg1 eq $choice and return 1 0 0 0
1978 }
1979
1980 0 return 0;
1981 }
1982  
1983 # IIN: case insensitive IN
1984 # not in MYSQL
1985 0 ▶$dbin[OP_MISC]{'iin'} = {s=>sub{return string_in(@_, i=>1)}, args=>ARG_RAW, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1986  
1987 # NUMERIC COMPARISONS
1988 0 ▶$dbin[OP_MISC]{'>'} = {s=>sub{$_[1] > $_[2]}, args=>ARG_NUMERIC, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1989 0 ▶$dbin[OP_MISC]{'<'} = {s=>sub{$_[1] < $_[2]}, args=>ARG_NUMERIC, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1990 0 ▶$dbin[OP_MISC]{'>='} = {s=>sub{$_[1] >= $_[2]}, args=>ARG_NUMERIC, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1991 0 ▶$dbin[OP_MISC]{'<='} = {s=>sub{$_[1] <= $_[2]}, args=>ARG_NUMERIC, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1992 0 ▶$dbin[OP_MISC]{'=='} = {s=>sub{$_[1] == $_[2]}, args=>ARG_NUMERIC, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1993  
1994 # NUMERIC NOT EQUAL: different than MySql, where <> is the same as !=
1995 0 ▶$dbin[OP_MISC]{'<>'} = {s=>sub{$_[1] != $_[2]}, args=>ARG_NUMERIC, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
1996  
1997  
1998 # CONCATENATION
1999 0 ▶$dbin[OP_MISC]{'||'} = {s=>sub{ (defined($_[1]) ? $_[1] : '') . (defined($_[2]) ? $_[2] : '')}, args=>ARG_SENDNULLS, c=>CMP_STRING};
Subroutine __ANON__: not called CC=3 SCAR=21.1
Branch: 2 branches
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
defined $_[2] ? : 0 0 0
2000 0 ▶$dbin[OP_MISC]{'|||'} = {args=>ARG_SENDNULLS, c=>CMP_STRING, s=>sub{
Subroutine __ANON__: not called CC=7 SCAR=40.3
2001 0 ▶ my $space = (defined($_[1]) && defined($_[2]) && ($_[1] =~ m|\S$|) && ($_[2] =~ m|^\S|) ) ? ' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] && defined $_[2] && $_[1] =~ /\S$/ && $_[2] =~ /^\S/ ? : 0 0 0
Condition0%0/9
defined $_[1] and defined $_[2] and $_[1] =~ /\S$/ and $_[2] =~ /^\S/
!ll&&!rl&&r
000
defined $_[1] and defined $_[2] and $_[1] =~ /\S$/
!ll&&!rl&&r
000
defined $_[1] and defined $_[2]
!ll&&!rl&&r
000
MC/DC0%0/4
defined $_[1] and defined $_[2] and $_[1] =~ /\S$/ and $_[2] =~ /^\S/
defined $_[1] defined $_[2] $_[1] =~ /\S$/ $_[2] =~ /^\S/
Truth table0%0/5
A and B and C and D
A= defined $_[1]B= defined $_[2]C= $_[1] =~ /\S$/D= $_[2] =~ /^\S/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2002 0 ▶ (defined($_[1]) ? $_[1] : '') . $space . (defined($_[2]) ? $_[2] : '');
Branch: 2 branches
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
defined $_[2] ? : 0 0 0
2003 1 }};
2004  
2005 # NUMERIC OPERATIONS
2006 0 ▶$dbin[OP_ADD]{'-'} = {s=>sub{$_[1] - $_[2]}, args=>ARG_NUMERIC, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2007 0 ▶$dbin[OP_ADD]{'+'} = {s=>sub{$_[1] + $_[2]}, args=>ARG_NUMERIC, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2008 0 ▶$dbin[OP_MULT]{'*'} = {s=>sub{$_[1] * $_[2]}, args=>ARG_NUMERIC, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2009 0 ▶$dbin[OP_MULT]{'%'} = {s=>sub{$_[1] % $_[2]}, args=>ARG_NUMERIC, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2010 0 ▶$dbin[OP_EXP]{'^'} = {s=>sub{$_[1] ** $_[2]}, args=>ARG_NUMERIC, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2011 0 ▶$dbin[OP_MULT]{'%'} = {args=>ARG_NUMERIC, c=>CMP_NUMBER, s=>sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
2012 0 ▶ $_[2] or return set_err('divide by zero');
Branch: 1 branch
BranchTrueFalseTotal
$_[2] or return set_err("divide by zero") 0 0 0
2013 0 $_[1] % $_[2];
2014 1 }};
2015 0 ▶$dbin[OP_MULT]{'/'} = {args=>ARG_NUMERIC, c=>CMP_NUMBER, s=>sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
2016 0 ▶ $_[2] or return set_err('divide by zero');
Branch: 1 branch
BranchTrueFalseTotal
$_[2] or return set_err("divide by zero") 0 0 0
2017 0 $_[1] / $_[2];
2018 1 }};
2019  
2020  
2021 # TOLOWER, LCASE, LOWER
2022 $dfuncs{'tolower'} =
2023 $dfuncs{'lcase'} =
2024 $dfuncs{'lower'} =
2025 0 ▶ {s=>sub{lc($_[1])}, c=>CMP_STRING};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2026  
2027 # TOUPPER, UCASE, UPPER
2028 $dfuncs{'toupper'} =
2029 $dfuncs{'ucase'} =
2030 $dfuncs{'upper'} =
2031 0 ▶ { s=>sub{uc($_[1])}, c=>CMP_STRING};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2032  
2033 # TOTITLE, TCASE, TITLE
2034 $dfuncs{'totitle'} =
2035 $dfuncs{'tcase'} =
2036 $dfuncs{'title'} = {
2037 0 ▶ s=>sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
2038 0 my $rv = lc($_[1]);
2039 0 $rv =~ s|\b(.)|\U$1|sg;
2040 0 $rv;
2041 },
2042 1 c=>CMP_STRING
2043 };
2044  
2045 # NOT: negate results
2046 1 $dfuncs{'not'} = {s=>\&lnot, args=>ARG_SENDNULLS, c=>CMP_NUMBER};
2047 0 ▶sub lnot {
Subroutine lnot: not called CC=4 SCAR=30.0
2048 0 ▶ $_[0]->{'parser'}->{'lukas'} and (! defined $_[1]) and return undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"parser"}{"lukas"} and not defined $_[1] and return undef 0 0 0
Condition0%0/3
$_[0]{"parser"}{"lukas"} and not defined $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0]{"parser"}{"lukas"} and not defined $_[1]
$_[0]{"parser"}{"lukas"} not defined $_[1]
Truth table0%0/3
A and not(B)
A= $_[0]{"parser"}{"lukas"}B= not defined $_[1]
ABresult
0X0
100
111
2049 0 ▶ return $_[1] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
2050 }
2051  
2052 # ERR: sets an error
2053 0 ▶$dfuncs{'err'} = {s=>sub{return set_err($_[1])}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2054  
2055 # ISNULL: returns true if the given value is NOT defined
2056 0 ▶$dfuncs{'isnull'} = {s=>sub{! defined $_[1]}, args=>ARG_SENDNULLS, c=>CMP_NUMBER, rv=>RV_BOOL};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2057  
2058 # DEFINED: returns true if *all* of the given values are defined
2059 # empty strings count as defined
2060 0 ▶$dfuncs{'defined'} = {args=>ARG_RAW, rv=>RV_BOOL, c=>CMP_NUMBER, s=>sub{
Subroutine __ANON__: not called CC=4 SCAR=30.0
2061 0 my ($opts, @args) = @_;
2062 0 my ($val);
2063
2064 0 foreach my $arg (comma_split(\@args)) {
2065 0 ▶ evalexpr($arg, $opts, $val) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($arg, $opts, $val) or return 0 0 0
2066 0 ▶ defined($val) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $val or return 0 0 0 0
2067 }
2068
2069 0 return 1;
2070 1 }};
2071  
2072 # HASCONTENT: returns true if the given value is defined
2073 # and has at least one non-space character
2074 0 ▶$dfuncs{'hascontent'} = {s=>sub{$_[1] =~ m|\S|}, rv=>RV_BOOL, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2075  
2076 # HASNULL: returns true if *any* the given values are null
2077 0 ▶$dfuncs{'hasnull'} = {args=>ARG_RAW, rv=>RV_BOOL, c=>CMP_NUMBER, s=>sub{
Subroutine __ANON__: not called CC=4 SCAR=30.0
2078 0 my ($opts, @args) = @_;
2079 0 my ($val);
2080
2081 0 foreach my $arg (comma_split(\@args)) {
2082 0 ▶ evalexpr($arg, $opts, $val) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($arg, $opts, $val) or return 0 0 0
2083 0 ▶ defined($val) or return 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $val or return 1 0 0 0
2084 }
2085
2086 0 return 0;
2087 1 }};
2088  
2089 # NULL, TRUE, FALSE
2090 0 ▶$dfuncs{'undef'} = $dfuncs{'null'} = {s=>sub{undef}, args=>ARG_NONE, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2091 0 ▶$dfuncs{'true'} = {s=>sub{1}, args=>ARG_NONE, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2092 0 ▶$dfuncs{'false'} = {s=>sub{0}, args=>ARG_NONE, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2093  
2094 # IF
2095 1 $dfuncs{'if'} = {s=>\&func_if, args=>ARG_RAW};
2096 0 ▶sub func_if {
Subroutine func_if: not called CC=7 SCAR=40.3
2097 0 my ($opts, @args) = @_;
2098 0 my ($expr, $true, $false) = comma_split(\@args);
2099 0 my ($val);
2100
2101 0 ▶ evalexpr($expr, $opts, $val) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($expr, $opts, $val) or return 0 0 0
2102
2103 0 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 0 0 0
2104 0 ▶ evalexpr($true, $opts, $val) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($true, $opts, $val) or return 0 0 0
2105 0 return $val
2106 }
2107
2108 0 ▶ unless ($false and @{$false})
Branch: 1 branch
BranchTrueFalseTotal
unless ($false and @{$false;}) 0 0 0
Condition0%0/3
$false and @{$false;}
!ll&&!rl&&r
000
MC/DC0%0/2
$false and @{$false;}
$false @{$false;}
Truth table0%0/3
A and B
A= $falseB= @{$false;}
ABresult
0X0
100
111
2109 0 {return undef}
2110
2111 0 ▶ evalexpr($false, $opts, $val) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($false, $opts, $val) or return 0 0 0
2112 0 return $val;
2113 }
2114  
2115  
2116 # CAT, CONCAT
2117 # returns all arguments concatenated together
2118 # Following the MySql documentation, this function returns NULL
2119 # if any argument is null. That seems a little harsh to me. If
2120 # you feel like I misread the documentation on that feel free
2121 # to drop me an email on the matter: miko@idocs.com
2122 0 ▶$dfuncs{'cat'} = $dfuncs{'concat'} = {c=>CMP_STRING, s=>sub{shift;grep {defined($_) or return undef} @_;join('', @_)}};
Subroutine __ANON__: not called CC=2 SCAR=15.1
Condition0%0/2
defined $_ || (return undef)
l!l
00
MC/DC0%0/1
defined $_ || (return undef)
defined $_
Truth table0%0/2
A
A= defined $_
Aresult
11
00
2123  
2124  
2125 # CONCAT_WS
2126 # returns all arguments concatenated together with a separator
2127 # Following the MySql documentation, this function returns NULL
2128 # if the first argument is null, but nulls after that are ignored
2129 # (not counted as part of the returned string).
2130 1 $dfuncs{'cat_ws'} = $dfuncs{'concat_ws'} = {s=>\&concat_ws, c=>CMP_STRING};
2131 0 ▶sub concat_ws {
Subroutine concat_ws: not called CC=2 SCAR=17.9
2132 0 shift;
2133 0 my ($sep, @args) = @_;
2134 0 ▶ defined($sep) or return(undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $sep or return undef 0 0 0
2135 0 return join($sep, grep {defined $_} @args);
2136 }
2137  
2138 # COALESCE
2139 1 $dfuncs{'coalesce'} = {s=>\&coalesce};
2140 0 ▶sub coalesce {
Subroutine coalesce: not called CC=3 SCAR=24.8
2141 0 shift;
2142 0 foreach (@_)
2143 0 ▶ {defined($_) and return $_}
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and return $_ 0 0 0
2144 0 return undef;
2145 }
2146  
2147 # LOAD_FILE
2148 1 $dfuncs{'load_file'} = {s=>\&load_file, c=>CMP_STRING};
2149 0 ▶sub load_file {
Subroutine load_file: not called CC=2 SCAR=17.9
2150 0 require FileHandle;
2151 0 ▶ my $fh = FileHandle->new($_[1]) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "FileHandle"->new($_[1]) or return undef 0 0 0
2152 0 return join('', <$fh>);
2153 }
2154  
2155 #------------------------------------------------------------------------------
2156 # mathematical functions
2157 #
2158  
2159 # ORD, OCT, HEX, ABS, SIGN
2160 0 ▶$dfuncs{'ord'} = {s=>sub{ord $_[1]}, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2161 0 ▶$dfuncs{'oct'} = {s=>sub{oct $_[1]}, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2162 0 ▶$dfuncs{'hex'} = {s=>sub{hex $_[1]}, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2163 0 ▶$dfuncs{'abs'} = {s=>sub{abs $_[1]}, args=>ARG_NUMERIC, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2164 0 ▶$dfuncs{'sign'} = {s=>sub{$_[1] or return 0;($_[1] > 0) ? 1 : -1;}, args=>ARG_NUMERIC, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=3 SCAR=21.6
Branch: 2 branches
BranchTrueFalseTotal
$_[1] or return 0 0 0 0
$_[1] > 0 ? : 0 0 0
2165  
2166 # MOD
2167 0 ▶$dfuncs{'mod'} = {s=>sub{$_[1] % $_[2]}, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2168  
2169 # POW, POWER
2170 0 ▶$dfuncs{'pow'} = $dfuncs{'power'} = {s=>sub{$_[1] ** $_[2]}, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2171  
2172 # FLOOR
2173 1 $dfuncs{'floor'} = {s=>\&floor, c=>CMP_NUMBER};
2174 0 ▶sub floor {
Subroutine floor: not called CC=3 SCAR=24.8
2175 0 ▶ ($_[1] >= 0) and return int($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
$_[1] >= 0 and return int $_[1] 0 0 0
2176 0 ▶ ($_[1] =~ m|\.0*[1-9]|) ? int($_[1]-1) : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
$_[1] =~ /\.0*[1-9]/ ? : 0 0 0
2177 }
2178  
2179 # CEILING
2180 1 $dfuncs{'ceil'} = $dfuncs{'ceiling'} = {s=>\&ceil, c=>CMP_NUMBER};
2181 0 ▶sub ceil {
Subroutine ceil: not called CC=3 SCAR=24.8
2182 0 ▶ ($_[1] <= 0) and return int($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
$_[1] <= 0 and return int $_[1] 0 0 0
2183 0 ▶ ($_[1] =~ m|\.0*[1-9]|) ? int($_[1]+1) : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
$_[1] =~ /\.0*[1-9]/ ? : 0 0 0
2184 }
2185  
2186 # INT
2187 0 ▶$dfuncs{'int'} = $dfuncs{'ceiling'} = {s=>sub{int($_[1])}, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2188  
2189  
2190 # SQUARE, SQUARED
2191 0 ▶$dfuncs{'square'} = $dfuncs{'squared'} = {s=>sub{$_[1] ** 2}, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2192  
2193  
2194 # unary minus
2195 0 ▶$dfuncs{'-'} = {s=>sub{$_[1] * -1}, args=>ARG_NUMERIC, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2196  
2197 # unary plus
2198 # this rather useless looking function allows us to
2199 # have expressions like this: 1/+2
2200 0 ▶$dfuncs{'+'} = {s=>sub{$_[1]}, args=>ARG_NUMERIC, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2201  
2202 #
2203 # mathematical functions
2204 #------------------------------------------------------------------------------
2205  
2206  
2207 # CHAR
2208 1 $dfuncs{'char'} = {s=>\&char, c=>CMP_STRING};
2209 0 ▶sub char {
Subroutine char: not called CC=2 SCAR=17.9
2210 0 shift;
2211 0 my(@rv);
2212 0 foreach my $el (@_)
2213 0 {push @rv, chr int $el}
2214 0 return join('', @rv);
2215 }
2216  
2217  
2218 # STRING MANIPULATION AND INFORMATION
2219 0 ▶$dfuncs{'length'} = {s=>sub{length $_[1]}, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2220 0 ▶$dfuncs{'ltrim'} = {s=>sub{$_[1] =~ s|\s+$||s;$_[1];}, c=>CMP_STRING};
Subroutine __ANON__: not called CC=1 SCAR=2.6
2221 0 ▶$dfuncs{'rtrim'} = {s=>sub{$_[1]=~s|^\s+||s;$_[1];}, c=>CMP_STRING};
Subroutine __ANON__: not called CC=1 SCAR=2.6
2222 0 ▶$dfuncs{'left'} = {s=>sub{substr($_[1],0,$_[2])}, c=>CMP_STRING};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2223 0 ▶$dfuncs{'right'} = {s=>sub{reverse(substr(reverse($_[1]), 0, $_[2]))}, c=>CMP_STRING};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2224 0 ▶$dfuncs{'reverse'} = {s=>sub{reverse($_[1])}, c=>CMP_STRING};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2225 0 ▶$dfuncs{'space'} = {s=>sub{' ' x $_[1]}, c=>CMP_STRING};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2226 0 ▶$dfuncs{'repeat'} = {s=>sub{defined($_[1]) && defined($_[2]) or return(undef);$_[1] x $_[2]}, c=>CMP_STRING};
Subroutine __ANON__: not called CC=3 SCAR=22.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] and defined $_[2] or return undef 0 0 0
Condition0%0/3
defined $_[1] and defined $_[2]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[1] and defined $_[2]
defined $_[1] defined $_[2]
Truth table0%0/3
A and B
A= defined $_[1]B= defined $_[2]
ABresult
0X0
100
111
2227 0 ▶$dfuncs{'insert'} = {s=>sub{substr($_[1], $_[2]-1, $_[3]) = $_[4];$_[1]}, c=>CMP_STRING};
Subroutine __ANON__: not called CC=1 SCAR=2.6
2228  
2229  
2230 # REPLACE
2231 1 $dfuncs{'replace'} = {s=>\&replace, c=>CMP_STRING};
2232 0 ▶sub replace {
Subroutine replace: not called CC=1 SCAR=6.9
2233 0 shift;
2234 0 my ($str, $from, $to) = @_;
2235 0 $from = quotemeta($from);
2236 0 $str =~ s/$from/$to/i;
2237 0 $str;
2238 }
2239  
2240 # QUOTE
2241 # needs to be fixed, doesn't quote enough stuff
2242 # $dfuncs{'quote'} = {s=>sub{my($v)=@_;$v =~ s|'|\\'|gs;$v}, c=>CMP_STRING};
2243  
2244  
2245 # SOUNDEX
2246 # this function returns shorter values than the
2247 # MySql documentation, so this function may not work as expected
2248 0 ▶$dfuncs{'soundex'} = {s=>sub{require Text::Soundex;Text::Soundex::soundex($_[1])}, c=>CMP_STRING};
Subroutine __ANON__: not called CC=1 SCAR=2.6
2249  
2250 # STRCMP
2251 0 ▶$dfuncs{'strcmp'} = $dfuncs{'cmp'} = {s=>sub{$_[1] cmp $_[2]}, c=>CMP_NUMBER};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2252  
2253 # LOCATE and friends
2254 1 $dfuncs{'locate'} = $dfuncs{'position'} = {s=>\&locate, c=>CMP_NUMBER};
2255 0 ▶$dfuncs{'instr'} = {s=>sub{locate(@_[2,1,3])}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
2256 0 ▶sub locate {
Subroutine locate: not called CC=2 SCAR=17.9
2257 0 ▶ $_[3] ||= 1;
Condition0%0/2
$_[3] ||= 1
l!l
00
MC/DC0%0/1
$_[3] ||= 1
$_[3]
Truth table0%0/2
A
A= $_[3]
Aresult
11
00
2258 0 index(lc($_[2]), lc($_[1]), $_[3]-1)+1;
2259 }
2260  
2261 # CRUNCH
2262 # remove leading and trailing spaces,
2263 # reduce internal contigous spaces to single spaces
2264 1 $dfuncs{'crunch'} = {s=>\&crunch, c=>CMP_STRING};
2265 0 ▶sub crunch {
Subroutine crunch: not called CC=1 SCAR=6.9
2266 0 my $rv = $_[1];
2267 0 $rv =~ s|^\s+||s;
2268 0 $rv =~ s|\s+$||s;
2269 0 $rv =~ s|\s+| |sg;
2270 0 $rv;
2271 }
2272  
2273 # TRIM
2274 # syntax: TRIM([[BOTH | LEADING | TRAILING] [remstr] FROM] str)
2275 1 $dfuncs{'trim'} = {s=>\&trim, args=>ARG_RAW, c=>CMP_STRING};
2276 0 ▶sub trim {
Subroutine trim: not called CC=14 SCAR=53.5
2277 0 shift;
2278 0 my ($opts, @args) = @_;
2279 0 my ($leading, $trailing, $next, $left, $str, $regex);
2280
2281 # get before and after FROM
2282 0 ($left, $str) = arr_split(['from'], @args);
2283
2284 # early exit: no FROM, so just trim and return
2285 0 ▶ if (! $str) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str) 0 0 0
2286 0 ▶ evalexpr($left, $opts, $str) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($left, $opts, $str) or return 0 0 0
2287 0 $str =~ s|^\s+||s;
2288 0 $str =~ s|\s+$||s;
2289 0 return $str;
2290 }
2291
2292 0 ▶ evalexpr($str, $opts, $str) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($str, $opts, $str) or return 0 0 0
2293 0 @args = @$left;
2294
2295 # determine leading and trailing trim actions
2296 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (@args and $args[0] =~ /^(both|leading|trailing)$/) 0 0 0
Condition0%0/3
@args and $args[0] =~ /^(both|leading|trailing)$/
!ll&&!rl&&r
000
MC/DC0%0/2
@args and $args[0] =~ /^(both|leading|trailing)$/
@args $args[0] =~ /^(both|leading|trailing)$/
Truth table0%0/3
A and B
A= @argsB= $args[0] =~ /^(both|leading|trailing)$/
ABresult
0X0
100
111
2297 @args &&
2298 ($args[0] =~ m/^(both|leading|trailing)$/)
2299 ) {
2300 0 ▶ $leading ||= $args[0] =~ m/^(both|leading)$/;
Condition0%0/3
$leading ||= $args[0] =~ /^(both|leading)$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$leading ||= $args[0] =~ /^(both|leading)$/
$leading $args[0] =~ /^(both|leading)$/
Truth table0%0/3
A ||= B
A= $leadingB= $args[0] =~ /^(both|leading)$/
ABresult
1X1
011
000
2301 0 ▶ $trailing ||= $args[0] =~ m/^(both|trailing)$/;
Condition0%0/3
$trailing ||= $args[0] =~ /^(both|trailing)$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$trailing ||= $args[0] =~ /^(both|trailing)$/
$trailing $args[0] =~ /^(both|trailing)$/
Truth table0%0/3
A ||= B
A= $trailingB= $args[0] =~ /^(both|trailing)$/
ABresult
1X1
011
000
2302 0 shift @args;
2303 }
2304
2305 # "If none of the specifiers BOTH, LEADING or TRAILING are given, BOTH is assumed."
2306 # -- MySql docs
2307 0 ▶ unless ($leading || $trailing)
Branch: 1 branch
BranchTrueFalseTotal
unless ($leading or $trailing) 0 0 0
Condition0%0/3
$leading or $trailing
l!l&&r!l&&!r
000
MC/DC0%0/2
$leading or $trailing
$leading $trailing
Truth table0%0/3
A or B
A= $leadingB= $trailing
ABresult
1X1
011
000
2308 0 {$leading = $trailing = 1}
2309
2310 # left defaults to \s
2311 0 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 0 0 0
2312 0 ▶ evalexpr(\@args, $opts, $regex) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr(\@args, $opts, $regex) or return 0 0 0
2313 0 $regex = quotemeta($regex);
2314 }
2315 else
2316 0 {$regex = '\s'}
2317
2318 0 ▶ $leading and $str =~ s/^($regex)+//s;
Branch: 1 branch
BranchTrueFalseTotal
$leading and $str =~ s/^($regex)+//s 0 0 0
2319 0 ▶ $trailing and $str =~ s/($regex)+$//s;
Branch: 1 branch
BranchTrueFalseTotal
$trailing and $str =~ s/($regex)+$//s 0 0 0
2320 0 return $str;
2321 }
2322  
2323  
2324 # LPAD
2325 1 $dfuncs{'lpad'} = {s=>\&lpad, c=>CMP_STRING};
2326 0 ▶sub lpad {
Subroutine lpad: not called CC=4 SCAR=30.0
2327 0 shift;
2328 0 my @str = split('', shift);
2329 0 my $len = shift;
2330 0 my @padstr = split('', shift);
2331 0 ▶ @padstr or @padstr = (' ');
Branch: 1 branch
BranchTrueFalseTotal
@padstr or @padstr = " " 0 0 0
2332
2333 0 ▶ while (@str < $len)
Branch: 1 branch
BranchTrueFalseTotal
if (@str < $len) 0 0 0
2334 0 {unshift @str, @padstr}
2335 0 ▶ while (@str > $len)
Branch: 1 branch
BranchTrueFalseTotal
if (@str > $len) 0 0 0
2336 0 {shift @str}
2337
2338 0 return join('', @str);
2339 }
2340  
2341  
2342 # RPAD
2343 1 $dfuncs{'rpad'} = {s=>\&rpad, c=>CMP_STRING};
2344 0 ▶sub rpad {
Subroutine rpad: not called CC=4 SCAR=30.0
2345 0 shift;
2346 0 my @str = split('', shift);
2347 0 my $len = shift;
2348 0 my @padstr = split('', shift);
2349 0 ▶ @padstr or @padstr = (' ');
Branch: 1 branch
BranchTrueFalseTotal
@padstr or @padstr = " " 0 0 0
2350
2351 0 ▶ while (@str < $len)
Branch: 1 branch
BranchTrueFalseTotal
if (@str < $len) 0 0 0
2352 0 {push @str, @padstr}
2353 0 ▶ while (@str > $len)
Branch: 1 branch
BranchTrueFalseTotal
if (@str > $len) 0 0 0
2354 0 {pop @str}
2355
2356 0 return join('', @str);
2357 }
2358  
2359  
2360 # SUBSTRING
2361 $dfuncs{'substring'} =
2362 $dfuncs{'mid'} =
2363 1 $dfuncs{'substr'} =
2364 {s=>\&substring, args=>ARG_RAW, c=>CMP_STRING};
2365 0 ▶sub substring {
Subroutine substring: not called CC=5 SCAR=34.0
2366 0 my ($opts, @args) = @_;
2367 0 my ($str, $pos, $len) = arr_split([',', 'from', 'for'], @args);
2368 0 ▶ evalexpr($str, $opts, $str) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($str, $opts, $str) or return 0 0 0
2369 0 ▶ evalexpr($pos, $opts, $pos) or return;
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($pos, $opts, $pos) or return 0 0 0
2370
2371 0 ▶ if ($len)
Branch: 1 branch
BranchTrueFalseTotal
if ($len) { } 0 0 0
2372 0 ▶ {evalexpr($len, $opts, $len) or return}
Branch: 1 branch
BranchTrueFalseTotal
evalexpr($len, $opts, $len) or return 0 0 0
2373 else
2374 0 {$len = length($str)}
2375
2376 0 return substr($str, $pos-1, $len);
2377 }
2378  
2379  
2380 # SUBSTRING_INDEX
2381 1 $dfuncs{'substring_index'} = {s=>\&substring_index, c=>CMP_STRING};
2382 0 ▶sub substring_index {
Subroutine substring_index: not called CC=5 SCAR=34.0
2383 0 shift;
2384 0 my ($str, $del, $count) = @_;
2385 0 my (@arr, $reverse, $del_esc);
2386
2387 0 $del_esc = quotemeta($del);
2388
2389 0 ▶ if ($count < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 0) 0 0 0
2390 0 $reverse = 1;
2391 0 $count *= -1;
2392 }
2393
2394 0 @arr = split($del_esc, $str);
2395 0 ▶ $reverse and @arr = reverse @arr;
Branch: 1 branch
BranchTrueFalseTotal
$reverse and @arr = reverse @arr 0 0 0
2396
2397 0 ▶ if (@arr > $count)
Branch: 1 branch
BranchTrueFalseTotal
if (@arr > $count) 0 0 0
2398 0 {@arr = @arr[0..($count-1)]}
2399
2400 0 ▶ $reverse and @arr = reverse @arr;
Branch: 1 branch
BranchTrueFalseTotal
$reverse and @arr = reverse @arr 0 0 0
2401 0 return join($del, @arr);
2402 }
2403  
2404  
2405 # ELT
2406 1 $dfuncs{'elt'} = {s=>\&elt, c=>CMP_AGNOSTIC};
2407 0 ▶sub elt {
Subroutine elt: not called CC=1 SCAR=6.9
2408 0 shift;
2409 0 my $val=shift;
2410 0 return $_[$val-1];
2411 }
2412  
2413 # FIELD
2414 1 $dfuncs{'field'} = {s=>\&field, c=>CMP_AGNOSTIC};
2415 0 ▶sub field {
Subroutine field: not called CC=3 SCAR=24.8
2416 0 shift;
2417 0 my $val=lc(shift);
2418 0 my $i = 0;
2419
2420 0 ▶ while ($i <= $#_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#_) 0 0 0
2421 0 ▶ if (lc($_[$i]) eq $val)
Branch: 1 branch
BranchTrueFalseTotal
if (lc $_[$i] eq $val) 0 0 0
2422 0 {return $i+1}
2423 0 $i++;
2424 }
2425
2426 0 return undef;
2427 }
2428  
2429 #
2430 # SQL::YASP::Expr
2431 ###############################################################################
2432  
2433  
2434 # return true;
2435 1 1;
2436  
2437 __END__
↑ Summary