Fey/SQL/Fragment/Where/Comparison.pm

filter: Statementstmt 98.7% 80 / 81 Branchbran 100% 32 / 32 Conditioncond 92.5% 25 / 27 MC/DCmcdc 86.6% 13 / 15 Subroutinesub 92.3% 12 / 13 Podpod 33.3% 1 / 3 totaltotal 95.3% 163 / 171 CC 27 SCAR 24.1
CC
27
Cov
98%
BUILDARGS
18.0
sql
10.0
__ANON__
2.0
CRAP 11.2
« Fey/SQL/Select.pm ↑ Summary Fey/Types/Internal.pm »
1 package Fey::SQL::Fragment::Where::Comparison;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 our $VERSION = '0.44';
8  
9 27 ▶use Fey::Exceptions qw( param_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use Fey::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use Fey::Placeholder;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use Fey::Types qw( ArrayRef WhereClauseSide Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 27 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 27 ▶use Moose 2.1200;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 27 has '_lhs' => (
18 is => 'ro',
19 isa => WhereClauseSide,
20 required => 1,
21 );
22  
23 27 has '_operator' => (
24 is => 'ro',
25 isa => Str,
26 required => 1,
27 );
28  
29 27 has '_rhs' => (
30 is => 'ro',
31 isa => ArrayRef [WhereClauseSide],
32 required => 1,
33 );
34  
35 has '_bind_params' => (
36 is => 'ro',
37 isa => ArrayRef,
38 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 27 );
40  
41 27 our $eq_comp_re = qr/^(?:=|!=|<>)$/;
42 27 our $in_comp_re = qr/^(?:not\s+)?in$/i;
43  
44 92 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=18 SCAR=28.9
Pod documented
45 92 my $class = shift;
46 92 my $auto_placeholders = shift;
47 92 my $lhs = shift;
48 92 my $operator = shift;
49 92 my @rhs = @_;
50  
51 92 my @bind;
52 92 for ( $lhs, @rhs ) {
53 207 ▶ if ( defined $_ && blessed $_ && $_->can('is_comparable') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_ and builtin::blessed($_) and $_->can("is_comparable")) 105 102 207
Condition100%6/6
defined $_ and builtin::blessed($_) and $_->can("is_comparable")
!ll&&!rl&&r
957105
defined $_ and builtin::blessed($_)
!ll&&!rl&&r
788112
MC/DC100%3/3
defined $_ and builtin::blessed($_) and $_->can("is_comparable")
defined $_ builtin::blessed($_) $_->can("is_comparable")
Truth table100%4/4
A and B and C
A= defined $_B= builtin::blessed($_)C= $_->can("is_comparable")
ABCresult
0XX0
10X0
1100
1111
54 105 ▶ if ( $_->can('bind_params') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->can("bind_params")) 10 95 105
55 10 push @bind, $_->bind_params();
56 }
57  
58 105 next;
59 }
60  
61 102 ▶ if ( defined $_ && blessed $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_ and builtin::blessed($_)) 7 95 102
Condition100%3/3
defined $_ and builtin::blessed($_)
!ll&&!rl&&r
7887
MC/DC100%2/2
defined $_ and builtin::blessed($_)
defined $_ builtin::blessed($_)
Truth table100%3/3
A and B
A= defined $_B= builtin::blessed($_)
ABresult
0X0
100
111
62 7 ▶ if ( overload::Overloaded($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (overload::Overloaded($_)) { } 6 1 7
63  
64 # This "de-references" the value, which will make
65 # things simpler when we pass it to DBI, test
66 # code, etc. It works fine with numbers, more or
67 # less (see Fey::Literal).
68 6 $_ .= q{};
69 }
70 else {
71 1 param_error
72 "Cannot pass an object as part of a where clause comparison"
73 . " unless that object does Fey::Role::Comparable or is overloaded.";
74 }
75 }
76  
77 101 ▶ if ( defined $_ && $auto_placeholders ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_ and $auto_placeholders) { } 38 63 101
Condition100%3/3
defined $_ and $auto_placeholders
!ll&&!rl&&r
75638
MC/DC100%2/2
defined $_ and $auto_placeholders
defined $_ $auto_placeholders
Truth table100%3/3
A and B
A= defined $_B= $auto_placeholders
ABresult
0X0
100
111
78 38 push @bind, $_;
79  
80 38 $_ = Fey::Placeholder->new();
81 }
82 else {
83 63 $_ = Fey::Literal->new_from_scalar($_);
84 }
85  
86 }
87  
88 91 ▶ if ( grep { $_->does('Fey::Role::SQL::ReturnsData') } @rhs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->does("Fey::Role::SQL::ReturnsData");} @rhs) 10 81 91
89 10 ▶ param_error
Branch: 1 branch
BranchTrueFalseTotal
unless $operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re|$Fey::SQL::Fragment::Where::Comparison::in_comp_re/ 2 8 10
90 "Cannot use a subselect on the right-hand side with $operator"
91 unless $operator =~ /$eq_comp_re|$in_comp_re/;
92 }
93  
94 89 ▶ if ( defined $operator && lc $operator eq 'between' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $operator and lc $operator eq "between") 5 84 89
Condition100%3/3
defined $operator and lc $operator eq "between"
!ll&&!rl&&r
2825
MC/DC100%2/2
defined $operator and lc $operator eq "between"
defined $operator lc $operator eq "between"
Truth table100%3/3
A and B
A= defined $operatorB= lc $operator eq "between"
ABresult
0X0
100
111
95 5 ▶ param_error "The BETWEEN operator requires two arguments"
Branch: 1 branch
BranchTrueFalseTotal
unless @rhs == 2 2 3 5
96 unless @rhs == 2;
97 }
98  
99 87 ▶ if ( @rhs > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rhs > 1) 15 72 87
100 15 ▶ param_error
Branch: 1 branch
BranchTrueFalseTotal
unless $operator =~ /^(?:$Fey::SQL::Fragment::Where::Comparison::in_comp_re|between)$/i 2 13 15
101 "Cannot pass more than one right-hand side argument with $operator"
102 unless $operator =~ /^(?:$in_comp_re|between)$/i;
103 }
104  
105 return {
106 85 _lhs => $lhs,
107 _operator => $operator,
108 _rhs => \@rhs,
109 _bind_params => \@bind,
110 };
111 }
112  
113 78 ▶sub sql {
Subroutine sql: called CC=10 SCAR=23.0
Pod undocumented
114 78 my $self = shift;
115 78 my $dbh = shift;
116  
117 78 my $sql = $self->_lhs()->sql_or_alias($dbh);
118  
119 78 ▶ if ( $self->_operator() =~ /$eq_comp_re/
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/ and $self->_rhs->[0]->isa("Fey::Literal::Null")) 5 73 78
Condition100%3/3
$self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/ and $self->_rhs->[0]->isa("Fey::Literal::Null")
!ll&&!rl&&r
20535
MC/DC100%2/2
$self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/ and $self->_rhs->[0]->isa("Fey::Literal::Null")
$self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/ $self->_rhs->[0]->isa("Fey::Literal::Null")
Truth table100%3/3
A and B
A= $self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/B= $self->_rhs->[0]->isa("Fey::Literal::Null")
ABresult
0X0
100
111
120 && $self->_rhs()->[0]->isa('Fey::Literal::Null') ) {
121 return (
122 5 ▶ $sql
Branch: 1 branch
BranchTrueFalseTotal
$self->_operator eq '=' ? : 3 2 5
123 . (
124 $self->_operator() eq '='
125 ? ' IS NULL'
126 : ' IS NOT NULL'
127 )
128 );
129 }
130  
131 73 ▶ if ( lc $self->_operator() eq 'between' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $self->_operator eq "between") 2 71 73
132 2 return ( $sql
133 . ' BETWEEN '
134 . $self->_rhs()->[0]->sql_or_alias($dbh) . ' AND '
135 . $self->_rhs()->[1]->sql_or_alias($dbh) );
136 }
137  
138 71 ▶ if ( $self->_operator() =~ /$in_comp_re/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::in_comp_re/) 16 55 71
139 return (
140 $sql . ' '
141 . ( uc $self->_operator() ) . ' ('
142 . (
143 join ', ',
144 16 map { $_->sql_or_alias($dbh) } @{ $self->_rhs() }
145 )
146 . ')'
147 );
148 }
149  
150 55 ▶ if ( $self->_operator() =~ /$eq_comp_re/
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/ and @{$self->_rhs;} == 1 and builtin::blessed($self->_rhs->[0]) and $self->_rhs->[0]->does("Fey::Role::SQL::ReturnsData")) 1 54 55
Condition77%7/9
$self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/ and @{$self->_rhs;} == 1 and builtin::blessed($self->_rhs->[0]) and $self->_rhs->[0]->does("Fey::Role::SQL::ReturnsData")
!ll&&!rl&&r
2521
$self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/ and @{$self->_rhs;} == 1 and builtin::blessed($self->_rhs->[0])
!ll&&!rl&&r
2053
$self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/ and @{$self->_rhs;} == 1
!ll&&!rl&&r
2053
MC/DC50%2/4
$self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/ and @{$self->_rhs;} == 1 and builtin::blessed($self->_rhs->[0]) and $self->_rhs->[0]->does("Fey::Role::SQL::ReturnsData")
$self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/ @{$self->_rhs;} == 1 builtin::blessed($self->_rhs->[0]) $self->_rhs->[0]->does("Fey::Role::SQL::ReturnsData")
Truth table60%3/5
A and B and C and D
A= $self->_operator =~ /$Fey::SQL::Fragment::Where::Comparison::eq_comp_re/B= @{$self->_rhs;} == 1C= builtin::blessed($self->_rhs->[0])D= $self->_rhs->[0]->does("Fey::Role::SQL::ReturnsData")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
151 53 && @{ $self->_rhs() } == 1
152 && blessed $self->_rhs()->[0]
153 && $self->_rhs()->[0]->does('Fey::Role::SQL::ReturnsData') ) {
154 1 return ( $sql . ' '
155 . $self->_operator() . ' ('
156 . $self->_rhs()->[0]->sql_or_alias($dbh)
157 . ')' );
158 }
159  
160 54 return ( $sql . ' '
161 . $self->_operator() . ' '
162 . $self->_rhs()->[0]->sql_or_alias($dbh) );
163 }
164  
165 30 ▶sub bind_params {
Subroutine bind_params: called CC=1 SCAR=0.0
Pod undocumented
166 30 return @{ $_[0]->_bind_params() };
167 }
168  
169 27 __PACKAGE__->meta()->make_immutable();
170  
171 27 1;
172  
173 # ABSTRACT: Represents a comparison in a WHERE clause
174  
175 __END__
« Fey/SQL/Select.pm ↑ Summary Fey/Types/Internal.pm »