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

filter: Statementstmt 75.3% 251 / 333 Branchbran 52.8% 75 / 142 Conditioncond 31.2% 5 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 82.6% 43 / 52 Podpod 75.0% 24 / 32 totaltotal 68.0% 398 / 585 CC 79 SCAR 22.5
CC
79
Cov
67%
update_from_AGA
72.0
run
20.0
update_from_file
13.7
CRAP 9.5
↑ Summary
1 #===============================================================================
2 # FILE: Games::Go::AGA::TDListDB
3 # ABSTRACT: an SQL object for holding AGA TDList data
4 # AUTHOR: Reid Augustin (REID), <reid@hellosix.com>
5 # CREATED: 12/02/2010 08:51:22 AM PST
6 #===============================================================================
7  
8 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package Games::Go::AGA::TDListDB;
13  
14 1 ▶use open qw( :utf8 :std ); # UTF8 for all files and STDIO
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use IO::Handle; # for autoflush
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Games::Go::AGA::Parse::TDList;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Games::Go::AGA::Parse::Util qw( is_Rating );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 our $VERSION = '0.048'; # VERSION
27  
28 1 Readonly my $BUF_MAX => 4096;
29  
30 # list the names of the default database field index subroutines, in order
31 1 my @_column_names = (qw(
32 last_name
33 first_name
34 id
35 membership
36 rank
37 date
38 club
39 state
40 extra
41 ));
42 1 my %_column_idx = (
43 last_name => 0,
44 first_name => 1,
45 id => 2,
46 membership => 3,
47 rank => 4,
48 date => 5,
49 club => 6,
50 state => 7,
51 extra => 8,
52 );
53  
54 # list of default column names
55 52 ▶sub column_idx {
Subroutine column_idx: called CC=3 SCAR=14.2
Pod documented
56 52 my ($self, $which) = @_;
57  
58 52 ▶ if (@_ >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) 52 0 52
59 52 return $_column_idx{lc $which};
60 }
61 return wantarray
62 ? @_column_names
63 0 ▶ : \@_column_names;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
64 }
65  
66 1 Readonly my @columns => (
67 {last_name => 'VARCHAR(256)', },
68 {first_name => 'VARCHAR(256)', },
69 {id => 'VARCHAR(256) NOT NULL PRIMARY KEY', },
70 {membership => 'VARCHAR(256)', },
71 {rank => 'VARCHAR(256)', },
72 {date => 'VARCHAR(256)', },
73 {club => 'VARCHAR(256)', },
74 {state => 'VARCHAR(256)', },
75 {extra => 'VARCHAR(256)', },
76 );
77  
78 my %pre_defaults = (
79 url => 'https://www.usgo.org/ratings/TDListN.txt',
80 dbdname => 'tdlistdb.sqlite',
81 table_name => 'tdlist',
82 extra_columns => [],
83 0 ▶ extra_columns_callback => sub { return () },
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 1 max_update_errors => 10,
85 raw_filename => 'TDList.txt',
86 verbose => 0,
87 );
88  
89 1 ▶__PACKAGE__->run( @ARGV ) if not caller(); # modulino
Branch: 1 branch
BranchTrueFalseTotal
unless caller() 0 1 1
90  
91 1 ▶sub new {
Subroutine new: called CC=6 SCAR=19.1
Pod documented
92 1 my ($class, %args) = @_;
93  
94 1 my $self = {};
95 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
96  
97 1 ▶ while (my ($key, $value) = each %pre_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %pre_defaults) 8 1 9
98 8 ▶ $value = delete $args{$key} if (exists $args{$key});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} 4 4 8
99 8 $self->$key($value);
100 }
101  
102 1 my $db = $self->db(delete $args{db});
103  
104 1 for my $key (keys %args) { # any leftovers?
105 0 $self->$key($args{$key});
106 }
107  
108 # SQL for finding players by name
109 1 $self->sth('select_by_name',
110 $db->prepare(
111 join('',
112 'SELECT * FROM ',
113 $self->table_name,
114 ' WHERE last_name = ?',
115 ' AND first_name = ?',
116 ),
117 ),
118 );
119  
120 # and a statement for inserting new players
121 1 $self->sth('insert_player',
122 $db->prepare(
123 join('',
124 'INSERT INTO ',
125 $self->table_name,
126 ' ( ',
127 $self->sql_columns,
128 ' ) ',
129 'VALUES ( ',
130 $self->sql_insert_qs,
131 ' )',
132 ),
133 ),
134 );
135  
136 # SQL for updating when player is already in DB
137 1 $self->sth('update_id',
138 $db->prepare(
139 join('',
140 'UPDATE ',
141 $self->table_name,
142 ' SET ',
143 $self->sql_update_qs,
144 ' WHERE id = ?',
145 ),
146 ),
147 );
148  
149 # SQL for finding IDs
150 1 $self->sth('select_id',
151 $db->prepare(
152 join('',
153 'SELECT * FROM ',
154 $self->table_name,
155 ' WHERE id = ?',
156 ),
157 ),
158 );
159  
160 # SQL for getting and setting DB update time
161 1 $self->sth('select_time',
162 $db->prepare(
163 join('',
164 'SELECT update_time FROM ',
165 $self->table_name_meta,
166 ' WHERE key = 1',
167 ),
168 ),
169 );
170 1 $self->sth('update_time',
171 $db->prepare(
172 join('',
173 'UPDATE ',
174 $self->table_name_meta,
175 ' SET update_time = ?',
176 ' WHERE key = 1',
177 ),
178 ),
179 );
180  
181 # SQL to get/set next_tmp marker
182 1 $self->sth('select_next_tmp',
183 $db->prepare(
184 join('',
185 'SELECT next_tmp_id FROM ',
186 $self->table_name_meta,
187 ' WHERE key = 1',
188 ),
189 ),
190 );
191 1 $self->sth('update_next_tmp',
192 $db->prepare(
193 join('',
194 'UPDATE ',
195 $self->table_name_meta,
196 ' SET next_tmp_id = ?',
197 ' WHERE key = 1',
198 ),
199 )
200 );
201  
202 1 $self->init(\%args); # in case any subclass needs initialization
203  
204 map {
205 0 ▶ if (not $self->can($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($_)) 0 0 0
206 0 my $ref = ref $self;
207 0 confess("$ref can't '->$_'\n");
208 }
209 0 $self->$_($args{$_});
210 } keys %args;
211  
212 1 return $self;
213 }
214  
215 1 my $usage = qq(
216  
217 TDListDB [ -tdlist_file filename ] [ -sqlite_file filename ]
218 [ -url url | AGA ] [ -verbose ] [ -help ]
219  
220 Options may be abbreviated to their first letter.
221  
222 By default, TDListDB.pm updates from a file in the current
223 directory named TDList.txt. Specify -tdlist_file to update
224 from a different file, or specify -url to update from a
225 website. -url AGA updates from the usual AGA website at
226 https://www.usgo.org/ratings/TDListN.txt
227  
228 );
229  
230 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod undocumented
231 0 my ($class) = @_;
232  
233 0 require Getopt::Long;
234 0 Getopt::Long->import(qw( :config pass_through ));
235  
236 0 my $verbose;
237 my $url;
238 exit 0 if (not GetOptions(
239 'tdlist_file=s', => \$pre_defaults{raw_filename}, # update from file
240 'sqlite_file=s', => \$pre_defaults{dbdname}, # sqlite file
241 'url=s', => \$url, # URL to update from
242 'verbose', => \$verbose,
243 0 ▶ 'help' => sub { print $usage; exit 0; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
244 0 ▶ ));
Branch: 1 branch
BranchTrueFalseTotal
unless GetOptions("tdlist_file=s", \$pre_defaults{"raw_filename"}, "sqlite_file=s", \$pre_defaults{"dbdname"}, "url=s", \$url, "verbose", \$verbose, "help", sub { print $usage; exit 0; } ) 0 0 0
245  
246 0 my $tdlist = $class->new( verbose => $verbose );
247 0 my $filename = $tdlist->raw_filename;
248 0 my $dbfile = $tdlist->dbdname;
249 0 STDOUT->autoflush(1);
250  
251 0 ▶ if ($url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url) 0 0 0
252 0 ▶ if (uc $url ne 'AGA') {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $url ne "AGA") 0 0 0
253 0 $tdlist->url($url);
254 }
255 0 $url = $tdlist->url;
256 0 print "Updating $dbfile from AGA ($url)\n";
257 0 $tdlist->update_from_AGA();
258 0 exit;
259 }
260 0 print "Updating $dbfile from file ($filename)\n";
261 0 $tdlist->update_from_file($filename);
262 }
263  
264 # stub for subclass to override
265 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
266 1 my ($self) = @_;
267 }
268  
269 5 ▶sub verbose {
Subroutine verbose: called CC=2 SCAR=6.9
Pod undocumented
270 5 my ($self, $new) = @_;
271  
272 5 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 4 5
273 1 $self->{verbose} = $new;
274 }
275  
276 5 return $self->{verbose};
277 }
278  
279 1 ▶sub raw_filename {
Subroutine raw_filename: called CC=2 SCAR=7.0
Pod documented
280 1 my ($self, $new) = @_;
281  
282 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 0 1
283 1 $self->{raw_filename} = $new;
284 }
285  
286 1 return $self->{raw_filename};
287 }
288  
289 2 ▶sub dbdname {
Subroutine dbdname: called CC=2 SCAR=6.9
Pod documented
290 2 my ($self, $new) = @_;
291  
292 2 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 1 2
293 1 $self->{dbdname} = $new;
294 }
295  
296 2 return $self->{dbdname};
297 }
298  
299 12 ▶sub table_name {
Subroutine table_name: called CC=2 SCAR=6.9
Pod documented
300 12 my ($self, $new) = @_;
301  
302 12 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 11 12
303 1 $self->{table_name} = $new;
304 }
305  
306 12 return quotemeta $self->{table_name};
307 }
308  
309 6 ▶sub table_name_meta {
Subroutine table_name_meta: called CC=1 SCAR=0.0
Pod undocumented
310 6 my ($self) = @_;
311  
312 6 return $self->table_name . '_meta';
313 }
314  
315 1 ▶sub url {
Subroutine url: called CC=2 SCAR=7.0
Pod documented
316 1 my ($self, $new) = @_;
317  
318 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 0 1
319 1 $self->{url} = $new;
320 }
321  
322 1 return $self->{url};
323 }
324  
325 2 ▶sub background {
Subroutine background: called CC=2 SCAR=6.9
Pod documented
326 2 my ($self, $new) = @_;
327  
328 2 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 1 2
329 1 $self->{background} = $new;
330 }
331  
332 2 return $self->{background};
333 }
334  
335 14 ▶sub max_update_errors {
Subroutine max_update_errors: called CC=2 SCAR=6.9
Pod documented
336 14 my ($self, $new) = @_;
337  
338 14 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 13 14
339 1 $self->{max_update_errors} = $new;
340 }
341  
342 14 return $self->{max_update_errors};
343 }
344  
345 14 ▶sub extra_columns_callback {
Subroutine extra_columns_callback: called CC=3 SCAR=11.6
Pod documented
346 14 my ($self, @new) = @_;
347  
348 14 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 13 14
349 1 ▶ if (ref $new[0] ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new[0] ne "CODE") 0 1 1
350 0 $self->my_print("Must set a code-ref in extra_columns_callback\n");
351 0 die;
352 }
353 1 $self->{extra_columns_callback} = $new[0];
354 }
355 14 return $self->{extra_columns_callback};
356 }
357  
358 5 ▶sub extra_columns {
Subroutine extra_columns: called CC=4 SCAR=14.0
Pod documented
359 5 my ($self, @new) = @_;
360  
361 5 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 4 5
362 1 ▶ if (ref $new[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new[0] eq 'ARRAY') { } 1 0 1
363 1 $self->{extra_columns} = $new[0];
364 }
365 else {
366 0 $self->{extra_columns} = \@new;
367 }
368 }
369 5 ▶ return wantarray ? @{$self->{extra_columns}} : $self->{extra_columns};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 1 5
370 }
371  
372 13 ▶sub db {
Subroutine db: called CC=4 SCAR=14.5
Pod documented
373 13 my ($self, $new) = @_;
374  
375 13 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 12 13
376 1 ▶ if (not $new) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new) 1 0 1
377 1 ▶ if (my $fname = $self->dbdname) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fname = $self->dbdname) { } 1 0 1
378 1 $new = DBI->connect( # connect to your database, create if needed
379 "dbi:SQLite:dbname=$fname", # DSN: dbi, driver, database file
380 "", # no user
381 "", # no password
382 {
383 AutoCommit => 1,
384 RaiseError => 1, # complain if something goes wrong
385 },
386 )
387 }
388 else {
389 0 $self->my_print("No dbdname for SQLite file\n");
390 0 die;
391 }
392 }
393 1 $self->{db} = $new;
394 1 $self->_db_schema(); # make sure table exists
395 }
396  
397 13 return $self->{db};
398 }
399  
400 # library of statement handles
401 104 ▶sub sth {
Subroutine sth: called CC=4 SCAR=15.5
Pod documented
402 104 my ($self, $name, $new) = @_;
403  
404 104 ▶ if (not $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 104 104
405 0 $self->my_print("Statement handle name is required\n");
406 0 die;
407 }
408 104 ▶ if (@_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 8 96 104
409 8 $self->{sth}{$name} = $new;
410 }
411  
412 104 my $sth = $self->{sth}{$name};
413 104 ▶ if (not $sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 104 104
414 0 $self->my_print("No statement handle called '$name'\n");
415 0 die;
416 }
417  
418 104 return $sth;
419 }
420  
421 1 ▶sub _db_schema {
Subroutine _db_schema: called CC=1 SCAR=0.0
422 1 my ($self) = @_;
423  
424 1 $self->db->do(
425 join('',
426 'CREATE TABLE IF NOT EXISTS ',
427 $self->table_name,
428 ' (',
429 $self->sql_column_types,
430 ' )',
431 ),
432 );
433  
434 1 $self->db->do(join '',
435 'CREATE TABLE IF NOT EXISTS ',
436 $self->table_name_meta,
437 ' (',
438 'key INTEGER PRIMARY KEY, ',
439 'update_time VARCHAR(12), ',
440 'next_tmp_id VARCHAR(12)',
441 ' )',
442 );
443  
444 1 $self->db->do(join '',
445 'INSERT OR IGNORE INTO ',
446 $self->table_name_meta,
447 ' (',
448 'key, ',
449 'update_time, ',
450 'next_tmp_id',
451 ' ) VALUES ( 1, 0, 1 )',
452 );
453 }
454  
455 6 ▶sub update_time {
Subroutine update_time: called CC=3 SCAR=11.0
Pod documented
456 6 my ($self, $new) = @_;
457  
458 6 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 4 2 6
459 4 $self->sth('update_time')->execute($new);
460 }
461 6 $self->sth('select_time')->execute();
462 6 my $time = $self->sth('select_time')->fetchall_arrayref();
463 6 $time = $time->[0][0];
464 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
465 }
466  
467 19 ▶sub select_id {
Subroutine select_id: called CC=3 SCAR=11.8
Pod documented
468 19 my ($self, $id) = @_;
469  
470 19 $self->sth('select_id')->execute($id);
471 # ID is primary index, so can only be one - fetch into first array
472 # element:
473 19 my ($player) = $self->sth('select_id')->fetchall_arrayref;
474 19 ▶ $player->[$self->column_idx('rank')] += 0 if (is_Rating($player->[$self->column_idx('rank')])); # numify ratings
Branch: 1 branch
BranchTrueFalseTotal
if is_Rating($$player[$self->column_idx("rank")]) 0 19 19
475 return wantarray
476 0 ▶ ? @{$player->[0]}
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 19 19
477 : $player->[0];
478 }
479  
480 6 ▶sub insert_player {
Subroutine insert_player: called CC=3 SCAR=11.4
Pod undocumented
481 6 my ($self, @new) = @_;
482  
483 6 ▶ $new[$self->column_idx('id')] = $self->next_tmp_id(1) if (not $new[$self->column_idx('id')]);
Branch: 1 branch
BranchTrueFalseTotal
unless $new[$self->column_idx("id")] 0 6 6
484 6 $self->sth('insert_player')->execute(@new);
485 return wantarray
486 ? @new
487 6 ▶ : \@new;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
488 }
489  
490 5 ▶sub next_tmp_id {
Subroutine next_tmp_id: called CC=4 SCAR=14.5
Pod documented
491 5 my ($self, $used) = @_;
492  
493 5 $self->sth('select_next_tmp')->execute;
494 5 my $next_tmp = $self->sth('select_next_tmp')->fetchall_arrayref;
495 5 $next_tmp = $next_tmp->[0][0];
496 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
497 5 ▶ while ($self->select_id("TMP$next_tmp")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->select_id("TMP$next_tmp")) 0 5 5
498 0 $next_tmp++
499 }
500  
501 5 ▶ if ($used) { # is the caller planning on allocating this one?
Branch: 1 branch
BranchTrueFalseTotal
if ($used) 5 0 5
502 5 $self->sth('update_next_tmp')->execute($next_tmp + 1);
503 }
504 5 return "TMP$next_tmp";
505 }
506  
507 # reap any child zombies from earlier update_from_AGA calls
508 0 ▶sub reap {
Subroutine reap: not called CC=3 SCAR=24.8
Pod documented
509 0 my $kid;
510 0 my $reaped = 0;
511 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$kid > 0 and do { do { $kid = waitpid(-1, 1); ++$reaped if $kid > 0 } } 0 0 0
512 0 $kid = waitpid(-1, WNOHANG);
513 0 ▶ $reaped++ if ($kid > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $kid > 0 0 0 0
514 } while $kid > 0;
515 0 return $reaped;
516 }
517  
518 0 ▶sub update_from_AGA {
Subroutine update_from_AGA: not called CC=8 SCAR=42.8
Pod documented
519 0 my ($self) = @_;
520  
521 0 my $pid;
522 0 ▶ if ($self->background) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->background) 0 0 0
523 0 $pid = fork;
524 0 ▶ die "fork failed: $!\n" if not defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
525 }
526 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
527 # parent process
528 0 return;
529 }
530  
531 0 ▶ if (not $self->{ua}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ua"}) 0 0 0
532 0 $self->{ua} = LWP::UserAgent->new;
533 }
534  
535 0 my $fname = $self->raw_filename;
536 0 my $url = $self->url;
537 0 ▶ $self->my_print("Starting $fname fetch from $url at ", scalar localtime, "\n") if ($self->verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
538 0 $self->{ua}->mirror($url, $fname);
539 0 ▶ $self->my_print("... fetch done at ", scalar localtime, "\n") if ($self->verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
540 0 $self->update_from_file($fname);
541  
542 0 ▶ exit if (defined $pid); # exit if this is a spawned child ($pid == 0)
Branch: 1 branch
BranchTrueFalseTotal
if defined $pid 0 0 0
543 }
544  
545 4 ▶sub update_from_file {
Subroutine update_from_file: called CC=10 SCAR=26.2
Pod documented
546 4 my ($self, $fh) = @_;
547  
548 4 ▶ if (not ref $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 0 4 4
549 0 my $fname = $fh;
550 0 $fh = undef;
551 0 ▶ if (not open($fh, '<', $fname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $fname) 0 0 0
552 0 $self->my_print("Error opening $fname for reading: $!\n");
553 0 die;
554 }
555 }
556 4 $self->fh($fh);
557  
558 4 my $parser = Games::Go::AGA::Parse::TDList->new();
559 4 my $verbose = $self->verbose;
560 4 ▶ $self->my_print("Starting database update at ", scalar localtime, "\n") if ($verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 4 4
561 4 $self->db->do('BEGIN');
562 4 my $error_count = 0;
563 4 my $ii = 0;
564 4 my $ID = $self->column_idx('id');
565 4 while (1) {
566 22 $ii++;
567 22 my $line = $self->next_line;
568 22 ▶ last if (not defined $line);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 4 18 22
569 18 ▶ next if (not $line);
Branch: 1 branch
BranchTrueFalseTotal
unless $line 5 13 18
570  
571 13 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 13 13
572 0 ▶ $self->my_print('.') if ($ii % 1000 == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $ii % 1000 == 0 0 0 0
573 0 ▶ $self->my_print("\n") if ($ii % 40000 == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $ii % 40000 == 0 0 0 0
574 }
575 13 ▶ try { # in case a line crashes, print error but continue
Subroutine __ANON__: called CC=9 SCAR=22.6
576 #$self->my_print("parse $line") if ($verbose);
577 13 $parser->parse($line);
578 13 my $update = $parser->as_array;
579 13 ▶ if ($update->[$self->column_idx('last_name')] or $update->[$self->column_idx('first_name')]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$update[$self->column_idx('last_name')] or $$update[$self->column_idx('first_name')]) { } 13 0 13
Condition33%1/3
$$update[$self->column_idx("last_name")] or $$update[$self->column_idx("first_name")]
l!l&&r!l&&!r
1300
MC/DC0%0/2
$$update[$self->column_idx("last_name")] or $$update[$self->column_idx("first_name")]
$$update[$self->column_idx("last_name")] $$update[$self->column_idx("first_name")]
Truth table33%1/3
A or B
A= $$update[$self->column_idx("last_name")]B= $$update[$self->column_idx("first_name")]
ABresult
1X1
011
000
580 13 push @{$update}, $self->extra_columns_callback->($self, $update);
581 13 ▶ if ($update->[$ID]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update->[$ID]) { } 8 5 13
582 8 ▶ if ($update->[$ID] =~ m/tmp/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update->[$ID] =~ /tmp/i) 0 8 8
583 0 die "TMP IDs not allowed in TDList input";
584 }
585 }
586 else {
587 5 $self->sth('select_by_name')->execute($update->[$self->column_idx('last_name')], $update->[$self->column_idx('first_name')]);
588 5 my $players = $self->sth('select_by_name')->fetchall_arrayref;
589 5 for my $player (@{$players}) {
590 2 ▶ if ($player->[$ID] =~ m/tmp/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($player->[$ID] =~ /tmp/i) 2 0 2
591 2 $update->[$ID] = $player->[$ID]; # already in DB (hope it's the same guy!)
592 }
593 }
594 5 ▶ if (not $update->[$ID]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($update->[$ID]) 3 2 5
595 3 $update->[$ID] = $self->next_tmp_id(1);
596 }
597 }
598 13 ▶ if ($self->select_id($update->[$ID])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->select_id($update->[$ID])) { } 7 6 13
599 # ID is already in database, do an update
600 $self->sth('update_id')->execute(
601 7 @{$update}, # new values for all columns
602 $update->[$ID], # player ID (for WHERE condition)
603 );
604 }
605 else {
606 # ID is not in database, insert new record
607 6 $self->insert_player(@{$update});
608 }
609 }
610 else {
611 0 die "no name parsed from $line";
612 }
613 }
614 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
615 0 $error_count++;
616 0 my $error = $_;
617 0 $self->my_print("Error at line $ii: $error");
618 13 };
619 13 ▶ if ($error_count >= $self->max_update_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_count >= $self->max_update_errors) 0 13 13
620 0 $self->my_print("$error_count errors - aborting\n");
621 0 last;
622 }
623 }
624 4 $self->db->do('COMMIT'); # make sure we do this!
625 4 $self->update_time(time);
626 }
627  
628 # file might not have lines. enforce lines here
629 22 ▶sub next_line {
Subroutine next_line: called CC=6 SCAR=18.0
Pod undocumented
630 22 my ($self) = @_;
631  
632 22 my $offset = $self->{buf_offset};
633 22 ▶ if ($self->{buf_end} - $offset <= 160) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"buf_end"} - $offset <= 160) 13 9 22
634 13 $self->get_fh_chunk;
635 13 $offset = $self->{buf_offset};
636 }
637 22 ▶ return if ($offset >= $self->{buf_end});
Branch: 1 branch
BranchTrueFalseTotal
if $offset >= $self->{"buf_end"} 4 18 22
638 18 my $eol_idx;
639 18 ▶ if ($self->{has_lines}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'has_lines'}) { } 16 2 18
640 16 $eol_idx = index($self->{buf}, "\n", $offset);
641 16 ▶ if ($eol_idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eol_idx < 0) 0 16 16
642 0 die "no EOL"; # shouldn't happen
643 }
644 }
645 else {
646 # assume 80 characters per line
647 2 $eol_idx = $offset + 80;
648 # but not past the end of the buffer
649 2 ▶ $eol_idx = $self->{buf_end} if ($eol_idx > $self->{buf_end});
Branch: 1 branch
BranchTrueFalseTotal
if $eol_idx > $self->{"buf_end"} 2 0 2
650 2 $eol_idx--;
651 }
652 18 my $len = $eol_idx - $offset;
653 18 my $line = substr $self->{buf}, $offset, $len;
654 18 $self->{buf_offset} += $len + 1;
655 18 return $line;
656 }
657  
658 21 ▶sub fh {
Subroutine fh: called CC=4 SCAR=14.1
Pod undocumented
659 21 my ($self, $new) = @_;
660  
661 21 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 4 17 21
662 4 $self->{fh} = $new;
663 4 delete $self->{has_lines};
664 4 ▶ if ($new and ref $new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new and ref $new) 4 0 4
Condition33%1/3
$new and ref $new
!ll&&!rl&&r
004
MC/DC0%0/2
$new and ref $new
$new ref $new
Truth table33%1/3
A and B
A= $newB= ref $new
ABresult
0X0
100
111
665 4 $self->{buf} = '';
666 4 $self->{buf_offset} = $self->{buf_end} = 0;
667 4 $self->get_fh_chunk;
668 4 $self->{has_lines} = (index($self->{buf}, "\n") >= 0);
669 }
670 }
671 21 return $self->{fh};
672 }
673  
674 17 ▶sub get_fh_chunk {
Subroutine get_fh_chunk: called CC=2 SCAR=7.1
Pod undocumented
675 17 my ($self) = @_;
676  
677 # how much is still left?
678 17 my $left = $self->{buf_end} - $self->{buf_offset};
679 # shift unused part of buf down to the beginning
680 17 substr($self->{buf}, 0, $self->{buf_offset}, '');
681 # read in a new chunk
682 17 my $new = read $self->fh, $self->{buf}, $BUF_MAX - $left, $left;
683 17 ▶ if (not defined $new) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new) 0 17 17
684 0 die "Read error: $!";
685 }
686 17 $self->{buf_end} = $left + $new;
687 17 $self->{buf_offset} = 0;
688 }
689  
690 # sql columns (without column types)
691 1 ▶sub sql_columns {
Subroutine sql_columns: called CC=2 SCAR=7.0
Pod documented
692 1 my ($self, $joiner) = @_;
693  
694 1 ▶ $joiner = ', ' if (not defined $joiner);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $joiner 1 0 1
695 return join($joiner,
696 1 map({ keys %{$_} }
697 @columns,
698 $self->extra_columns,
699 ),
700 );
701 }
702  
703 # sql columns with column types
704 1 ▶sub sql_column_types {
Subroutine sql_column_types: called CC=2 SCAR=7.0
Pod documented
705 1 my ($self, $joiner) = @_;
706  
707 1 ▶ $joiner = ', ' if (not defined $joiner);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $joiner 1 0 1
708  
709 return join($joiner,
710 1 map({join ' ', each %{$_}}
711 @columns,
712 $self->extra_columns,
713 ),
714 );
715 }
716  
717 # '?, ' place-holder question marks for each column,
718 # appropriate for an UPDATE or INSERT query
719 1 ▶sub sql_update_qs {
Subroutine sql_update_qs: called CC=2 SCAR=7.0
Pod documented
720 1 my ($self, $joiner) = @_;
721  
722 1 ▶ $joiner = ', ' if (not defined $joiner);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $joiner 1 0 1
723  
724 return join($joiner,
725 1 map({ (keys(%{$_}))[0] . ' = ?' }
726 @columns,
727 $self->extra_columns,
728 ),
729 );
730 }
731  
732 # place-holder question marks for each column,
733 # appropriate for an INSERT query
734 1 ▶sub sql_insert_qs {
Subroutine sql_insert_qs: called CC=2 SCAR=7.0
Pod documented
735 1 my ($self, $joiner) = @_;
736  
737 1 ▶ $joiner = ', ' if (not defined $joiner);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $joiner 1 0 1
738  
739 return join($joiner,
740 1 map({ '?' } # one question mark per column
741 @columns,
742 $self->extra_columns,
743 ),
744 );
745 }
746  
747 0 ▶sub my_print {
Subroutine my_print: not called CC=1 SCAR=6.9
Pod documented
748 0 my $self = shift;
749  
750 0 $self->print_cb->(@_);
751 }
752  
753 0 ▶sub print_cb {
Subroutine print_cb: not called CC=3 SCAR=24.8
Pod documented
754 0 my ($self, $new) = @_;
755  
756 0 ▶ $self->{print_cb} = $new if (@_ > 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
757 0 ▶ return $self->{print_cb} || sub { print @_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$self->{'print_cb'} || sub { print @_; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'print_cb'} || sub { print @_; }
$self->{'print_cb'} sub { print @_; }
Truth table0%0/3
A || B
A= $self->{'print_cb'}B= sub { print @_; }
ABresult
1X1
011
000
758 }
759  
760 1 1;
761  
762 __END__
↑ Summary