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/Literal.pm ↑ Summary Fey/Literal/Term.pm »
1 package Fey::Role::SQL::HasWhereClause;
2  
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 our $VERSION = '0.42';
8  
9 26 ▶use Fey::Exceptions qw( param_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 26 ▶use Fey::SQL::Fragment::Where::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use Fey::SQL::Fragment::Where::Comparison;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use Fey::SQL::Fragment::Where::SubgroupStart;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 26 ▶use Fey::SQL::Fragment::Where::SubgroupEnd;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 26 ▶use Fey::Types qw( ArrayRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 26 ▶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 26 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 sub where {
34 81 ▶ my $self = shift;
Subroutine where: called CC=1 SCAR=0.0
Pod documented
35  
36 81 $self->_condition( 'where', @_ );
37  
38 75 return $self;
39 }
40  
41 # Just some sugar
42 sub and {
43 1 ▶ my $self = shift;
Subroutine and: called CC=1 SCAR=0.0
Pod documented
44  
45 1 return $self->where(@_);
46 }
47  
48 {
49 26 my %dispatch = (
50 'and' => '_and',
51 'or' => '_or',
52 '(' => '_subgroup_start',
53 ')' => '_subgroup_end',
54 );
55  
56 sub _condition {
57 108 ▶ my $self = shift;
Subroutine _condition: called CC=3 SCAR=11.0
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 26 sub _add_and_if_needed {
83 97 ▶ my $self = shift;
Subroutine _add_and_if_needed: called CC=4 SCAR=13.9
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 sub _and {
100 7 ▶ my $self = shift;
Subroutine _and: called CC=1 SCAR=0.0
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 sub _or {
111 5 ▶ my $self = shift;
Subroutine _or: called CC=1 SCAR=0.0
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 sub _subgroup_start {
122 5 ▶ my $self = shift;
Subroutine _subgroup_start: called CC=1 SCAR=0.0
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 sub _subgroup_end {
134 5 ▶ my $self = shift;
Subroutine _subgroup_end: called CC=1 SCAR=0.0
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 sub where_clause {
144 117 ▶ my $self = shift;
Subroutine where_clause: called CC=3 SCAR=11.0
Pod documented
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 sub bind_params {
164 36 ▶ my $self = shift;
Subroutine bind_params: called CC=1 SCAR=0.0
Pod undocumented
165  
166 return (
167 27 map { $_->bind_params() }
168 36 grep { $_->can('bind_params') } $self->_where()
169 );
170 }
171  
172 26 1;
173  
174 # ABSTRACT: A role for queries which can include a WHERE clause
175  
176 __END__
177  
178 =pod
179  
180 =head1 NAME
181  
182 Fey::Role::SQL::HasWhereClause - A role for queries which can include a WHERE clause
183  
184 =head1 VERSION
185  
186 version 0.42
187  
188 =head1 SYNOPSIS
189  
190 use Moose 0.90;
191  
192 with 'Fey::Role::SQL::HasWhereClause';
193  
194 =head1 DESCRIPTION
195  
196 Classes which do this role represent a query which can include a
197 C<WHERE> clause.
198  
199 =head1 METHODS
200  
201 This role provides the following methods:
202  
203 =head2 $query->where(...)
204  
205 See the L<Fey::SQL section on WHERE Clauses|Fey::SQL/WHERE Clauses>
206 for more details.
207  
208 =head2 $query->and(...)
209  
210 See the L<Fey::SQL section on WHERE Clauses|Fey::SQL/WHERE Clauses>
211 for more details.
212  
213 =head2 $query->where_clause( $dbh, $skip_where )
214  
215 Returns the C<WHERE> clause portion of the SQL statement as a string. The
216 first argument, a database handle, is required. If the second argument is
217 true, the string returned will not start with "WHERE", it will simply start
218 with the where clause conditions.
219  
220 =head1 BUGS
221  
222 See L<Fey> for details on how to report bugs.
223  
224 =head1 AUTHOR
225  
226 Dave Rolsky <autarch@urth.org>
227  
228 =head1 COPYRIGHT AND LICENSE
229  
230 This software is Copyright (c) 2011 - 2015 by Dave Rolsky.
231  
232 This is free software, licensed under:
233  
234 The Artistic License 2.0 (GPL Compatible)
235  
236 =cut
« Fey/Literal.pm ↑ Summary Fey/Literal/Term.pm »