SQL/Amazon/Tables/Table.pm

filter: Statementstmt 5.6% 10 / 178 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 7.6% 3 / 39 Podpod 0.0% 0 / 35 totaltotal 3.5% 13 / 366 CC 62 SCAR 35.1
CC
62
Cov
4%
save_row
90.0
format_date
72.0
row_equals
72.0
CRAP 33.5
« SQL/Amazon/Statement.pm ↑ Summary SQL/Amazon/ReqFactory.pm »
1 #
2 # Copyright (c) 2005, Presicient Corp., USA
3 #
4 # Permission is granted to use this software according to the terms of the
5 # Artistic License, as specified in the Perl README file,
6 # with the exception that commercial redistribution, either
7 # electronic or via physical media, as either a standalone package,
8 # or incorporated into a third party product, requires prior
9 # written approval of the author.
10 #
11 # This software is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 #
15 # Presicient Corp. reserves the right to provide support for this software
16 # to individual sites under a separate (possibly fee-based)
17 # agreement.
18 #
19 # History:
20 #
21 # 2005-Jan-27 D. Arnold
22 # Coded.
23 #
24 package SQL::Amazon::Tables::Table;
25  
26 1 ▶use DBI qw(:sql_types);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use constant AMZN_CACHE_TIME_LIMIT => 1800;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 0 ▶sub get_time_limit { return AMZN_CACHE_TIME_LIMIT; }
Subroutine get_time_limit: not called CC=1 SCAR=6.9
Pod undocumented
32  
33 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
34 0 my ($class, $metadata) = @_;
35 0 ▶ my $obj = $metadata ? { %$metadata } : {};
Branch: 1 branch
BranchTrueFalseTotal
$metadata ? : 0 0 0
36 0 $obj->{_rows} = {};
37 0 $obj->{_readonly} = 1;
38 0 $obj->{_request_map} = {};
39  
40 0 ▶ if ($metadata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metadata) 0 0 0
41 0 $obj->{col_names} = $obj->{NAME};
42 0 my %colnums = ();
43 $colnums{$obj->{NAME}[$_]} = $_
44 0 foreach (0..$#{$obj->{NAME}});
45  
46 0 $obj->{col_nums} = \%colnums;
47 }
48 0 $obj->{_key_cols} = [ $obj->{col_nums}{ASIN} ];
49
50 0 bless $obj, $class;
51 0 return $obj;
52 }
53 0 ▶sub name {
Subroutine name: not called CC=2 SCAR=17.9
Pod undocumented
54 0 my $obj = shift;
55  
56 0 ▶ return (ref $obj=~/.+::(\S+)$/) ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj =~ /.+::(\S+)$/ ? : 0 0 0
57 }
58  
59 0 ▶sub is_readonly { return shift->{_readonly}; }
Subroutine is_readonly: not called CC=1 SCAR=6.9
Pod undocumented
60 0 ▶sub is_cacheonly { return shift->{_cache_only}; }
Subroutine is_cacheonly: not called CC=1 SCAR=6.9
Pod undocumented
61 0 ▶sub is_local { return shift->{_local}; }
Subroutine is_local: not called CC=1 SCAR=6.9
Pod undocumented
62 0 ▶sub debug { shift->{_debug} = shift; }
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
63  
64 0 ▶sub commit {
Subroutine commit: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my ($obj, $sql, $table) = @_;
66 0 1;
67 }
68  
69 0 ▶sub rollback {
Subroutine rollback: not called CC=1 SCAR=6.9
Pod undocumented
70 0 my ($obj, $sql, $table) = @_;
71 0 1;
72 }
73  
74 0 ▶sub trim {
Subroutine trim: not called CC=1 SCAR=6.9
Pod undocumented
75 0 my $x = shift;
76 0 $x =~ s/^\s+//;
77 0 $x =~ s/\s+$//;
78 0 $x;
79 }
80 0 ▶sub get_metadata {
Subroutine get_metadata: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my $obj = shift;
82
83 return {
84 NAME => $obj->{NAME},
85 TYPE => $obj->{TYPE},
86 PRECISION => $obj->{PRECISION},
87 SCALE => $obj->{SCALE},
88 NULLABLE => $obj->{NULLABLE}
89 0 };
90 }
91 0 ▶sub fetch {
Subroutine fetch: not called CC=5 SCAR=34.0
Pod undocumented
92 0 my($obj, $key) = @_;
93  
94 return undef
95 0 ▶ unless exists $obj->{_rows}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->{"_rows"}{$key} 0 0 0
96 0 ▶ unless (ref $obj->{_rows}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $obj->{"_rows"}{$key}) 0 0 0
97 0 $key .= "\0" . '1';
98 return undef
99 0 ▶ unless exists $obj->{_rows}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->{"_rows"}{$key} 0 0 0
100 }
101  
102 return $obj->{_rows}{$key}[0] > time() ?
103 0 ▶ $obj->{_rows}{$key}[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$obj->{'_rows'}{$key}[0] > time ? : 0 0 0
104 }
105 0 ▶sub fetch_all {
Subroutine fetch_all: not called CC=7 SCAR=40.3
Pod undocumented
106 0 my ($obj, $reqids) = @_;
107  
108 0 my $rows = $obj->{_rows};
109 0 my $reqmap = $obj->{_request_map};
110 0 ▶ $reqids = { %$reqmap }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reqids 0 0 0
111 unless defined($reqids);
112  
113 0 my %keys = ();
114 0 foreach my $reqid (keys %$reqids) {
115 0 my $reqkeys = $reqmap->{$reqid};
116 0 foreach (keys %$reqkeys) {
117 delete $reqkeys->{$_},
118 next
119 0 ▶ unless defined($rows->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rows->{$_} 0 0 0
120  
121 next
122 0 ▶ unless (ref $rows->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rows->{$_} 0 0 0
123  
124 $keys{$_} = 1,
125 next
126 0 ▶ if ($rows->{$_}[0] > time());
Branch: 1 branch
BranchTrueFalseTotal
if $rows->{$_}[0] > time 0 0 0
127  
128 0 delete $rows->{$_};
129 0 delete $reqkeys->{$_};# its timed out, get rid of it
130 }
131 }
132 0 my @keys = keys %keys;
133 0 return \@keys;
134 }
135 0 ▶sub format_date {
Subroutine format_date: not called CC=8 SCAR=42.8
Pod undocumented
136 0 my $date = shift;
137 0 ▶ $date = shift
Branch: 1 branch
BranchTrueFalseTotal
if ref $date 0 0 0
138 if ref $date;
139
140 0 ▶ return '****-**-**'
Branch: 1 branch
BranchTrueFalseTotal
unless $date =~ /^(\d{4})-(\d{1,2})(-(\d{1,2}))?$/ 0 0 0
141 unless ($date=~/^(\d{4})-(\d{1,2})(-(\d{1,2}))?$/);
142 0 my ($yr, $mo, $da) = ($1, $2, $4);
143 0 ▶ $mo = '0' . $mo
Branch: 1 branch
BranchTrueFalseTotal
unless length $mo > 1 0 0 0
144 unless (length($mo) > 1);
145 0 ▶ $da = defined($da) ?
Branch: 2 branches
BranchTrueFalseTotal
defined $da ? : 0 0 0
length $da < 2 ? : 0 0 0
146 (length($da) < 2) ? '0' . $da : $da :
147 '01';
148 0 ▶ return '****-**-**'
Branch: 1 branch
BranchTrueFalseTotal
unless $mo < 13 and $da < 32 0 0 0
Condition0%0/3
$mo < 13 and $da < 32
!ll&&!rl&&r
000
MC/DC0%0/2
$mo < 13 and $da < 32
$mo < 13 $da < 32
Truth table0%0/3
A and B
A= $mo < 13B= $da < 32
ABresult
0X0
100
111
149 unless (($mo < 13) && ($da < 32));
150  
151 0 return join('-', $yr, $mo, $da);
152 }
153 0 ▶sub format_money {
Subroutine format_money: not called CC=4 SCAR=30.0
Pod undocumented
154 0 my $amt = shift;
155 0 ▶ $amt = shift
Branch: 1 branch
BranchTrueFalseTotal
if ref $amt 0 0 0
156 if ref $amt;
157  
158 0 ▶ return '*********.**'
Branch: 1 branch
BranchTrueFalseTotal
unless $amt =~ /^-?[0-9]+$/ 0 0 0
159 unless ($amt=~/^-?[0-9]+$/);
160 0 ▶ $amt = '0' x (3 - length($amt)) . $amt
Branch: 1 branch
BranchTrueFalseTotal
if length $amt < 3 0 0 0
161 if (length($amt) < 3);
162  
163 0 substr($amt, -2, 0) = '.';
164 0 return $amt;
165 }
166  
167 0 ▶sub insert {
Subroutine insert: not called CC=5 SCAR=34.0
Pod undocumented
168 0 my ($obj, $item, $reqid) = @_;
169 0 my $names = $obj->{NAME};
170 0 my $types = $obj->{TYPE};
171 0 my @row = ();
172  
173 $row[$_] = exists $item->{$names->[$_]} ?
174 (($types->[$_] == SQL_DATE) ?
175 format_date($item->{$names->[$_]}) :
176 ($types->[$_] == SQL_DECIMAL) ?
177 format_money($item->{$names->[$_]}) :
178 $item->{$names->[$_]}) :
179 undef
180 0 ▶ foreach (0..$#$names);
Branch: 3 branches
BranchTrueFalseTotal
exists $$item{$names->[$_]} ? : 0 0 0
$types->[$_] == &SQL_DATE() ? : 0 0 0
$types->[$_] == &SQL_DECIMAL() ? : 0 0 0
181
182 0 return $obj->save_row(\@row, $item, $reqid);
183 }
184 0 ▶sub save_row {
Subroutine save_row: not called CC=9 SCAR=45.0
Pod undocumented
185 0 my ($obj, $row, $item, $reqid) = @_;
186 0 my @keyvals = ();
187 push @keyvals, (defined($row->[$_]) ? $row->[$_] : '')
188 0 ▶ foreach (@{$obj->{_key_cols}});
Branch: 1 branch
BranchTrueFalseTotal
defined $row->[$_] ? : 0 0 0
189  
190 0 my $key = join("\0", @keyvals);
191 my $expires = $obj->{_local} ?
192 0 ▶ 0x7FFFFFFF :
Branch: 1 branch
BranchTrueFalseTotal
$obj->{'_local'} ? : 0 0 0
193 time() + AMZN_CACHE_TIME_LIMIT;
194 0 my $rows = $obj->{_rows};
195 0 ▶ if ($rows->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows->{$key}) { } 0 0 0
196 0 ▶ if (ref $rows->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rows->{$key}) { } 0 0 0
197 0 ▶ if (row_equals($row, $rows->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (row_equals($row, $rows->{$key})) { } 0 0 0
198 0 $rows->{$key}[0] = $expires;
199 }
200 else {
201 0 my $oldkey = $key . "\0" . '1';
202 0 $rows->{$oldkey} = $rows->{$key};
203 0 $rows->{$key} = 2;
204  
205 0 $key .= "\0" . '2';
206 0 $rows->{$key} = [ $expires, $row ];
207 }
208 }
209 else {
210 0 my $uniquifier = $rows->{$key} + 1;
211 0 $rows->{$key} = $uniquifier;
212 0 $key .= "\0$uniquifier";
213 0 $rows->{$key} = [ $expires, $row ];
214 }
215 }
216 else {
217 0 $rows->{$key} = [ $expires, $row ];
218 }
219 0 $obj->{_request_map}{$reqid}{$key} = 1;
220 $obj->trace_insert($row, $item)
221 0 ▶ if $obj->{_debug} && defined($item);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"_debug"} and defined $item 0 0 0
Condition0%0/3
$obj->{"_debug"} and defined $item
!ll&&!rl&&r
000
MC/DC0%0/2
$obj->{"_debug"} and defined $item
$obj->{"_debug"} defined $item
Truth table0%0/3
A and B
A= $obj->{"_debug"}B= defined $item
ABresult
0X0
100
111
222  
223 0 return $row;
224 }
225  
226 0 ▶sub trace_insert {
Subroutine trace_insert: not called CC=5 SCAR=34.0
Pod undocumented
227 0 my ($obj, $row, $item) = @_;
228 0 my $names = $obj->{NAME};
229  
230 0 my ($tblname) = (ref $obj=~/::(\w+)$/);
231 0 foreach (@$names) {
232 warn "[SQL::Amazon::Tables::insert] Column $_ not supplied for table $tblname\n"
233 0 ▶ unless $row->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{$_} 0 0 0
234 }
235 0 foreach (keys %$item) {
236 warn "[SQL::Amazon::Tables::insert] Column $_ (value '$row->{$_}') not recognized for table $tblname\n"
237 0 ▶ unless defined($obj->{col_nums}{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj->{"col_nums"}{$_} 0 0 0
238 }
239 0 return $obj;
240 }
241 0 ▶sub compute_key {
Subroutine compute_key: not called CC=3 SCAR=24.8
Pod undocumented
242 0 my ($obj, $row) = @_;
243
244 0 my @keys = ();
245 push @keys, uc (defined($row->[$_]) ? $row->[$_] : '')
246 0 ▶ foreach (@{$obj->{_key_cols}});
Branch: 1 branch
BranchTrueFalseTotal
defined $row->[$_] ? : 0 0 0
247 0 return join("\0", @keys);
248 }
249  
250 0 ▶sub is_key_column {
Subroutine is_key_column: not called CC=5 SCAR=34.0
Pod undocumented
251 0 my ($obj, $colname) = @_;
252
253 0 ▶ unless ($colname=~/^[0-9]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($colname =~ /^[0-9]+$/) 0 0 0
254 0 $colname = $obj->{col_nums}{$colname};
255 return undef
256 0 ▶ unless defined($colname);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colname 0 0 0
257 }
258  
259 0 my $keycols = $obj->{_key_cols};
260 0 foreach (@$keycols) {
261 0 ▶ return $obj
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $colname 0 0 0
262 if ($_ == $colname);
263 }
264 0 return undef;
265 }
266  
267 0 ▶sub spoil {
Subroutine spoil: not called CC=1 SCAR=6.9
Pod undocumented
268 0 my ($obj, $id) = @_;
269
270 0 delete $obj->{_rows}{$id};
271 0 return $obj;
272 }
273  
274 0 ▶sub spoil_all {
Subroutine spoil_all: not called CC=1 SCAR=6.9
Pod undocumented
275 0 my $obj = shift;
276
277 0 $obj->{_rows} = {};
278 0 return $obj;
279 }
280 0 ▶sub row ($;$) {
Subroutine row: not called CC=3 SCAR=24.8
Pod undocumented
281 0 my($obj, $row) = @_;
282 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
283 0 $obj->{row} = $row;
284 }
285 else {
286 $obj->{row} = undef,
287 return undef
288 0 ▶ if ($obj->{_rows}{_currkey}[0] < time());
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"_rows"}{"_currkey"}[0] < time 0 0 0
289 0 return $obj->{row};
290 }
291 }
292  
293 0 ▶sub column ($$;$) {
Subroutine column: not called CC=2 SCAR=17.9
Pod undocumented
294 0 my($self, $column, $val) = @_;
295 0 ▶ if (@_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) { } 0 0 0
296 0 $self->{row}[$self->{col_nums}{$column}] = $val;
297 } else {
298 0 $self->{row}[$self->{col_nums}{$column}];
299 }
300 }
301  
302 0 ▶sub column_num ($$) {
Subroutine column_num: not called CC=1 SCAR=6.9
Pod undocumented
303 0 my($self, $col) = @_;
304 0 $self->{col_nums}{$col};
305 }
306  
307 0 ▶sub col_names ($) {
Subroutine col_names: not called CC=1 SCAR=6.9
Pod undocumented
308 0 shift->{col_names};
309 }
310  
311 0 ▶sub col_nums ($) {
Subroutine col_nums: not called CC=1 SCAR=6.9
Pod undocumented
312 0 shift->{col_nums};
313 }
314 0 ▶sub fetch_row ($$$) {
Subroutine fetch_row: not called CC=1 SCAR=6.9
Pod undocumented
315 0 my($obj, $handle, $row) = @_;
316 0 return undef;
317 }
318  
319 0 ▶sub push_names ($$$) {
Subroutine push_names: not called CC=1 SCAR=6.9
Pod undocumented
320 0 my($obj, $data, $names) = @_;
321  
322 0 return 1;
323 }
324  
325 0 ▶sub push_row ($$$) {
Subroutine push_row: not called CC=4 SCAR=30.0
Pod undocumented
326 0 my($obj, $data, $fields) = @_;
327  
328 0 ▶ return undef if $obj->{_readonly};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"_readonly"} 0 0 0
329 0 my $col_num = $obj->{col_nums};
330 0 my @keyvals = ();
331 push @keyvals, ($fields->[$col_num->{$_}] || '')
332 0 ▶ foreach (@{$obj->{_key_cols}});
Condition0%0/2
$$fields[$col_num->{$_}] || ''
l!l
00
MC/DC0%0/1
$$fields[$col_num->{$_}] || ''
$$fields[$col_num->{$_}]
Truth table0%0/2
A
A= $$fields[$col_num->{$_}]
Aresult
11
00
333  
334 0 $obj->{_rows}{join("\0", @keyvals)}[1] = $fields;
335 0 1;
336 }
337  
338 0 ▶sub seek ($$$$) {
Subroutine seek: not called CC=1 SCAR=6.9
Pod undocumented
339 0 my($obj, $data, $pos, $whence) = @_;
340 0 return 1;
341 }
342  
343 0 ▶sub drop ($$) {
Subroutine drop: not called CC=1 SCAR=6.9
Pod undocumented
344 0 my($obj, $data) = @_;
345 0 return undef;
346 }
347  
348 0 ▶sub truncate ($$) {
Subroutine truncate: not called CC=2 SCAR=17.9
Pod undocumented
349 0 my ($obj, $data) = @_;
350
351 0 ▶ return undef if $obj->{_readonly};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"_readonly"} 0 0 0
352
353 0 my $rowcnt = scalar keys %{$obj->{_rows}};
354 0 $obj->{_rows} = {};
355 0 return $rowcnt;
356 }
357 0 ▶sub purge_requests {
Subroutine purge_requests: not called CC=2 SCAR=17.9
Pod undocumented
358 0 my ($obj, $reqids) = @_;
359
360 0 my $reqmap = $obj->{_request_map};
361 delete $reqmap->{$_}
362 0 foreach (keys %$reqids);
363 0 return $obj;
364 }
365 0 ▶sub row_equals {
Subroutine row_equals: not called CC=8 SCAR=42.8
Pod undocumented
366 0 my ($row1, $row2) = @_;
367
368 0 ▶ return undef unless ($#$row1 == $#$row2);
Branch: 1 branch
BranchTrueFalseTotal
unless $row1->$#* == $row2->$#* 0 0 0
369 0 foreach (0..$#$row1) {
370 return undef
371 unless (
372 0 ▶ (defined($row1->[$_]) &&
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row1->[$_] and defined $row2->[$_] and $row1->[$_] eq $row1->[$_] or not defined $row1->[$_] || defined $row2->[$_] 0 0 0
Condition0%0/12
defined $row1->[$_] and defined $row2->[$_] and $row1->[$_] eq $row1->[$_] or not defined $row1->[$_] || defined $row2->[$_]
l!l&&r!l&&!r
000
defined $row1->[$_] and defined $row2->[$_] and $row1->[$_] eq $row1->[$_]
!ll&&!rl&&r
000
defined $row1->[$_] and defined $row2->[$_]
!ll&&!rl&&r
000
defined $row1->[$_] || defined $row2->[$_]
l!l&&r!l&&!r
000
MC/DC0%0/5
defined $row1->[$_] and defined $row2->[$_] and $row1->[$_] eq $row1->[$_] or not defined $row1->[$_] || defined $row2->[$_]
defined $row1->[$_] defined $row2->[$_] $row1->[$_] eq $row1->[$_] defined $row1->[$_] defined $row2->[$_]
Truth table0%0/10
A and B and C or not(D || E)
A= defined $row1->[$_]B= defined $row2->[$_]C= $row1->[$_] eq $row1->[$_]D= defined $row1->[$_]E= defined $row2->[$_]
ABCDEresult
111XX1
0XX001
10X001
110001
0XX1X0
0XX010
10X1X0
10X010
1101X0
110010
373 defined($row2->[$_]) &&
374 ($row1->[$_] eq $row1->[$_])
375 ) ||
376 (!defined($row1->[$_]) &&
377 !defined($row2->[$_])
378 )
379 );
380 }
381 0 return 1;
382 }
383 0 ▶sub DESTROY { undef; }
Subroutine DESTROY: not called CC=1 SCAR=6.9
384  
385 1 1;
386  
« SQL/Amazon/Statement.pm ↑ Summary SQL/Amazon/ReqFactory.pm »