blib/lib/DBIx/UpdateTable/FromHoH.pm

filter: Statementstmt 100% 106 / 106 Branchbran 77.7% 28 / 36 Conditioncond 52.6% 10 / 19 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 86.0% 154 / 179 CC 35 SCAR 32.1
CC
35
Cov
89%
update_table_from_hoh
31.5
_eq
6.7
BEGIN
1.0
CRAP 24.8
↑ Summary
1 package DBIx::UpdateTable::FromHoH;
2  
3 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 2 our $DATE = '2020-05-06'; # DATE
5 2 our $DIST = 'DBIx-UpdateTable-FromHoH'; # DIST
6 2 our $VERSION = '0.002'; # VERSION
7  
8 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 our @ISA = qw(Exporter);
15 2 our @EXPORT_OK = qw(
16 update_table_from_hoh
17 );
18  
19 2 our %SPEC;
20  
21 10 ▶sub _eq {
Subroutine _eq: called CC=5 SCAR=19.1
22 10 my ($v1, $v2) = @_;
23 10 my $v1_def = defined $v1;
24 10 my $v2_def = defined $v2;
25 10 ▶ return 1 if !$v1_def && !$v2_def;
Branch: 1 branch
BranchTrueFalseTotal
unless $v1_def or $v2_def 0 10 10
Condition33%1/3
$v1_def or $v2_def
l!l&&r!l&&!r
1000
MC/DC0%0/2
$v1_def or $v2_def
$v1_def $v2_def
Truth table33%1/3
A or B
A= $v1_defB= $v2_def
ABresult
1X1
011
000
26 10 ▶ return 0 if $v1_def xor $v2_def;
Branch: 1 branch
BranchTrueFalseTotal
if $v1_def xor $v2_def 0 10 10
Condition25%1/4
$v1_def xor $v2_def
l&&rl&&!r!l&&r!l&&!r
10000
MC/DC0%0/2
$v1_def xor $v2_def
$v1_def $v2_def
Truth table25%1/4
A xor B
A= $v1_defB= $v2_def
ABresult
110
101
011
000
27 10 $v1 eq $v2;
28 }
29  
30 $SPEC{update_table_from_hoh} = {
31 2 v => 1.1,
32 summary => 'Update database table from hash-of-hash',
33 description => <<'_',
34  
35 Given a table `t1` like this:
36  
37 id col1 col2 col3
38 -- ---- ---- ----
39 1 a b foo
40 2 c c bar
41 3 g h qux
42  
43 this code:
44  
45 my $res = update_table_from_hoh(
46 dbh => $dbh,
47 table => 't1',
48 key_column => 'id',
49 hoh => {
50 1 => {col1=>'a', col2=>'b'},
51 2 => {col1=>'c', col2=>'d'},
52 4 => {col1=>'e', col2=>'f'},
53 },
54 );
55  
56 will perform these SQL queries:
57  
58 UPDATE TABLE t1 SET col2='d' WHERE id='2';
59 INSERT INTO t1 (id,col1,col2) VALUES (4,'e','f');
60 DELETE FROM t1 WHERE id='3';
61  
62 to make table `t1` become like this:
63  
64 id col1 col2 col3
65 -- ---- ---- ----
66 1 a b foo
67 2 c d bar
68 4 e f qux
69  
70 _
71 args => {
72 dbh => {
73 schema => ['obj*'],
74 req => 1,
75 },
76 table => {
77 schema => 'str*',
78 req => 1,
79 },
80 hoh => {
81 schema => 'hoh*',
82 req => 1,
83 },
84 key_column => {
85 schema => 'str*',
86 req => 1,
87 },
88 data_columns => {
89 schema => ['array*', of=>'str*'],
90 },
91 use_tx => {
92 schema => 'bool*',
93 default => 1,
94 },
95 extra_insert_columns => {
96 schema => ['hos*'], # XXX or code
97 },
98 extra_update_columns => {
99 schema => ['hos*'], # XXX or code
100 },
101 },
102 };
103 2 ▶sub update_table_from_hoh {
Subroutine update_table_from_hoh: called CC=31 SCAR=34.5
Pod documented
104 2 my %args = @_;
105  
106 2 my $dbh = $args{dbh};
107 2 my $table = $args{table};
108 2 my $hoh = $args{hoh};
109 2 my $key_column = $args{key_column};
110 2 my $data_columns = $args{data_columns};
111 2 ▶ my $use_tx = $args{use_tx} // 1;
Condition50%1/2
$args{"use_tx"} // 1
l!l
02
MC/DC0%0/1
$args{"use_tx"} // 1
$args{"use_tx"}
Truth table50%1/2
A
A= $args{"use_tx"}
Aresult
11
00
112  
113 2 ▶ unless ($data_columns) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_columns) 2 0 2
114 2 my %columns;
115 2 for my $key (keys %$hoh) {
116 6 my $row = $hoh->{$key};
117 6 $columns{ $_ }++ for keys %$row;
118 }
119 2 $data_columns = [sort keys %columns];
120 }
121  
122 2 my @columns = @$data_columns;
123 2 ▶ push @columns, $key_column unless grep { $_ eq $key_column } @columns;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $key_column;} @columns 2 0 2
124 2 my $columns_str = join(",", @columns);
125  
126 2 ▶ $dbh->begin_work if $use_tx;
Branch: 1 branch
BranchTrueFalseTotal
if $use_tx 2 0 2
127  
128 2 my $hoh_table = {};
129 GET_ROWS: {
130 2 my $sth = $dbh->prepare("SELECT $columns_str FROM $table");
131 2 $sth->execute;
132 2 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 6 2 8
133 6 $hoh_table->{ $row->{$key_column} } = $row;
134 }
135 }
136 2 my $num_rows_unchanged = keys %$hoh_table;
137  
138 2 my $num_rows_deleted = 0;
139 DELETE: {
140 2 for my $key (sort keys %$hoh_table) {
141 6 ▶ unless (exists $hoh->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $hoh->{$key}) 1 5 6
142 1 $dbh->do("DELETE FROM $table WHERE $key_column=?", {}, $key);
143 1 $num_rows_deleted++;
144 1 $num_rows_unchanged--;
145 }
146 }
147 }
148  
149 2 my $num_rows_updated = 0;
150 UPDATE: {
151 2 for my $key (sort keys %$hoh) {
152 6 ▶ next unless exists $hoh_table->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hoh_table->{$key} 1 5 6
153 5 my @update_columns;
154 my @values;
155 5 for my $column (@columns) {
156 15 ▶ next if $column eq $key_column;
Branch: 1 branch
BranchTrueFalseTotal
if $column eq $key_column 5 10 15
157 10 ▶ unless (_eq($hoh_table->{$key}{$column}, $hoh->{$key}{$column})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_eq($hoh_table->{$key}{$column}, $hoh->{$key}{$column})) 1 9 10
158 1 push @update_columns, $column;
159 1 push @values, $hoh->{$key}{$column};
160 }
161 }
162 5 ▶ next unless @update_columns;
Branch: 1 branch
BranchTrueFalseTotal
unless @update_columns 4 1 5
163  
164 1 ▶ for my $column (keys %{ $args{extra_update_columns} // {}}) {
Condition50%1/2
$args{"extra_update_columns"} // {}
l!l
10
MC/DC0%0/1
$args{"extra_update_columns"} // {}
$args{"extra_update_columns"}
Truth table50%1/2
A
A= $args{"extra_update_columns"}
Aresult
11
00
165 1 ▶ next if grep { $column eq $_ } @columns;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$column eq $_;} @columns 0 1 1
166 1 push @update_columns, $column;
167 1 push @values, $args{extra_update_columns}{$column};
168 }
169  
170 $dbh->do("UPDATE $table SET ".
171 1 join(",", map {"$_=?"} @update_columns).
172 " WHERE $key_column=?",
173 {},
174 @values, $key);
175 1 $num_rows_updated++;
176 1 $num_rows_unchanged--;
177 }
178 }
179  
180 2 my $num_rows_inserted = 0;
181 INSERT: {
182 2 my @insert_columns = @columns;
183 2 ▶ my @extra_insert_columns = keys %{ $args{extra_insert_columns} // {} };
Condition100%2/2
$args{"extra_insert_columns"} // {}
l!l
11
MC/DC100%1/1
$args{"extra_insert_columns"} // {}
$args{"extra_insert_columns"}
Truth table100%2/2
A
A= $args{"extra_insert_columns"}
Aresult
11
00
184 2 ▶ for my $column (@extra_insert_columns) { push @insert_columns, $column unless grep { $_ eq $column } @insert_columns }
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $column;} @insert_columns 1 0 1
185  
186 2 my $insert_columns_str = join(",", @insert_columns);
187 2 my $placeholders_str = join(",", map {"?"} @insert_columns);
188 2 for my $key (sort keys %$hoh) {
189 6 ▶ unless (exists $hoh_table->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $hoh_table->{$key}) 1 5 6
190 1 my @values;
191 1 for my $column (@insert_columns) {
192 4 ▶ if ($column eq $key_column) {
Branch: 2 branches
BranchTrueFalseTotal
if ($column eq $key_column) { } 1 3 4
elsif (grep {$column eq $_;} @extra_insert_columns) { } 1 2 3
193 1 push @values, $key;
194 3 } elsif (grep { $column eq $_ } @extra_insert_columns) {
195 1 push @values, $args{extra_insert_columns}{$column};
196 } else {
197 2 push @values, $hoh->{$key}{$column};
198 }
199 }
200 1 $dbh->do("INSERT INTO $table ($insert_columns_str) VALUES ($placeholders_str)", {}, @values);
201 1 $num_rows_inserted++;
202 }
203 }
204 }
205  
206 2 ▶ $dbh->commit if $use_tx;
Branch: 1 branch
BranchTrueFalseTotal
if $use_tx 2 0 2
207  
208 2 ▶ [$num_rows_deleted || $num_rows_inserted || $num_rows_updated ? 200 : 304,
Branch: 1 branch
BranchTrueFalseTotal
$num_rows_deleted || $num_rows_inserted || $num_rows_updated ? : 1 1 2
Condition66%4/6
$num_rows_deleted or $num_rows_inserted or $num_rows_updated
l!l&&r!l&&!r
101
$num_rows_deleted or $num_rows_inserted
l!l&&r!l&&!r
101
MC/DC33%1/3
$num_rows_deleted or $num_rows_inserted or $num_rows_updated
$num_rows_deleted $num_rows_inserted $num_rows_updated
Truth table50%2/4
A or B or C
A= $num_rows_deletedB= $num_rows_insertedC= $num_rows_updated
ABCresult
1XX1
01X1
0011
0000
209 "OK",
210 {
211 num_rows_deleted => $num_rows_deleted,
212 num_rows_inserted => $num_rows_inserted,
213 num_rows_updated => $num_rows_updated,
214 num_rows_unchanged => $num_rows_unchanged,
215 }];
216 }
217  
218 2 1;
219 # ABSTRACT: Update database table from hash-of-hash
220  
221 __END__
↑ Summary