Statement/TermFactory.pm

filter: Statementstmt 96.3% 80 / 83 Branchbran 90.4% 38 / 42 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 50.0% 1 / 2 totaltotal 93.1% 135 / 145 CC 23 SCAR 26.0
CC
23
Cov
94%
buildCondition
21.4
_getOpClass
3.0
BEGIN
1.0
CRAP 13.4
« Parser.pm ↑ Summary Eval.pm »
1 package SQL::Statement::TermFactory;
2  
3 ######################################################################
4 #
5 # This module is copyright (c), 2009-2016 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 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use warnings FATAL => "all";
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 13 ▶use SQL::Statement::Term ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use SQL::Statement::Operation ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use SQL::Statement::Placeholder ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use SQL::Statement::Function ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 13 ▶use Params::Util qw(_HASH _ARRAY0 _INSTANCE);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 13 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 13 our $VERSION = '1.408_001';
26  
27 13 my %oplist = (
28 '=' => 'Equal',
29 '<>' => 'NotEqual',
30 'AND' => 'And',
31 'OR' => 'Or',
32 '<=' => 'LowerEqual',
33 '>=' => 'GreaterEqual',
34 '<' => 'Lower',
35 '>' => 'Greater',
36 'LIKE' => 'Like',
37 'RLIKE' => 'Rlike',
38 'CLIKE' => 'Clike',
39 'IN' => 'Contains',
40 'BETWEEN' => 'Between',
41 'IS' => 'Is',
42 );
43  
44 sub new
45 {
46 823 ▶ my ( $class, $owner ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
47 823 my $self = bless(
48 {
49 OWNER => $owner,
50 },
51 $class
52 );
53  
54 823 weaken( $self->{OWNER} );
55  
56 823 return $self;
57 }
58  
59 13 my %opClasses;
60  
61 sub _getOpClass($)
62 {
63 264 ▶ my ( $self, $op ) = @_;
Subroutine _getOpClass: called CC=3 SCAR=11.0
64 264 ▶ unless ( defined( $opClasses{$op} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opClasses{$op}) 34 230 264
65 {
66 34 my $opBase = 'SQL::Statement::Operation';
67 34 my $opDialect = join( '::', $opBase, $self->{OWNER}->{dialect}, $oplist{$op} );
68 $opClasses{$op} =
69 34 ▶ $opDialect->isa($opBase) ? $opDialect : join( '::', $opBase, $oplist{$op} );
Branch: 1 branch
BranchTrueFalseTotal
$opDialect->isa($opBase) ? : 2 32 34
70 }
71  
72 264 return $opClasses{$op};
73 }
74  
75 sub buildCondition
76 {
77 2248 ▶ my ( $self, $pred ) = @_;
Subroutine buildCondition: called CC=21 SCAR=30.6
Pod documented
78 2248 my $term;
79  
80 2248 ▶ if ( _ARRAY0($pred) )
Branch: 4 branches
BranchTrueFalseTotal
if (&_ARRAY0($pred)) { } 27 2221 2248
elsif (defined $pred->{'op'}) { } 321 1900 2221
elsif (defined $pred->{'type'}) { } 1898 2 1900
elsif (defined &_INSTANCE($pred, 'SQL::Statement::Term')) { } 0 2 2
81 {
82 27 $term = [ map { $self->buildCondition($_) } @{$pred} ];
83 }
84 elsif ( defined( $pred->{op} ) )
85 {
86 321 my $op = uc( $pred->{op} );
87 321 ▶ if ( $op eq 'USER_DEFINED' && !$pred->{arg2} )
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'USER_DEFINED' and not $pred->{'arg2'}) { } 56 265 321
elsif (defined $oplist{$op}) { } 264 1 265
elsif (defined $self->{'OWNER'}{'opts'}{'function_names'}{$op}) { } 1 0 1
Condition66%2/3
$op eq "USER_DEFINED" and not $pred->{"arg2"}
!ll&&!rl&&r
265056
MC/DC50%1/2
$op eq "USER_DEFINED" and not $pred->{"arg2"}
$op eq "USER_DEFINED" not $pred->{"arg2"}
Truth table66%2/3
A and not(B)
A= $op eq "USER_DEFINED"B= not $pred->{"arg2"}
ABresult
0X0
100
111
88 {
89 56 $term = SQL::Statement::ConstantTerm->new( $self->{OWNER}, $pred->{arg1}->{value} );
90 }
91 elsif ( defined( $oplist{$op} ) )
92 {
93 264 my $cn = $self->_getOpClass($op);
94 264 my $left = $self->buildCondition( $pred->{arg1} );
95 264 my $right = $self->buildCondition( $pred->{arg2} );
96 264 $term = $cn->new( $self->{OWNER}, $op, $left, $right );
97 }
98 elsif ( defined( $self->{OWNER}->{opts}->{function_names}->{$op} ) )
99 {
100 1 my $left = $self->buildCondition( $pred->{arg1} );
101 1 my $right = $self->buildCondition( $pred->{arg2} );
102  
103 $term = SQL::Statement::Function::UserFunc->new(
104 $self->{OWNER}, $op,
105 1 $self->{OWNER}->{opts}->{function_names}->{$op},
106 [ $left, $right ]
107 );
108 }
109 else
110 {
111 0 return $self->{OWNER}->do_err( sprintf( q{Unknown operation '%s'}, $pred->{op} ) );
112 }
113  
114 321 ▶ if ( $pred->{neg} )
Branch: 1 branch
BranchTrueFalseTotal
if ($pred->{"neg"}) 22 299 321
115 {
116 22 $term = SQL::Statement::Operation::Neg->new( $self->{OWNER}, 'NOT', $term );
117 }
118 }
119 elsif ( defined( $pred->{type} ) )
120 {
121 1898 my $type = uc( $pred->{type} );
122 1898 ▶ if ( $type =~ m/^(?:STRING|NUMBER|BOOLEAN)$/ )
Branch: 5 branches
BranchTrueFalseTotal
if ($type =~ /^(?:STRING|NUMBER|BOOLEAN)$/) { } 1083 815 1898
elsif ($type eq 'NULL') { } 24 791 815
elsif ($type eq 'COLUMN') { } 432 359 791
elsif ($type eq 'PLACEHOLDER') { } 28 331 359
elsif ($type eq 'FUNCTION') { } 331 0 331
123 {
124 1083 $term = SQL::Statement::ConstantTerm->new( $self->{OWNER}, $pred->{value} );
125 }
126 elsif ( $type eq 'NULL' )
127 {
128 24 $term = SQL::Statement::ConstantTerm->new( $self->{OWNER}, undef );
129 }
130 elsif ( $type eq 'COLUMN' )
131 {
132 432 $term = SQL::Statement::ColumnValue->new( $self->{OWNER}, $pred->{value} );
133 }
134 elsif ( $type eq 'PLACEHOLDER' )
135 {
136 28 $term = SQL::Statement::Placeholder->new( $self->{OWNER}, $pred->{argnum} );
137 }
138 elsif ( $type eq 'FUNCTION' )
139 {
140 331 ▶ my @params = map { blessed($_) ? $_ : $self->buildCondition($_) } @{ $pred->{value} };
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 0 457 457
141  
142 331 ▶ if ( $pred->{name} eq 'numeric_exp' )
Branch: 4 branches
BranchTrueFalseTotal
if ($pred->{'name'} eq 'numeric_exp') { } 41 290 331
elsif ($pred->{'name'} eq 'str_concat') { } 7 283 290
elsif ($pred->{'name'} eq 'TRIM') { } 10 273 283
elsif ($pred->{'name'} eq 'SUBSTRING') { } 3 270 273
143 {
144 41 $term = SQL::Statement::Function::NumericEval->new( $self->{OWNER}, $pred->{str}, \@params );
145 }
146 elsif ( $pred->{name} eq 'str_concat' )
147 {
148 7 $term = SQL::Statement::Function::StrConcat->new( $self->{OWNER}, \@params );
149 }
150 elsif ( $pred->{name} eq 'TRIM' )
151 {
152 10 $term = SQL::Statement::Function::Trim->new( $self->{OWNER}, $pred->{trim_spec}, $pred->{trim_char}, \@params );
153 }
154 elsif ( $pred->{name} eq 'SUBSTRING' )
155 {
156 3 my $start = $self->buildCondition( $pred->{start} );
157 my $length = $self->buildCondition( $pred->{length} )
158 3 ▶ if ( _HASH( $pred->{length} ) );
Branch: 1 branch
BranchTrueFalseTotal
if &_HASH($pred->{"length"}) 2 1 3
159 3 $term = SQL::Statement::Function::SubString->new( $self->{OWNER}, $start, $length, \@params );
160 }
161 else
162 {
163 270 $term = SQL::Statement::Function::UserFunc->new( $self->{OWNER}, $pred->{name}, $pred->{subname}, \@params );
164 }
165 }
166 else
167 {
168 0 return $self->{OWNER}->do_err( sprintf( q{Unknown type '%s'}, $pred->{type} ) );
169 }
170 }
171 elsif ( defined( _INSTANCE( $pred, 'SQL::Statement::Term' ) ) )
172 {
173 0 return $pred;
174 }
175 else
176 {
177 2 return $self->{OWNER}->do_err( sprintf( q~Unknown predicate '{%s}'~, Dumper($pred) ) );
178 }
179  
180 2246 return $term;
181 }
182  
183 sub DESTROY
184 {
185 821 ▶ my $self = $_[0];
Subroutine DESTROY: called CC=1 SCAR=0.0
186 821 undef $self->{OWNER};
187 }
188  
189 =pod
190  
191 =head1 NAME
192  
193 SQL::Statement::TermFactory - Factory for SQL::Statement::Term instances
194  
195 =head1 SYNOPSIS
196  
197 my $termFactory = SQL::Statement::TermFactory->new($stmt);
198 my $whereTerms = $termFactory->buildCondition( $stmt->{where_clause} );
199 my $col = $termFactory->buildCondition( $stmt->{col_obj}->{$name}->{content} );
200  
201 =head1 DESCRIPTION
202  
203 This package implements a factory to create type and operation based terms.
204 Those terms are used to access data from the table(s) - either when evaluating
205 the where clause or returning column data.
206  
207 The concept of a factory can be studied in I<Design Patterns> by the Gang of
208 Four. The concept of using polymorphism instead of conditions is suggested by
209 Martin Fowler in his book I<Refactoring>.
210  
211 =head1 METHODS
212  
213 =head2 buildCondition
214  
215 Builds a condition object from a given (part of a) where clause. This method
216 calls itself recursively for I<predicates>.
217  
218 =head1 AUTHOR AND COPYRIGHT
219  
220 Copyright (c) 2001,2005 by Jeff Zucker: jzuckerATcpan.org
221 Copyright (c) 2009-2016 by Jens Rehsack: rehsackATcpan.org
222  
223 All rights reserved.
224  
225 You may distribute this module under the terms of either the GNU
226 General Public License or the Artistic License, as specified in
227 the Perl README file.
228  
229 =cut
230  
231 13 1;
« Parser.pm ↑ Summary Eval.pm »