Fey/Role/SQL/HasWhereClause.pm

filter: Statementstmt 100% 93 / 93 Branchbran 100% 14 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 21 / 21 Podpod 75.0% 3 / 4 totaltotal 99.2% 131 / 132 CC 8 SCAR 6.2
CC
8
Cov
100%
_add_and_if_needed
4.0
_condition
3.0
where_clause
3.0
CRAP 1.9
« Fey/Role/SetOperation.pm ↑ Summary Fey/Literal/Term.pm »
1 package Fey::Role::SQL::HasWhereClause;
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  
11 27 ▶use Fey::SQL::Fragment::Where::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use Fey::SQL::Fragment::Where::Comparison;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 27 ▶use Fey::SQL::Fragment::Where::SubgroupStart;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 27 ▶use Fey::SQL::Fragment::Where::SubgroupEnd;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 27 ▶use Fey::Types qw( ArrayRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 27 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 has '_where' => (
20 traits => ['Array'],
21 is => 'bare',
22 isa => ArrayRef,
23 193 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 27 handles => {
25 _add_where_element => 'push',
26 _has_where_elements => 'count',
27 _last_where_element => [ 'get', -1 ],
28 _where => 'elements',
29 },
30 init_arg => undef,
31 );
32  
33 81 ▶sub where {
Subroutine where: called CC=1 SCAR=0.0
Pod documented
34 81 my $self = shift;
35  
36 81 $self->_condition( 'where', @_ );
37  
38 75 return $self;
39 }
40  
41 # Just some sugar
42 1 ▶sub and {
Subroutine and: called CC=1 SCAR=0.0
Pod documented
43 1 my $self = shift;
44  
45 1 return $self->where(@_);
46 }
47  
48 {
49 27 my %dispatch = (
50 'and' => '_and',
51 'or' => '_or',
52 '(' => '_subgroup_start',
53 ')' => '_subgroup_end',
54 );
55  
56 108 ▶ sub _condition {
Subroutine _condition: called CC=3 SCAR=11.0
57 108 my $self = shift;
58 108 my $key = shift;
59  
60 108 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 16 92 108
61 16 ▶ if ( my $meth = $dispatch{ lc $_[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meth = $dispatch{lc $_[0]}) { } 15 1 16
62 15 $self->$meth($key);
63 15 return;
64 }
65 else {
66 1 param_error
67 qq|Cannot pass one argument to $key() unless it is one of "and", "or", "(", or ")".|;
68 }
69 }
70  
71 92 $self->_add_and_if_needed($key);
72  
73 92 my $add_method = '_add_' . $key . '_element';
74 92 $self->$add_method(
75 Fey::SQL::Fragment::Where::Comparison->new(
76 $self->auto_placeholders(), @_
77 )
78 );
79 }
80 }
81  
82 27 ▶sub _add_and_if_needed {
Subroutine _add_and_if_needed: called CC=4 SCAR=13.9
83 97 my $self = shift;
84 97 my $key = shift;
85  
86 97 my $has_method = '_has_' . $key . '_elements';
87  
88 97 ▶ return unless $self->$has_method();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$has_method 80 17 97
89  
90 17 my $last_method = '_last_' . $key . '_element';
91 17 my $last = $self->$last_method();
92  
93 17 ▶ return if $last->isa('Fey::SQL::Fragment::Where::Boolean');
Branch: 1 branch
BranchTrueFalseTotal
if $last->isa("Fey::SQL::Fragment::Where::Boolean") 5 12 17
94 12 ▶ return if $last->isa('Fey::SQL::Fragment::Where::SubgroupStart');
Branch: 1 branch
BranchTrueFalseTotal
if $last->isa("Fey::SQL::Fragment::Where::SubgroupStart") 5 7 12
95  
96 7 $self->_and($key);
97 }
98  
99 7 ▶sub _and {
Subroutine _and: called CC=1 SCAR=0.0
100 7 my $self = shift;
101 7 my $key = shift;
102  
103 7 my $add_method = '_add_' . $key . '_element';
104 7 $self->$add_method(
105 Fey::SQL::Fragment::Where::Boolean->new( comparison => 'AND' ) );
106  
107 7 return $self;
108 }
109  
110 5 ▶sub _or {
Subroutine _or: called CC=1 SCAR=0.0
111 5 my $self = shift;
112 5 my $key = shift;
113  
114 5 my $add_method = '_add_' . $key . '_element';
115 5 $self->$add_method(
116 Fey::SQL::Fragment::Where::Boolean->new( comparison => 'OR' ) );
117  
118 5 return $self;
119 }
120  
121 5 ▶sub _subgroup_start {
Subroutine _subgroup_start: called CC=1 SCAR=0.0
122 5 my $self = shift;
123 5 my $key = shift;
124  
125 5 $self->_add_and_if_needed($key);
126  
127 5 my $add_method = '_add_' . $key . '_element';
128 5 $self->$add_method( Fey::SQL::Fragment::Where::SubgroupStart->new() );
129  
130 5 return $self;
131 }
132  
133 5 ▶sub _subgroup_end {
Subroutine _subgroup_end: called CC=1 SCAR=0.0
134 5 my $self = shift;
135 5 my $key = shift;
136  
137 5 my $add_method = '_add_' . $key . '_element';
138 5 $self->$add_method( Fey::SQL::Fragment::Where::SubgroupEnd->new() );
139  
140 5 return $self;
141 }
142  
143 117 ▶sub where_clause {
Subroutine where_clause: called CC=3 SCAR=11.0
Pod documented
144 117 my $self = shift;
145 117 my $dbh = shift;
146 117 my $skip_where = shift;
147  
148 117 ▶ return unless $self->_has_where_elements();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_where_elements 67 50 117
149  
150 50 my $sql = '';
151 50 ▶ $sql = 'WHERE '
Branch: 1 branch
BranchTrueFalseTotal
unless $skip_where 48 2 50
152 unless $skip_where;
153  
154 return (
155 $sql
156 . (
157 join ' ',
158 50 map { $_->sql($dbh) } $self->_where()
159 )
160 );
161 }
162  
163 36 ▶sub bind_params {
Subroutine bind_params: called CC=1 SCAR=0.0
Pod undocumented
164 36 my $self = shift;
165  
166 return (
167 27 map { $_->bind_params() }
168 36 grep { $_->can('bind_params') } $self->_where()
169 );
170 }
171  
172 27 1;
173  
174 # ABSTRACT: A role for queries which can include a WHERE clause
175  
176 __END__
« Fey/Role/SetOperation.pm ↑ Summary Fey/Literal/Term.pm »