Foo/UpdateQuery.pm

filter: Statementstmt 77.8% 95 / 122 Branchbran 50.0% 22 / 44 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 81.2% 13 / 16 Podpod 0.0% 0 / 13 totaltotal 63.3% 133 / 210 CC 30 SCAR 17.7
CC
30
Cov
69%
InsertQuery
12.8
DoDelete
12.0
UpdateQuery
5.2
CRAP 5.8
« Foo.pm ↑ Summary Foo/SimpleQuery.pm »
1 package DBIx::Foo::UpdateQuery;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 ####################################################
9 # Constructor
10 #
11  
12 1 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
13  
14 1 my ($class, $table, $dbh) = @_;
15  
16 1 ▶ die "No Table name passed to UpdateQuery" unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 1 1
17  
18 1 my $self = {
19 table => $table,
20 dbh => $dbh,
21 fields => {},
22 keys => {},
23 debug => 0
24 };
25  
26 1 bless $self, $class;
27  
28 # use tied hashes to preserve the order of added fields
29 1 tie(%{$self->{fields}}, 'Tie::IxHash');
30 1 tie(%{$self->{keys}}, 'Tie::IxHash');
31  
32 1 return $self;
33 }
34  
35 ####################################################
36 # Properties
37 #
38  
39 1 ▶sub UpdateQuery {
Subroutine UpdateQuery: called CC=5 SCAR=16.4
Pod undocumented
40  
41 1 my $self = $_[0];
42  
43 1 my $field_list = "";
44 1 my @args;
45  
46 1 for my $field (keys %{$self->{fields}})
47 {
48 3 ▶ $field_list .= ", " if $field_list;
Branch: 1 branch
BranchTrueFalseTotal
if $field_list 2 1 3
49  
50 3 my $value = $self->{fields}->{$field};
51  
52 # Temp fix for current_timestamp as a param
53 3 ▶ if ($value && $value eq "current_timestamp()") {
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $value eq 'current_timestamp()') { } 0 3 3
Condition33%1/3
$value and $value eq "current_timestamp()"
!ll&&!rl&&r
030
MC/DC0%0/2
$value and $value eq "current_timestamp()"
$value $value eq "current_timestamp()"
Truth table33%1/3
A and B
A= $valueB= $value eq "current_timestamp()"
ABresult
0X0
100
111
54  
55 0 $field_list .= "$field = current_timestamp()";
56  
57 } else {
58  
59 3 $field_list .= "$field = ?";
60  
61 3 push @args, $value;
62 }
63 }
64  
65 1 my ($where, @keys) = $self->WhereClause();
66  
67 1 my $query = "update " . $self->{table} . " set $field_list $where;";
68  
69 #warn $query . " : " . join(",", @args, @keys);
70  
71 1 return $query, @args, @keys;
72  
73 }
74  
75 2 ▶sub InsertQuery {
Subroutine InsertQuery: called CC=9 SCAR=25.5
Pod undocumented
76  
77 2 my $self = $_[0];
78  
79 2 my $field_list = "";
80 2 my $value_list = "";
81 2 my @args;
82  
83 2 for my $field (keys %{$self->{fields}})
84 {
85 6 ▶ $field_list .= ", " if $field_list;
Branch: 1 branch
BranchTrueFalseTotal
if $field_list 4 2 6
86 6 ▶ $value_list .= ", " if $field_list;
Branch: 1 branch
BranchTrueFalseTotal
if $field_list 4 2 6
87  
88 6 $field_list .= $field;
89  
90 6 my $value = $self->{fields}->{$field};
91  
92 # Temp fix for current_timestamp as a param
93 6 ▶ if ($value && $value eq "current_timestamp()") {
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $value eq 'current_timestamp()') { } 0 6 6
Condition33%1/3
$value and $value eq "current_timestamp()"
!ll&&!rl&&r
060
MC/DC0%0/2
$value and $value eq "current_timestamp()"
$value $value eq "current_timestamp()"
Truth table33%1/3
A and B
A= $valueB= $value eq "current_timestamp()"
ABresult
0X0
100
111
94  
95 0 $value_list .= "current_timestamp()";
96  
97 } else {
98  
99 6 $value_list .= "?";
100  
101 6 push @args, $value;
102 }
103 }
104  
105 2 for my $field (keys %{$self->{keys}})
106 {
107 0 ▶ $field_list .= ", " if $field_list;
Branch: 1 branch
BranchTrueFalseTotal
if $field_list 0 0 0
108 0 ▶ $value_list .= ", " if $field_list;
Branch: 1 branch
BranchTrueFalseTotal
if $field_list 0 0 0
109  
110 0 $field_list .= $field;
111 0 $value_list .= "?";
112  
113 0 push @args, $self->{keys}->{$field};
114 }
115  
116 2 my $query = "insert into " . $self->{table} . " ( $field_list ) values ( $value_list );";
117  
118 2 return $query, @args;
119 }
120  
121 0 ▶sub DeleteQuery {
Subroutine DeleteQuery: not called CC=1 SCAR=6.9
Pod undocumented
122  
123 0 my $self = $_[0];
124  
125 0 my ($where, @keys) = $self->WhereClause();
126  
127 0 my $query = "delete from " . $self->{table} . " $where";
128  
129 0 return $query, @keys;
130 }
131  
132 sub WhereClause
133 1 ▶{
Subroutine WhereClause: called CC=4 SCAR=14.0
Pod undocumented
134 1 my $self = $_[0];
135  
136 1 my $where = "";
137 1 my @args;
138  
139 1 for my $field (keys %{$self->{keys}})
140 {
141 1 ▶ $where .= " and " if $where;
Branch: 1 branch
BranchTrueFalseTotal
if $where 0 1 1
142  
143 1 $where .= "$field = ?";
144  
145 1 push @args, $self->{keys}->{$field};
146 }
147  
148 1 ▶ $where = "where $where" if $where;
Branch: 1 branch
BranchTrueFalseTotal
if $where 1 0 1
149  
150 1 return $where, @args;
151 }
152  
153  
154 ####################################################
155 # Methods
156 #
157  
158 1 ▶sub addKey {
Subroutine addKey: called CC=1 SCAR=0.0
Pod undocumented
159  
160 1 my ($self, $field, $value) = @_;
161  
162 1 $self->{keys}->{$field} = $value;
163 }
164  
165 3 ▶sub addField {
Subroutine addField: called CC=3 SCAR=12.5
Pod undocumented
166  
167 3 my ($self, $field, $value) = @_;
168  
169 3 ▶ $value = undef if $value && $value eq ''; # MT - not sure about this! Maybee remove this line?
Branch: 1 branch
BranchTrueFalseTotal
if $value and $value eq "" 0 3 3
Condition33%1/3
$value and $value eq ""
!ll&&!rl&&r
030
MC/DC0%0/2
$value and $value eq ""
$value $value eq ""
Truth table33%1/3
A and B
A= $valueB= $value eq ""
ABresult
0X0
100
111
170  
171 3 $self->{fields}->{$field} = $value;
172 }
173  
174 2 ▶sub addFields {
Subroutine addFields: called CC=4 SCAR=14.2
Pod undocumented
175  
176 2 my $self = shift;
177  
178 2 my $fields;
179  
180 2 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 2 2
181  
182 0 $fields = $_[0];
183  
184 } else {
185  
186 2 $fields = {@_};
187 }
188  
189 2 ▶ while (my ($field, $value) = each(%$fields)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$fields) 6 2 8
190  
191 6 ▶ $value = undef if $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 0 6 6
192  
193 6 $self->{fields}->{$field} = $value;
194 }
195 }
196  
197 2 ▶sub DoInsert {
Subroutine DoInsert: called CC=3 SCAR=11.1
Pod undocumented
198  
199 2 my ($self, $dbh) = @_;
200  
201 2 ▶ $dbh = $self->{dbh} unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 2 0 2
202  
203 2 my ($sql, @args) = $self->InsertQuery();
204  
205 2 my $rows;
206  
207 2 $dbh->do($sql, {}, @args);
208  
209 2 log_query($dbh, $sql, \@args);
210  
211 2 ▶ return 0 if $dbh->err;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->err 0 2 2
212  
213 2 my $newid = $dbh->last_insert_id(undef, undef, $self->{table}, undef);
214  
215 2 $log->info("Insert ID: $newid");
216  
217 2 return $newid;
218 }
219  
220 1 ▶sub DoUpdate {
Subroutine DoUpdate: called CC=4 SCAR=14.7
Pod undocumented
221  
222 1 my ($self, $dbh) = @_;
223  
224 1 ▶ $dbh = $self->{dbh} unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 1 0 1
225  
226 1 my ($sql, @args) = $self->UpdateQuery();
227  
228 1 ▶ unless (keys %{$self->{keys}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$$self{"keys"};}) 0 1 1
229  
230 0 $log->error("Update query with no keys not allowed: $sql (" . join(",", @args) . ")");
231  
232 0 return undef;
233 }
234  
235 1 my $rows = $dbh->do($sql, {}, @args);
236  
237 1 log_query($dbh, $sql, \@args);
238  
239 1 ▶ return 0 if $dbh->err;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->err 0 1 1
240  
241 1 $log->info("Updated: $rows");
242  
243 1 return($rows);
244 }
245  
246 0 ▶sub DontUpdate {
Subroutine DontUpdate: not called CC=1 SCAR=6.9
Pod undocumented
247  
248 # for testing: don't do anything, just show SQL update statement & its args
249  
250 0 my ($self, $dbh) = @_;
251  
252 0 my ($sql, @args) = $self->UpdateQuery();
253  
254 0 warn "UpdateQuery->DoUpdate: $sql " . join(",", @args);
255  
256 0 return 1;
257 }
258  
259 0 ▶sub DoDelete {
Subroutine DoDelete: not called CC=3 SCAR=24.8
Pod undocumented
260  
261 0 my ($self, $dbh) = @_;
262  
263 0 ▶ $dbh = $self->{dbh} unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 0 0
264  
265 0 my ($sql, @args) = $self->DeleteQuery();
266  
267 0 my $rows = $dbh->do($sql, {}, @args);
268  
269 0 log_query($dbh, $sql, \@args);
270  
271 0 ▶ return 0 if $dbh->err;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->err 0 0 0
272  
273 0 $log->info("Deleted: $rows");
274  
275 0 return $rows;
276 }
277  
278  
279 sub log_query
280 3 ▶{
Subroutine log_query: called CC=2 SCAR=7.4
Pod undocumented
281 3 my ($dbh, $sql, $args) = @_;
282  
283 # use the 'caller' function name to work out context
284 3 my $caller = ( caller(2) )[3];
285  
286 3 ▶ if ($dbh->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->err) { } 0 3 3
287  
288 0 $log->error($dbh->errstr . " - $sql (" . join(",", @$args) . ") called by $caller");
289  
290 } else {
291  
292 3 $log->debug("$sql (" . join(",", @$args) . ") called by $caller");
293 }
294 }
295  
296  
297 ####################################################
298 # End of Package
299 ####################################################
300  
301 4 1;
302  
« Foo.pm ↑ Summary Foo/SimpleQuery.pm »