Statement/Function.pm

filter: Statementstmt 92.0% 128 / 139 Branchbran 50.0% 14 / 28 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 96.4% 27 / 28 Podpod n/a totaltotal 87.0% 175 / 201 CC 19 SCAR 15.5
CC
19
Cov
85%
new
16.0
new
6.0
value
4.0
CRAP 4.7
« Statement/Functions.pm ↑ Summary Statement/Term.pm »
1 package SQL::Statement::Function;
2  
3 ######################################################################
4 #
5 # This module is copyright (c), 2009-2020 by Jens Rehsack.
6 # All rights reserved.
7 #
8 # It may be freely distributed under the same terms as Perl itself.
9 # See below for help and copyright information (search for SYNOPSIS).
10 #
11 ######################################################################
12  
13 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use warnings FATAL => "all";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 16 ▶use SQL::Statement::Term ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 @ISA = qw(SQL::Statement::Term);
19  
20 16 $VERSION = '1.414';
21  
22 =pod
23  
24 =head1 NAME
25  
26 SQL::Statement::Function - abstract base class for all function executing terms
27  
28 =head1 SYNOPSIS
29  
30 # this class does not have a common constructor, because all derived classes
31 # have their special requirements
32  
33 =head1 DESCRIPTION
34  
35 SQL::Statement::Function is an abstract base class providing the interface
36 for all function executing terms.
37  
38 =head1 INHERITANCE
39  
40 SQL::Statement::Function
41 ISA SQL::Statement::Term
42  
43 =head1 METHODS
44  
45 =head2 DESTROY
46  
47 Destroys the term and undefines the weak reference to the owner as well
48 as the reference to the parameter list.
49  
50 =cut
51  
52 sub DESTROY
53 332 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
54 332 my $self = $_[0];
55  
56 332 undef $self->{PARAMS};
57  
58 332 $self->SUPER::DESTROY();
59 }
60  
61 package SQL::Statement::Function::UserFunc;
62  
63 16 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 16 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
66 16 ▶use Params::Util qw(_INSTANCE);
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 16 ▶use SQL::Statement::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 16 @ISA = qw(SQL::Statement::Function);
71  
72 =pod
73  
74 =head1 NAME
75  
76 SQL::Statement::Function::UserFunc - implements executing a perl subroutine
77  
78 =head1 SYNOPSIS
79  
80 # create an user function term with an SQL::Statement object as owner,
81 # specifying the function name, the subroutine name (full qualified)
82 # and the parameters to the subroutine
83 my $term = SQL::Statement::Function::UserFunc->new( $owner, $name, $sub, \@params );
84 # access the result of that operation
85 $term->value( $eval );
86  
87 =head1 DESCRIPTION
88  
89 SQL::Statement::Function::UserFunc implements a term which returns the result
90 of the specified subroutine.
91  
92 =head1 INHERITANCE
93  
94 SQL::Statement::Function
95 ISA SQL::Statement::Term
96  
97 =head1 METHODS
98  
99 =head2 new
100  
101 Instantiates a new C<SQL::Statement::Function::UserFunc> instance.
102  
103 =head2 value
104  
105 Invokes the given subroutine with the values of the params and return it's
106 result:
107  
108 my @params = map { $_->value($eval); } @{ $self->{PARAMS} };
109 return $subpkg->$subname( $self->{OWNER}, @params );
110  
111 =cut
112  
113 sub new
114 271 ▶{
Subroutine new: called CC=8 SCAR=27.7
115 271 my ( $class, $owner, $name, $subnm, $params ) = @_;
116  
117 271 my $self = $class->SUPER::new($owner);
118  
119 271 my ( $pkg, $sub ) = $subnm =~ m/^(.*::)([^:]+$)/;
120 271 ▶ if ( !$sub )
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub) 2 269 271
121 {
122 2 $sub = $subnm;
123 2 $pkg = 'main';
124 }
125 271 $pkg =~ s/::$//g;
126 271 ▶ $pkg = 'main' unless ($pkg);
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg 0 271 271
127  
128 271 $self->{SUB} = $sub;
129 271 $self->{PKG} = $pkg;
130 271 $self->{NAME} = $name;
131 271 $self->{PARAMS} = $params;
132  
133 271 ▶ unless ( UNIVERSAL::can( $pkg, $sub ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::can($pkg, $sub)) 0 271 271
134 {
135 0 ▶ unless ( 'main' eq $pkg )
Branch: 1 branch
BranchTrueFalseTotal
unless ("main" eq $pkg) 0 0 0
136 {
137 0 my $mod = $pkg;
138 0 $mod =~ s|::|/|g;
139 0 $mod .= '.pm';
140 0 ▶ eval { require $mod; } unless ( defined( $INC{$mod} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $INC{$mod} 0 0 0
141 0 ▶ return $owner->do_err($@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
142 }
143  
144 0 ▶ $pkg->can($sub) or return $owner->do_err( "Can't find subroutine $pkg" . "::$sub" );
Branch: 1 branch
BranchTrueFalseTotal
$pkg->can($sub) or return $owner->do_err("Can't find subroutine $pkg" . "::$sub") 0 0 0
145 }
146  
147 271 return $self;
148 }
149  
150 sub value($)
151 240 ▶{
Subroutine value: called CC=1 SCAR=0.0
152 240 my $self = $_[0];
153 240 my $eval = $_[1];
154 240 my $pkg = $self->{PKG};
155 240 my $sub = $self->{SUB};
156 240 my @params = map { $_->value($eval); } @{ $self->{PARAMS} };
157 240 return $pkg->$sub( $self->{OWNER}, @params ); # FIXME is $pkg just a string?
158 }
159  
160 package SQL::Statement::Function::NumericEval;
161  
162 16 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
163  
164 16 ▶use Params::Util qw(_NUMBER _INSTANCE);
Subroutine BEGIN: called CC=1 SCAR=0.0
165  
166 16 @ISA = qw(SQL::Statement::Function);
167  
168 =pod
169  
170 =head1 NAME
171  
172 SQL::Statement::Function::NumericEval - implements numeric evaluation of a term
173  
174 =head1 SYNOPSIS
175  
176 # create an user function term with an SQL::Statement object as owner,
177 # specifying the expression to evaluate and the parameters to the subroutine
178 my $term = SQL::Statement::NumericEval->new( $owner, $expr, \@params );
179 # access the result of that operation
180 $term->value( $eval );
181  
182 =head1 DESCRIPTION
183  
184 SQL::Statement::Function::NumericEval implements the numeric evaluation of a
185 term. All parameters are expected to be numeric.
186  
187 =head1 INHERITANCE
188  
189 SQL::Statement::Function::NumericEval
190 ISA SQL::Statement::Function
191 ISA SQL::Statement::Term
192  
193 =head1 METHODS
194  
195 =head2 new
196  
197 Instantiates a new C<SQL::Statement::Function::NumericEval> instance.
198 Takes I<$owner>, I<$expr> and I<\@params> as arguments (in specified order).
199  
200 =head2 value
201  
202 Returns the result of the evaluated expression.
203  
204 =cut
205  
206 sub new
207 41 ▶{
Subroutine new: called CC=1 SCAR=0.0
208 41 my ( $class, $owner, $expr, $params ) = @_;
209  
210 41 my $self = $class->SUPER::new($owner);
211  
212 41 $self->{EXPR} = $expr;
213 41 $self->{PARAMS} = $params;
214  
215 41 return $self;
216 }
217  
218 sub value($)
219 171 ▶{
Subroutine value: called CC=4 SCAR=14.0
220 171 my ( $self, $eval ) = @_;
221 my @vals =
222 171 ▶ map { _INSTANCE( $_, 'SQL::Statement::Term' ) ? $_->value($eval) : $_ } @{ $self->{PARAMS} };
Branch: 1 branch
BranchTrueFalseTotal
&_INSTANCE($_, 'SQL::Statement::Term') ? : 395 0 395
223 171 foreach my $val (@vals)
224 {
225 395 ▶ return $self->{OWNER}->do_err(qq~Bad numeric expression '$val'!~)
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_NUMBER($val) 0 395 395
226 unless ( defined( _NUMBER($val) ) );
227 }
228 171 my $expr = $self->{EXPR};
229 171 $expr =~ s/\?(\d+)\?/$vals[$1]/g;
230 171 $expr =~ s/\s//g;
231 171 $expr =~ s/^([\)\(+\-\*\/\%0-9]+)$/$1/; # untaint
232 171 return eval $expr;
233 }
234  
235 package SQL::Statement::Function::Trim;
236  
237 16 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
238  
239 16 ▶BEGIN { @ISA = qw(SQL::Statement::Function); }
Subroutine BEGIN: called CC=1 SCAR=0.0
240  
241 =pod
242  
243 =head1 NAME
244  
245 SQL::Statement::Function::Trim - implements the built-in trim function support
246  
247 =head1 SYNOPSIS
248  
249 # create an trim function term with an SQL::Statement object as owner,
250 # specifying the spec, char and the parameters to the subroutine
251 my $term = SQL::Statement::Trim->new( $owner, $spec, $char, \@params );
252 # access the result of that operation
253 $term->value( $eval );
254  
255 =head1 DESCRIPTION
256  
257 SQL::Statement::Function::Trim implements string trimming.
258  
259 =head1 INHERITANCE
260  
261 SQL::Statement::Function::Trim
262 ISA SQL::Statement::Function
263 ISA SQL::Statement::Term
264  
265 =head1 METHODS
266  
267 =head2 new
268  
269 Instantiates a new C<SQL::Statement::Function::Trim> instance.
270 Takes I<$owner>, I<$spec>, I<$char> and I<\@params> as arguments
271 (in specified order).
272  
273 Meaning of the parameters:
274  
275 =over 4
276  
277 =item I<$spec>
278  
279 Can be on of 'LEADING', 'TRAILING' 'BOTH'. Trims the leading chars, trailing
280 chars or at both ends, respectively.
281  
282 Defaults to 'BOTH'.
283  
284 =item I<$char>
285  
286 The character to trim - defaults to C<' '>
287  
288 =item I<\@params>
289  
290 Expected to be an array with exact 1 element (more are not evaluated).
291  
292 =back
293  
294 =head2 value
295  
296 Returns the trimmed value of first parameter argument.
297  
298 =cut
299  
300 sub new
301 10 ▶{
Subroutine new: called CC=6 SCAR=18.0
302 10 my ( $class, $owner, $spec, $char, $params ) = @_;
303 10 ▶ $spec ||= 'BOTH';
Condition100%2/2
$spec ||= "BOTH"
l!l
55
MC/DC100%1/1
$spec ||= "BOTH"
$spec
Truth table100%2/2
A
A= $spec
Aresult
11
00
304 10 ▶ $char ||= ' ';
Condition100%2/2
$char ||= " "
l!l
28
MC/DC100%1/1
$char ||= " "
$char
Truth table100%2/2
A
A= $char
Aresult
11
00
305  
306 10 my $self = $class->SUPER::new($owner);
307  
308 10 $self->{PARAMS} = $params;
309 1 ▶ $self->{TRIMFN} = sub { my $s = $_[0]; $s =~ s/^$char*//g; return $s; }
Subroutine __ANON__: called CC=1 SCAR=0.0
310 10 ▶ if ( $spec =~ m/LEADING/ );
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /LEADING/ 3 7 10
311 0 ▶ $self->{TRIMFN} = sub { my $s = $_[0]; $s =~ s/$char*$//g; return $s; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
312 10 ▶ if ( $spec =~ m/TRAILING/ );
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /TRAILING/ 1 9 10
313 2 ▶ $self->{TRIMFN} = sub { my $s = $_[0]; $s =~ s/^$char*//g; $s =~ s/$char*$//g; return $s; }
Subroutine __ANON__: called CC=1 SCAR=0.0
314 10 ▶ if ( $spec =~ m/BOTH/ );
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /BOTH/ 6 4 10
315  
316 10 return $self;
317 }
318  
319 sub value($)
320 3 ▶{
Subroutine value: called CC=1 SCAR=0.0
321 3 my $val = $_[0]->{PARAMS}->[0]->value( $_[1] );
322 3 $val = &{ $_[0]->{TRIMFN} }($val);
323 3 return $val;
324 }
325  
326 package SQL::Statement::Function::SubString;
327  
328 16 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
329  
330 16 @ISA = qw(SQL::Statement::Function);
331  
332 =pod
333  
334 =head1 NAME
335  
336 SQL::Statement::Function::SubString - implements the built-in sub-string function support
337  
338 =head1 SYNOPSIS
339  
340 # create an substr function term with an SQL::Statement object as owner,
341 # specifying the start and length of the sub string to extract from the
342 # first element of \@params
343 my $term = SQL::Statement::SubString->new( $owner, $start, $length, \@params );
344 # access the result of that operation
345 $term->value( $eval );
346  
347 =head1 DESCRIPTION
348  
349 SQL::Statement::Function::SubString implements a sub-string extraction term.
350  
351 =head1 INHERITANCE
352  
353 SQL::Statement::Function::SubString
354 ISA SQL::Statement::Function
355 ISA SQL::Statement::Term
356  
357 =head1 METHODS
358  
359 =head2 new
360  
361 Instantiates a new C<SQL::Statement::Function::SubString> instance.
362 Takes I<$owner>, I<$start>, I<$length> and I<\@params> as arguments
363 (in specified order).
364  
365 Meaning of the parameters:
366  
367 =over 4
368  
369 =item I<$start>
370  
371 Specifies the start position to extract the sub-string. This is expected
372 to be a L<SQL::Statement::Term> instance. The first character in a string
373 has the position 1.
374  
375 =item I<$length>
376  
377 Specifies the length of the extracted sub-string. This is expected
378 to be a L<SQL::Statement::Term> instance.
379  
380 If omitted, everything to the end of the string is returned.
381  
382 =item I<\@params>
383  
384 Expected to be an array with exact 1 element (more are not evaluated).
385  
386 =back
387  
388 =head2 value
389  
390 Returns the extracted sub-string value from first parameter argument.
391  
392 =cut
393  
394 sub new
395 3 ▶{
Subroutine new: called CC=1 SCAR=0.0
396 3 my ( $class, $owner, $start, $length, $params ) = @_;
397  
398 3 my $self = $class->SUPER::new($owner);
399  
400 3 $self->{START} = $start;
401 3 $self->{LENGTH} = $length;
402 3 $self->{PARAMS} = $params;
403  
404 3 return $self;
405 }
406  
407 sub value($)
408 5 ▶{
Subroutine value: called CC=2 SCAR=7.0
409 5 my $val = $_[0]->{PARAMS}->[0]->value( $_[1] );
410 5 my $start = $_[0]->{START}->value( $_[1] ) - 1;
411 my $length =
412 5 ▶ defined( $_[0]->{LENGTH} ) ? $_[0]->{LENGTH}->value( $_[1] ) : length($val) - $start;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'LENGTH'} ? : 5 0 5
413 5 return substr( $val, $start, $length );
414 }
415  
416 package SQL::Statement::Function::StrConcat;
417  
418 16 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
419  
420 16 @ISA = qw(SQL::Statement::Function);
421  
422 =pod
423  
424 =head1 NAME
425  
426 SQL::Statement::Function::StrConcat - implements the built-in string concatenation
427  
428 =head1 SYNOPSIS
429  
430 # create an substr function term with an SQL::Statement object as owner
431 # and \@params to concatenate
432 my $term = SQL::Statement::StrConcat->new( $owner, \@params );
433 # access the result of that operation
434 $term->value( $eval );
435  
436 =head1 DESCRIPTION
437  
438 SQL::Statement::Function::StrConcat implements a string concatenation term.
439  
440 =head1 INHERITANCE
441  
442 SQL::Statement::Function::StrConcat
443 ISA SQL::Statement::Function
444 ISA SQL::Statement::Term
445  
446 =head1 METHODS
447  
448 =head2 new
449  
450 Instantiates a new C<SQL::Statement::Function::StrConcat> instance.
451  
452 =head2 value
453  
454 Returns the concatenated string composed of the parameter values.
455  
456 =cut
457  
458 sub new
459 7 ▶{
Subroutine new: called CC=1 SCAR=0.0
460 7 my ( $class, $owner, $params ) = @_;
461  
462 7 my $self = $class->SUPER::new($owner);
463  
464 7 $self->{PARAMS} = $params;
465  
466 7 return $self;
467 }
468  
469 sub value($)
470 4009 ▶{
Subroutine value: called CC=3 SCAR=11.0
471 4009 my $rc = '';
472 4009 foreach my $val ( @{ $_[0]->{PARAMS} } )
473 {
474 3 my $catval = $val->value( $_[1] );
475 3 ▶ $rc .= defined($catval) ? $catval : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $catval ? : 3 0 3
476 }
477 4009 return $rc;
478 }
479  
480 =head1 AUTHOR AND COPYRIGHT
481  
482 Copyright (c) 2009-2020 by Jens Rehsack: rehsackATcpan.org
483  
484 All rights reserved.
485  
486 You may distribute this module under the terms of either the GNU
487 General Public License or the Artistic License, as specified in
488 the Perl README file.
489  
490 =cut
491  
492 16 1;
« Statement/Functions.pm ↑ Summary Statement/Term.pm »