Crop/Object/Warehouse/Lang/SQL/Query/Update.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 62 CC 6 SCAR 25.9
CC
6
Cov
0%
print_sql
30.0
new
6.0
BEGIN
2.0
CRAP 13.4
1 package Crop::Object::Warehouse::Lang::SQL::Query::Update;
2 use base qw/ Crop::Object::Warehouse::Lang::SQL::Query /;
3  
4 =begin nd
5 Class: Crop::Object::Warehouse::Lang::SQL::Query::Update
6 The Update query.
7 =cut
8  
9 use v5.14;
10 use warnings;
11  
12 use Crop::Error;
13 use Crop::Object::Warehouse::Lang::SQL::Clause;
14  
15 use Crop::Debug;
16  
17 =begin nd
18 Variable: our %Attributes
19 Class members:
20  
21 clause - exemplar of <Crop::Object::Warehouse::Lang::SQL::Clause>
22 new_val - hash of $attr=>$newval
23 table - table name
24 =cut
25 our %Attributes = (
26 clause => undef,
27 new_val => undef,
28 table => undef,
29 );
30  
31 =begin nd
32 Constructor: new ( )
33 Build clause.
34
35 Returns:
36 $self
37 =cut
38 sub new {
39 my ($class, %in) = @_;
40 # debug 'CROPOBJECTWAREHOUSELANGSQLQUERYUPDATE_NEW_IN=', \%in;
41  
42 my $self = $class->SUPER::new(
43 %in,
44 exists $in{clause} ?
45 (clause => Crop::Object::Warehouse::Lang::SQL::Clause->new($in{clause}))
46 : ()
47 ,
48 );
49
50 $self;
51 }
52  
53 =begin nd
54 Method: print_sql ( )
55 Compose query prepared string based on inner presentation.
56
57 Returns:
58 the array of two element, SQL string, and arrayref of values for placeholders
59 =cut
60 sub print_sql {
61 my $self = shift;
62 # debug 'CROPOBJECTWAREHOUSELANGSQLQUERYUPDATE_PRINTSQL_SELF=', $self;
63
64 my $sql = "UPDATE $self->{table} SET ";
65
66 my (@set, @values);
67 while (my ($attr, $newval) = each %{$self->{new_val}}) {
68 if (ref $newval eq 'SCALAR') {
69 # ...;
70 push @set, "$attr = $$newval";
71 } else {
72 push @set, "$attr = ?";
73 push @values, $attr, $newval;
74 }
75 }
76 return warn 'OBJECT: Nothing to Update' unless @set;
77
78 {
79 local $" = ', ';
80 $sql .= "@set ";
81 }
82  
83 if ($self->{clause}) {
84 my ($where, $val) = $self->{clause}->print_sql;
85
86 if ($where) {
87 $sql .= " WHERE $where ";
88 push @values, @$val;
89 }
90 }
91
92 ($sql, \@values);
93 }
94  
95 1;