blib/lib/Games/Go/AGA/TDListDB.pm

filter: Statementstmt 76.5% 215 / 281 Branchbran 50.0% 63 / 126 Conditioncond 50.0% 5 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 81.4% 44 / 54 Podpod 85.7% 30 / 35 totaltotal 69.9% 358 / 512 SCAR 54.2
CC
69
Cov
68%
update_from_AGA
90.0
run
20.0
update_from_file
15.8
CRAP 227.0
↑ Summary
1 #===============================================================================
2 # FILE: Games::Go::AGA::TDListDB
3 # ABSTRACT: an SQL object for holding AGA TDList data
4 # AUTHOR: Reid Augustin (REID), <reid@lucidport.com>
5 # COMPANY: LucidPort Technology, Inc.
6 # CREATED: 12/02/2010 08:51:22 AM PST
7 #===============================================================================
8  
9 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Games::Go::AGA::TDListDB;
14 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 our @EXPORT_OK = qw(
16 LAST_NAME
17 FIRST_NAME
18 ID
19 MEMBERSHIP
20 RANK
21 DATE
22 CLUB
23 STATE
24 EXTRA
25 );
26 1 our %EXPORT_TAGS = (all => \@EXPORT_OK);
27  
28 1 ▶use open qw( :utf8 :std ); # UTF8 for all files and STDIO
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use IO::Handle; # for autoflush
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use Games::Go::AGA::Parse::TDList;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use Games::Go::AGA::Parse::Util qw( is_Rating );
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 our $VERSION = '0.032'; # VERSION
41  
42 23 ▶sub LAST_NAME { 0 };
Subroutine LAST_NAME: called CC=1 SCAR=0.0
Pod documented
43 10 ▶sub FIRST_NAME { 1 };
Subroutine FIRST_NAME: called CC=1 SCAR=0.0
Pod documented
44 61 ▶sub ID { 2 };
Subroutine ID: called CC=1 SCAR=0.0
Pod documented
45 0 ▶sub MEMBERSHIP { 3 };
Subroutine MEMBERSHIP: not called CC=1 SCAR=6.9
Pod documented
46 19 ▶sub RANK { 4 };
Subroutine RANK: called CC=1 SCAR=0.0
Pod documented
47 0 ▶sub DATE { 5 };
Subroutine DATE: not called CC=1 SCAR=6.9
Pod documented
48 0 ▶sub CLUB { 6 };
Subroutine CLUB: not called CC=1 SCAR=6.9
Pod documented
49 0 ▶sub STATE { 7 };
Subroutine STATE: not called CC=1 SCAR=6.9
Pod documented
50 0 ▶sub EXTRA { 8 };
Subroutine EXTRA: not called CC=1 SCAR=6.9
Pod documented
51  
52 1 Readonly my @columns => (
53 {last_name => 'VARCHAR(256)', },
54 {first_name => 'VARCHAR(256)', },
55 {id => 'VARCHAR(256) NOT NULL PRIMARY KEY', },
56 {membership => 'VARCHAR(256)', },
57 {rank => 'VARCHAR(256)', },
58 {date => 'VARCHAR(256)', },
59 {club => 'VARCHAR(256)', },
60 {state => 'VARCHAR(256)', },
61 {extra => 'VARCHAR(256)', },
62 );
63  
64 my %pre_defaults = (
65 url => 'https://www.usgo.org/ratings/TDListN.txt',
66 dbdname => 'tdlistdb.sqlite',
67 table_name => 'tdlist',
68 extra_columns => [],
69 0 ▶ extra_columns_callback => sub { return () },
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 1 max_update_errors => 10,
71 raw_filename => 'TDList.txt',
72 verbose => 0,
73 );
74  
75 1 ▶__PACKAGE__->run( @ARGV ) if not caller(); # modulino
Branch: 1 branch
BranchTrueFalseTotal
unless caller() 0 1 1
76  
77 sub new {
78 1 ▶ my ($class, %args) = @_;
Subroutine new: called CC=6 SCAR=19.1
Pod documented
79  
80 1 my $self = {};
81 1 ▶ bless $self, (ref $class || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
82  
83 1 ▶ while (my ($key, $value) = each %pre_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %pre_defaults) 8 1 9
84 8 ▶ $value = delete $args{$key} if (exists $args{$key});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} 4 4 8
85 8 $self->$key($value);
86 }
87  
88 1 my $db = $self->db(delete $args{db});
89  
90 1 for my $key (keys %args) { # any leftovers?
91 0 $self->$key($args{$key});
92 }
93  
94 # SQL for finding players by name
95 1 $self->sth('select_by_name',
96 $db->prepare(
97 join('',
98 'SELECT * FROM ',
99 $self->table_name,
100 ' WHERE last_name = ?',
101 ' AND first_name = ?',
102 ),
103 ),
104 );
105  
106 # and a statement for inserting new players
107 1 $self->sth('insert_player',
108 $db->prepare(
109 join('',
110 'INSERT INTO ',
111 $self->table_name,
112 ' ( ',
113 $self->sql_columns,
114 ' ) ',
115 'VALUES ( ',
116 $self->sql_insert_qs,
117 ' )',
118 ),
119 ),
120 );
121  
122 # SQL for updating when player is already in DB
123 1 $self->sth('update_id',
124 $db->prepare(
125 join('',
126 'UPDATE ',
127 $self->table_name,
128 ' SET ',
129 $self->sql_update_qs,
130 ' WHERE id = ?',
131 ),
132 ),
133 );
134  
135 # SQL for finding IDs
136 1 $self->sth('select_id',
137 $db->prepare(
138 join('',
139 'SELECT * FROM ',
140 $self->table_name,
141 ' WHERE id = ?',
142 ),
143 ),
144 );
145  
146 # SQL for getting and setting DB update time
147 1 $self->sth('select_time',
148 $db->prepare(
149 join('',
150 'SELECT update_time FROM ',
151 $self->table_name_meta,
152 ' WHERE key = 1',
153 ),
154 ),
155 );
156 1 $self->sth('update_time',
157 $db->prepare(
158 join('',
159 'UPDATE ',
160 $self->table_name_meta,
161 ' SET update_time = ?',
162 ' WHERE key = 1',
163 ),
164 ),
165 );
166  
167 # SQL to get/set next_tmp marker
168 1 $self->sth('select_next_tmp',
169 $db->prepare(
170 join('',
171 'SELECT next_tmp_id FROM ',
172 $self->table_name_meta,
173 ' WHERE key = 1',
174 ),
175 ),
176 );
177 1 $self->sth('update_next_tmp',
178 $db->prepare(
179 join('',
180 'UPDATE ',
181 $self->table_name_meta,
182 ' SET next_tmp_id = ?',
183 ' WHERE key = 1',
184 ),
185 )
186 );
187  
188 1 $self->init(\%args); # in case any subclass needs initialization
189  
190 map {
191 1 ▶ if (not $self->can($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($_)) 0 0 0
192 0 my $ref = ref $self;
193 0 confess("$ref can't '->$_'\n");
194 }
195 0 $self->$_($args{$_});
196 } keys %args;
197  
198 1 return $self;
199 }
200  
201 sub run {
202 0 ▶ my ($class) = @_;
Subroutine run: not called CC=4 SCAR=30.0
Pod undocumented
203  
204 0 my $tdlist = $class->new();
205 0 my $filename = $tdlist->raw_filename;
206 0 my $dbfile = $tdlist->dbdname;
207 0 STDOUT->autoflush(1);
208  
209 0 ▶ if ($ARGV[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0]) 0 0 0
210 0 ▶ if ($ARGV[0] eq 'AGA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] eq "AGA") 0 0 0
211 0 my $url = $tdlist->url;
212 0 print "Updating $dbfile from AGA ($url)\n";
213 0 $tdlist->update_from_AGA();
214 0 exit;
215 }
216 0 ▶ $filename = $ARGV[0] if (-f $ARGV[0]);
Branch: 1 branch
BranchTrueFalseTotal
if -f $ARGV[0] 0 0 0
217 }
218 0 print "Updating $dbfile from file ($filename)\n";
219 0 $tdlist->update_from_file($filename);
220 }
221  
222 # stub for subclass to override
223 sub init {
224 1 ▶ my ($self) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
225 }
226  
227 sub verbose {
228 5 ▶ my ($self, $new) = @_;
Subroutine verbose: called CC=2 SCAR=6.9
Pod undocumented
229  
230 5 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 4 5
231 1 $self->{verbose} = $new;
232 }
233  
234 5 return $self->{verbose};
235 }
236  
237 sub raw_filename {
238 1 ▶ my ($self, $new) = @_;
Subroutine raw_filename: called CC=2 SCAR=7.0
Pod documented
239  
240 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 0 1
241 1 $self->{raw_filename} = $new;
242 }
243  
244 1 return $self->{raw_filename};
245 }
246  
247 sub dbdname {
248 2 ▶ my ($self, $new) = @_;
Subroutine dbdname: called CC=2 SCAR=6.9
Pod documented
249  
250 2 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 1 2
251 1 $self->{dbdname} = $new;
252 }
253  
254 2 return $self->{dbdname};
255 }
256  
257 sub table_name {
258 12 ▶ my ($self, $new) = @_;
Subroutine table_name: called CC=2 SCAR=6.9
Pod documented
259  
260 12 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 11 12
261 1 $self->{table_name} = $new;
262 }
263  
264 12 return quotemeta $self->{table_name};
265 }
266  
267 sub table_name_meta {
268 6 ▶ my ($self) = @_;
Subroutine table_name_meta: called CC=1 SCAR=0.0
Pod undocumented
269  
270 6 return $self->table_name . '_meta';
271 }
272  
273 sub url {
274 1 ▶ my ($self, $new) = @_;
Subroutine url: called CC=2 SCAR=7.0
Pod documented
275  
276 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 0 1
277 1 $self->{url} = $new;
278 }
279  
280 1 return $self->{url};
281 }
282  
283 sub background {
284 2 ▶ my ($self, $new) = @_;
Subroutine background: called CC=2 SCAR=6.9
Pod documented
285  
286 2 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 1 2
287 1 $self->{background} = $new;
288 }
289  
290 2 return $self->{background};
291 }
292  
293 sub max_update_errors {
294 19 ▶ my ($self, $new) = @_;
Subroutine max_update_errors: called CC=2 SCAR=6.9
Pod documented
295  
296 19 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 18 19
297 1 $self->{max_update_errors} = $new;
298 }
299  
300 19 return $self->{max_update_errors};
301 }
302  
303 sub extra_columns_callback {
304 14 ▶ my ($self, @new) = @_;
Subroutine extra_columns_callback: called CC=3 SCAR=11.2
Pod documented
305  
306 14 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 13 14
307 1 ▶ if (ref $new[0] ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new[0] ne "CODE") 0 1 1
308 0 croak("Must set a code-ref in extra_columns_callback\n");
309 }
310 1 $self->{extra_columns_callback} = $new[0];
311 }
312 14 return $self->{extra_columns_callback};
313 }
314  
315 sub extra_columns {
316 5 ▶ my ($self, @new) = @_;
Subroutine extra_columns: called CC=4 SCAR=14.0
Pod documented
317  
318 5 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 4 5
319 1 ▶ if (ref $new[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new[0] eq 'ARRAY') { } 1 0 1
320 1 $self->{extra_columns} = $new[0];
321 }
322 else {
323 0 $self->{extra_columns} = \@new;
324 }
325 }
326 5 ▶ return wantarray ? @{$self->{extra_columns}} : $self->{extra_columns};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 1 5
327 }
328  
329 sub db {
330 13 ▶ my ($self, $new) = @_;
Subroutine db: called CC=4 SCAR=14.2
Pod documented
331  
332 13 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 12 13
333 1 ▶ if (not $new) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new) 1 0 1
334 1 ▶ if (my $fname = $self->dbdname) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fname = $self->dbdname) { } 1 0 1
335 1 $new = DBI->connect( # connect to your database, create if needed
336 "dbi:SQLite:dbname=$fname", # DSN: dbi, driver, database file
337 "", # no user
338 "", # no password
339 {
340 AutoCommit => 1,
341 RaiseError => 1, # complain if something goes wrong
342 },
343 )
344 }
345 else {
346 0 croak("No dbdname for SQLite file\n");
347 }
348 }
349 1 $self->{db} = $new;
350 1 $self->_db_schema(); # make sure table exists
351 }
352  
353 13 return $self->{db};
354 }
355  
356 # library of statement handles
357 sub sth {
358 104 ▶ my ($self, $name, $new) = @_;
Subroutine sth: called CC=4 SCAR=14.0
Pod documented
359  
360 104 ▶ croak("Statement handle name is required\n") if (not $name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 104 104
361 104 ▶ if (@_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 8 96 104
362 8 $self->{sth}{$name} = $new;
363 }
364  
365 104 my $sth = $self->{sth}{$name};
366 104 ▶ croak("No statement handle called '$name'\n") if (not $sth);
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 104 104
367  
368 104 return $sth;
369 }
370  
371 sub _db_schema {
372 1 ▶ my ($self) = @_;
Subroutine _db_schema: called CC=1 SCAR=0.0
373  
374 1 $self->db->do(
375 join('',
376 'CREATE TABLE IF NOT EXISTS ',
377 $self->table_name,
378 ' (',
379 $self->sql_column_types,
380 ' )',
381 ),
382 );
383  
384 1 $self->db->do(join '',
385 'CREATE TABLE IF NOT EXISTS ',
386 $self->table_name_meta,
387 ' (',
388 'key INTEGER PRIMARY KEY, ',
389 'update_time VARCHAR(12), ',
390 'next_tmp_id VARCHAR(12)',
391 ' )',
392 );
393  
394 1 $self->db->do(join '',
395 'INSERT OR IGNORE INTO ',
396 $self->table_name_meta,
397 ' (',
398 'key, ',
399 'update_time, ',
400 'next_tmp_id',
401 ' ) VALUES ( 1, 0, 1 )',
402 );
403 }
404  
405 sub update_time {
406 6 ▶ my ($self, $new) = @_;
Subroutine update_time: called CC=3 SCAR=11.0
Pod documented
407  
408 6 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 4 2 6
409 4 $self->sth('update_time')->execute($new);
410 }
411 6 $self->sth('select_time')->execute();
412 6 my $time = $self->sth('select_time')->fetchall_arrayref();
413 6 $time = $time->[0][0];
414 6 ▶ return $time || 0;
Condition50%1/2
$time || 0
l!l
60
MC/DC0%0/1
$time || 0
$time
Truth table50%1/2
A
A= $time
Aresult
11
00
415 }
416  
417 sub select_id {
418 19 ▶ my ($self, $id) = @_;
Subroutine select_id: called CC=3 SCAR=11.8
Pod documented
419  
420 19 $self->sth('select_id')->execute($id);
421 # ID is primary index, so can only be one - fetch into first array
422 # element:
423 19 my ($player) = $self->sth('select_id')->fetchall_arrayref;
424 19 ▶ $player->[RANK] += 0 if (is_Rating($player->[RANK])); # numify ratings
Branch: 1 branch
BranchTrueFalseTotal
if is_Rating($$player[RANK()]) 0 19 19
425 return wantarray
426 19 ▶ ? @{$player->[0]}
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 19 19
427 : $player->[0];
428 }
429  
430 sub insert_player {
431 6 ▶ my ($self, @new) = @_;
Subroutine insert_player: called CC=3 SCAR=11.4
Pod undocumented
432  
433 6 ▶ $new[ID] = $self->next_tmp_id(1) if (not $new[ID]);
Branch: 1 branch
BranchTrueFalseTotal
unless $new[ID()] 0 6 6
434 6 $self->sth('insert_player')->execute(@new);
435 return wantarray
436 ? @new
437 6 ▶ : \@new;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
438 }
439  
440 sub next_tmp_id {
441 5 ▶ my ($self, $used) = @_;
Subroutine next_tmp_id: called CC=4 SCAR=14.5
Pod documented
442  
443 5 $self->sth('select_next_tmp')->execute;
444 5 my $next_tmp = $self->sth('select_next_tmp')->fetchall_arrayref;
445 5 $next_tmp = $next_tmp->[0][0];
446 5 ▶ $next_tmp ||= 1;
Condition50%1/2
$next_tmp ||= 1
l!l
50
MC/DC0%0/1
$next_tmp ||= 1
$next_tmp
Truth table50%1/2
A
A= $next_tmp
Aresult
11
00
447 5 ▶ while ($self->select_id("TMP$next_tmp")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->select_id("TMP$next_tmp")) 0 5 5
448 0 $next_tmp++
449 }
450  
451 5 ▶ if ($used) { # is the caller planning on allocating this one?
Branch: 1 branch
BranchTrueFalseTotal
if ($used) 5 0 5
452 5 $self->sth('update_next_tmp')->execute($next_tmp + 1);
453 }
454 5 return "TMP$next_tmp";
455 }
456  
457 # reap any child zombies from earlier update_from_AGA calls
458 sub reap {
459 0 ▶ my $kid;
Subroutine reap: not called CC=3 SCAR=24.8
Pod documented
460 0 my $reaped = 0;
461 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$kid > 0 and do { do { $kid = waitpid(-1, 1); ++$reaped if $kid > 0 } } 0 0 0
462 0 $kid = waitpid(-1, WNOHANG);
463 0 ▶ $reaped++ if ($kid > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $kid > 0 0 0 0
464 } while $kid > 0;
465 0 return $reaped;
466 }
467  
468 sub update_from_AGA {
469 0 ▶ my ($self) = @_;
Subroutine update_from_AGA: not called CC=9 SCAR=45.0
Pod documented
470  
471 0 my $pid;
472 0 ▶ if ($self->background) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->background) 0 0 0
473 0 $pid = fork;
474 0 ▶ die "fork failed: $!\n" if not defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
475 }
476 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
477 # parent process
478 0 return;
479 }
480  
481 0 ▶ if (not $self->{ua}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ua"}) 0 0 0
482 0 $self->{ua} = LWP::UserAgent->new;
483 }
484  
485 0 my $fname = $self->raw_filename;
486 0 ▶ print "Starting $fname fetch at ", scalar localtime, "\n" if ($self->verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
487 0 $self->{ua}->mirror($self->url, $fname);
488 0 ▶ print "... fetch done at ", scalar localtime, "\n" if ($self->verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
489 0 my $fh;
490 0 ▶ open($fh, '<', $fname)
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $fname or croak("Error opening $fname for reading: $!\n") 0 0 0
491 or croak("Error opening $fname for reading: $!\n");
492 0 $self->update_from_file($fh);
493 0 close $fh;
494  
495 0 ▶ exit if (defined $pid); # exit if this is a spawned child ($pid == 0)
Branch: 1 branch
BranchTrueFalseTotal
if defined $pid 0 0 0
496 }
497  
498 sub update_from_file {
499 4 ▶ my ($self, $fh) = @_;
Subroutine update_from_file: called CC=11 SCAR=27.6
Pod documented
500  
501 4 ▶ if (not ref $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 0 4 4
502 0 my $fname = $fh;
503 0 $fh = undef;
504 0 ▶ if (not open($fh, '<', $fname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $fname) 0 0 0
505 0 croak("Error opening $fname for reading: $!\n");
506 }
507 }
508  
509 4 my $parser = Games::Go::AGA::Parse::TDList->new();
510 4 my $verbose = $self->verbose;
511 4 ▶ print "Starting database update at ", scalar localtime, "\n" if ($verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 4 4
512 4 $self->db->do('BEGIN');
513 4 my @errors;
514 4 my $ii = 0;
515 4 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 18 4 22
516 18 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 18 18
517 0 ▶ print '.' if (++$ii % 1000 == 0);
Branch: 1 branch
BranchTrueFalseTotal
if ++$ii % 1000 == 0 0 0 0
518 0 ▶ print "\n" if ($ii % 40000 == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $ii % 40000 == 0 0 0 0
519 }
520 try { # in case a line crashes, print error but continue
521 #print "parse $line" if ($verbose);
522 18 ▶ $parser->parse($line);
Subroutine __ANON__: called CC=9 SCAR=22.1
523 18 my $update = $parser->as_array;
524 18 ▶ if ($update->[LAST_NAME] or $update->[FIRST_NAME]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$update[LAST_NAME()] or $$update[FIRST_NAME()]) 13 5 18
Condition66%2/3
$$update[LAST_NAME()] or $$update[FIRST_NAME()]
l!l&&r!l&&!r
1305
MC/DC50%1/2
$$update[LAST_NAME()] or $$update[FIRST_NAME()]
$$update[LAST_NAME()] $$update[FIRST_NAME()]
Truth table66%2/3
A or B
A= $$update[LAST_NAME()]B= $$update[FIRST_NAME()]
ABresult
1X1
011
000
525 13 push @{$update}, $self->extra_columns_callback->($self, $update);
526 13 ▶ if ($update->[ID]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$update[ID()]) { } 8 5 13
527 8 ▶ if ($update->[ID] =~ m/tmp/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$update[ID()] =~ /tmp/i) 0 8 8
528 0 croak "TMP IDs not allowed in TDList input"
529 }
530 }
531 else {
532 5 $self->sth('select_by_name')->execute($update->[LAST_NAME], $update->[FIRST_NAME]);
533 5 my $players = $self->sth('select_by_name')->fetchall_arrayref;
534 5 for my $player (@{$players}) {
535 2 ▶ if ($player->[ID] =~ m/tmp/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$player[ID()] =~ /tmp/i) 2 0 2
536 2 $update->[ID] = $player->[ID]; # already in DB (hope it's the same guy!)
537 }
538 }
539 5 ▶ if (not $update->[ID]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$update[ID()]) 3 2 5
540 3 $update->[ID] = $self->next_tmp_id(1);
541 }
542 }
543 13 ▶ if ($self->select_id($update->[ID])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->select_id($$update[ID()])) { } 7 6 13
544 # ID is already in database, do an update
545 $self->sth('update_id')->execute(
546 7 @{$update}, # new values for all columns
547 $update->[ID], # player ID (for WHERE condition)
548 );
549 }
550 else {
551 # ID is not in database, insert new record
552 6 $self->insert_player(@{$update});
553 }
554 }
555 }
556 catch {
557 0 ▶ push @errors, $_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
558 0 ▶ print $_ if ($verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
559 18 };
560 18 ▶ if (@errors >= $self->max_update_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors >= $self->max_update_errors) 0 18 18
561 0 push @errors, "Too many errors - aborting\n";
562 0 last;
563 }
564 }
565 4 $self->db->do('COMMIT'); # make sure we do this!
566 4 $self->update_time(time);
567 4 ▶ if (@errors > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors > 1) 0 4 4
568 0 unshift @errors, scalar @errors . " errors during update:\n";
569 }
570 4 ▶ croak(join "\n", @errors) if(@errors);
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 4 4
571 }
572  
573 # sql columns (without column types)
574 sub sql_columns {
575 1 ▶ my ($self, $joiner) = @_;
Subroutine sql_columns: called CC=2 SCAR=7.0
Pod documented
576  
577 1 ▶ $joiner = ', ' if (not defined $joiner);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $joiner 1 0 1
578 return join($joiner,
579 1 map({ keys %{$_} }
580 @columns,
581 $self->extra_columns,
582 ),
583 );
584 }
585  
586 # sql columns with column types
587 sub sql_column_types {
588 1 ▶ my ($self, $joiner) = @_;
Subroutine sql_column_types: called CC=2 SCAR=7.0
Pod documented
589  
590 1 ▶ $joiner = ', ' if (not defined $joiner);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $joiner 1 0 1
591  
592 return join($joiner,
593 1 map({join ' ', each %{$_}}
594 @columns,
595 $self->extra_columns,
596 ),
597 );
598 }
599  
600 # '?, ' place-holder question marks for each column,
601 # appropriate for an UPDATE or INSERT query
602 sub sql_update_qs {
603 1 ▶ my ($self, $joiner) = @_;
Subroutine sql_update_qs: called CC=2 SCAR=7.0
Pod documented
604  
605 1 ▶ $joiner = ', ' if (not defined $joiner);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $joiner 1 0 1
606  
607 return join($joiner,
608 1 map({ (keys(%{$_}))[0] . ' = ?' }
609 @columns,
610 $self->extra_columns,
611 ),
612 );
613 }
614  
615 # place-holder question marks for each column,
616 # appropriate for an INSERT query
617 sub sql_insert_qs {
618 1 ▶ my ($self, $joiner) = @_;
Subroutine sql_insert_qs: called CC=2 SCAR=7.0
Pod documented
619  
620 1 ▶ $joiner = ', ' if (not defined $joiner);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $joiner 1 0 1
621  
622 return join($joiner,
623 1 map({ '?' } # one question mark per column
624 @columns,
625 $self->extra_columns,
626 ),
627 );
628 }
629  
630 1 1;
631  
632 __END__
↑ Summary