Fey/SQL/Update.pm

filter: Statementstmt 100% 91 / 91 Branchbran 100% 20 / 20 Conditioncond 77.7% 14 / 18 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 23 / 23 Podpod 100% 6 / 6 totaltotal 95.8% 162 / 169 CC 17 SCAR 16.0
CC
17
Cov
97%
set
12.0
set_clause
5.1
update
2.0
CRAP 5.0
« Fey/Schema.pm ↑ Summary Fey/Role/SQL/HasLimitClause.pm »
1 package Fey::SQL::Update;
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::Types qw( ArrayRef CanQuote ColumnWithTable NonNullableUpdateValue
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 NullableUpdateValue Table );
13 27 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 27 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 27 ▶use Moose 2.1200;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 27 ▶use MooseX::Params::Validate 0.21 qw( pos_validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 27 ▶use MooseX::SemiAffordanceAccessor 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 27 ▶use MooseX::StrictConstructor 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 27 with 'Fey::Role::SQL::HasOrderByClause', 'Fey::Role::SQL::HasLimitClause';
22  
23 27 with 'Fey::Role::SQL::HasWhereClause' => {
24 -excludes => 'bind_params',
25 -alias => { bind_params => '_where_clause_bind_params' },
26 };
27  
28 27 with 'Fey::Role::SQL::HasBindParams' => {
29 -excludes => 'bind_params',
30 -alias => { bind_params => '_update_bind_params' },
31 };
32  
33 has '_update' => (
34 is => 'rw',
35 isa => ArrayRef,
36 26 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 27 init_arg => undef,
38 );
39  
40 has '_set_pairs' => (
41 traits => ['Array'],
42 is => 'bare',
43 isa => ArrayRef [ArrayRef],
44 26 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 27 handles => {
46 _add_set_pair => 'push',
47 _set_pairs => 'elements',
48 },
49 init_arg => undef,
50 );
51  
52 27 with 'Fey::Role::SQL::Cloneable';
53  
54 26 ▶sub update {
Subroutine update: called CC=2 SCAR=6.9
Pod documented
55 26 my $self = shift;
56  
57 26 ▶ my $count = @_ ? @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 25 1 26
58 26 my (@tables) = pos_validated_list(
59 \@_,
60 ( ( { isa => Table } ) x $count ),
61 MX_PARAMS_VALIDATE_NO_CACHE => 1,
62 );
63  
64 25 $self->_set_update( \@tables );
65  
66 25 return $self;
67 }
68  
69 23 ▶sub set {
Subroutine set: called CC=12 SCAR=24.9
Pod documented
70 23 my $self = shift;
71  
72 23 ▶ if ( !@_ || @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ or @_ % 2) 2 21 23
Condition100%3/3
not @_ or @_ % 2
l!l&&r!l&&!r
1121
MC/DC100%2/2
not @_ or @_ % 2
not @_ @_ % 2
Truth table100%3/3
not(A) or B
A= not @_B= @_ % 2
ABresult
1X1
011
000
73 2 my $count = @_;
74 2 param_error
75 "The set method expects a list of paired column objects and values but you passed $count parameters";
76 }
77  
78 21 my @spec;
79 21 ▶ for ( my $x = 0; $x < @_; $x += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @_) 25 21 46
80 25 push @spec, { isa => ColumnWithTable };
81 25 ▶ push @spec,
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[$x]) && $_[$x]->is_nullable ? : 6 19 25
Condition66%2/3
builtin::blessed($_[$x]) and $_[$x]->is_nullable
!ll&&!rl&&r
0196
MC/DC50%1/2
builtin::blessed($_[$x]) and $_[$x]->is_nullable
builtin::blessed($_[$x]) $_[$x]->is_nullable
Truth table66%2/3
A and B
A= builtin::blessed($_[$x])B= $_[$x]->is_nullable
ABresult
0X0
100
111
82 blessed $_[$x] && $_[$x]->is_nullable()
83 ? { isa => NullableUpdateValue }
84 : { isa => NonNullableUpdateValue };
85 }
86  
87 my @set
88 21 = pos_validated_list( \@_, @spec, MX_PARAMS_VALIDATE_NO_CACHE => 1 );
89  
90 21 ▶ for ( my $x = 0; $x < @_; $x += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @_) 25 21 46
91 25 my $val = $_[ $x + 1 ];
92  
93 25 ▶ $val .= ''
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($val) and overload::Overloaded($val) 4 21 25
Condition100%3/3
builtin::blessed($val) and overload::Overloaded($val)
!ll&&!rl&&r
1294
MC/DC100%2/2
builtin::blessed($val) and overload::Overloaded($val)
builtin::blessed($val) overload::Overloaded($val)
Truth table100%3/3
A and B
A= builtin::blessed($val)B= overload::Overloaded($val)
ABresult
0X0
100
111
94 if blessed $val && overload::Overloaded($val);
95  
96 25 ▶ if ( !blessed $val ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($val)) 16 9 25
97 16 ▶ if ( defined $val && $self->auto_placeholders() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $self->auto_placeholders) { } 8 8 16
Condition100%3/3
defined $val and $self->auto_placeholders
!ll&&!rl&&r
178
MC/DC100%2/2
defined $val and $self->auto_placeholders
defined $val $self->auto_placeholders
Truth table100%3/3
A and B
A= defined $valB= $self->auto_placeholders
ABresult
0X0
100
111
98 8 $self->_add_bind_param($val);
99  
100 8 $val = Fey::Placeholder->new();
101 }
102 else {
103 8 $val = Fey::Literal->new_from_scalar($val);
104 }
105 }
106  
107 25 $self->_add_set_pair( [ $_[$x], $val ] );
108 }
109  
110 21 return $self;
111 }
112  
113 4 ▶sub sql {
Subroutine sql: called CC=1 SCAR=0.0
Pod documented
114 4 my $self = shift;
115 4 my ($dbh) = pos_validated_list( \@_, { isa => CanQuote } );
116  
117 return (
118 4 join ' ',
119 $self->update_clause($dbh),
120 $self->set_clause($dbh),
121 $self->where_clause($dbh),
122 $self->order_by_clause($dbh),
123 $self->limit_clause($dbh),
124 );
125 }
126  
127 6 ▶sub update_clause {
Subroutine update_clause: called CC=1 SCAR=0.0
Pod documented
128 6 return 'UPDATE ' . $_[0]->_tables_subclause( $_[1] );
129 }
130  
131 6 ▶sub _tables_subclause {
Subroutine _tables_subclause: called CC=1 SCAR=0.0
132 return (
133 join ', ',
134 6 map { $_[1]->quote_identifier( $_->name() ) } @{ $_[0]->_update() }
135 );
136 }
137  
138 21 ▶sub set_clause {
Subroutine set_clause: called CC=5 SCAR=16.3
Pod documented
139 21 my $self = shift;
140 21 my $dbh = shift;
141  
142 # SQLite objects when the table name is provided ("User"."email")
143 # on the LHS of the set. I'm hoping that a DBMS which allows a
144 # multi-table update also allows the table name in the LHS.
145 21 ▶ my $col_quote = @{ $self->_update() } > 1 ? '_name_and_table' : '_name';
Branch: 1 branch
BranchTrueFalseTotal
@{$self->_update;} > 1 ? : 1 20 21
146  
147 return (
148 'SET ' . (
149 join ', ',
150 map {
151 21 my $val = $_->[1];
152 24 my $val_sql = $val->sql($dbh);
153 24 ▶ $val_sql = "($val_sql)"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($val) and $val->can("does") and $val->does("Fey::Role::SQL::ReturnsData") 1 23 24
Condition50%3/6
builtin::blessed($val) and $val->can("does") and $val->does("Fey::Role::SQL::ReturnsData")
!ll&&!rl&&r
0231
builtin::blessed($val) and $val->can("does")
!ll&&!rl&&r
0024
MC/DC33%1/3
builtin::blessed($val) and $val->can("does") and $val->does("Fey::Role::SQL::ReturnsData")
builtin::blessed($val) $val->can("does") $val->does("Fey::Role::SQL::ReturnsData")
Truth table50%2/4
A and B and C
A= builtin::blessed($val)B= $val->can("does")C= $val->does("Fey::Role::SQL::ReturnsData")
ABCresult
0XX0
10X0
1100
1111
154 if blessed $val
155 && $val->can('does')
156 && $val->does('Fey::Role::SQL::ReturnsData');
157 24 $self->$col_quote( $_->[0], $dbh ) . ' = ' . $val_sql;
158 } $self->_set_pairs()
159 )
160 );
161 }
162  
163 1 ▶sub _name_and_table {
Subroutine _name_and_table: called CC=1 SCAR=0.0
164 1 return $_[1]->sql( $_[2] );
165 }
166  
167 23 ▶sub _name {
Subroutine _name: called CC=1 SCAR=0.0
168 23 return $_[2]->quote_identifier( $_[1]->name() );
169 }
170  
171 5 ▶sub bind_params {
Subroutine bind_params: called CC=1 SCAR=0.0
Pod documented
172 5 my $self = shift;
173  
174 return (
175 5 $self->_update_bind_params(),
176 $self->_where_clause_bind_params(),
177 );
178 }
179  
180 27 __PACKAGE__->meta()->make_immutable();
181  
182 27 1;
183  
184 # ABSTRACT: Represents a UPDATE query
185  
186 __END__
« Fey/Schema.pm ↑ Summary Fey/Role/SQL/HasLimitClause.pm »