forked from soccermetrics/marcotti-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreloadTables.pl
More file actions
executable file
·533 lines (429 loc) · 10.1 KB
/
PreloadTables.pl
File metadata and controls
executable file
·533 lines (429 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/perl
# Title: PreloadTables.pl
# Synopsis: Load FMRD tables that we prefer to pre-load
# Format: ./PreloadTables.pl
# Date: 2010-08-01
# Version: 1.0
# Author: Howard Hamilton, Soccermetrics Research & Consulting, LLC
use DBI;
use DBI qw(:sql_types);
use DBD::Pg;
use Term::ReadKey;
# declare subroutines
sub trim($);
sub rtrim($);
($dbname) = @ARGV;
$attempt = 0;
$success = 0;
# connect to database
print "Preload tables...";
do {
print("\nEnter username: ");
chomp(my $user = <STDIN>);
print("Enter password: ");
ReadMode('noecho');
chomp(my $password = <STDIN>);
ReadMode(0);
print "\n";
if (!($dbh = DBI->connect("dbi:Pg:dbname=$dbname",$user,$password,{AutoCommit=>0}))) {
print "Login failed.\n";
$attempt++;
}
else {
$success = 1;
}
} until ($attempt == 3) || $success;
print("\nDatabase authentication confirmed.\n");
print("Enter maximum number of league or shootout rounds: ");
chomp(my $roundnum = <STDIN>);
print("[A]lpha or [N]umeric group names: ");
chomp(my $groupchar = <STDIN>);
# Populate database tables
load_confederations();
load_countries();
load_timezones();
load_rounds($roundnum);
load_group_rounds();
load_knockout_rounds();
load_matchdays();
load_phases();
load_groups($groupchar);
load_surfaces();
load_penoutcomes();
load_fieldpos();
load_flankpos();
load_positions();
load_wxconditions();
load_goalstrikes();
load_goalevents();
load_cards();
load_fouls();
# close database
$dbh->disconnect();
# ---------------------------------------------------------
# Subroutines
# ---------------------------------------------------------
# load confederations subroutine
sub load_confederations {
# open list file
open(LIST,"lists/confed-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_confederations(confed_name) VALUES (?)");
# execute
while (<LIST>) {
chomp;
my ($cstr) = split;
$sth->execute($cstr);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load country table
sub load_countries {
# open list file -- 2 columns: country and confederation (comma-delimited)
open(LIST,"lists/country-list.dat");
# prepare query and insertion statements
$sth = $dbh->prepare("INSERT INTO tbl_countries(confed_id,cty_name) VALUES (?,?)");
$qth = $dbh->prepare("SELECT confed_id FROM tbl_confederations WHERE confed_name=?");
# execute
while (<LIST>) {
chomp;
my (@cstr) = split(/,\s+/);
$qth->execute($cstr[1]) || die "Could not execute query: " . $qth->$errstr;
my @data;
while (@data = $qth->fetchrow_array) {
my $confedid = $data[0];
$sth->execute($confedid,$cstr[0]);
}
$qth->finish();
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load time zones table
sub load_timezones {
# open list file -- 3 columns: confederation, country, and offset
open(LIST,"lists/timezone-list.dat");
# prepare query and insertion statements
$sth = $dbh->prepare("INSERT INTO tbl_timezones(confed_id,tz_name,tz_offset) VALUES (?,?,?)");
$qth = $dbh->prepare("SELECT confed_id FROM tbl_confederations WHERE confed_name=?");
# execute
while (<LIST>) {
chomp;
my (@cstr) = split(/,\s+/);
$qth->execute(trim($cstr[0])) || die "Could not execute query: " . $qth->$errstr;
while (my @data = $qth->fetchrow_array) {
my $confedid = $data[0];
$sth->execute($confedid,trim($cstr[1]),trim($cstr[2]));
}
$qth->finish();
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load competition phases table
sub load_phases {
# open list file
open(LIST,"lists/phase-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_phases(phase_desc) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load groups table
sub load_groups {
$groupkey = $_[0]; # get group identifier
if (uc($groupkey) eq 'A') {
open(LIST,"lists/group-letters-list.dat");
}
elsif (uc($groupkey) eq 'N') {
open(LIST,"lists/group-numbers-list.dat");
}
else {
print "ERROR: Identifier must be 'A' or 'N'. Groups table not populated.\n";
return;
}
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_groups(group_desc) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
}
# load rounds table
sub load_rounds {
$maxrounds = $_[0]; # get maximum number of rounds
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_rounds(round_desc) VALUES (?)");
for ($i=1; $i<=$maxrounds; $i++) {
$cstr = "Round " . $i;
$sth->execute($cstr);
}
# commit
$dbh->commit();
}
# load group rounds table
sub load_group_rounds {
# open list file
open(LIST,"lists/group-round-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_grouprounds(grpround_desc) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load knockout rounds table
sub load_knockout_rounds {
# open list file
open(LIST,"lists/knockout-round-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_knockoutrounds(koround_desc) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load knockout round matchday table
sub load_matchdays {
# open list file
open(LIST,"lists/matchday-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_matchdays(matchday_desc) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load venue playing surfaces table
sub load_surfaces {
# open list file
open(LIST,"lists/fieldsurface-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_venuesurfaces(vensurf_desc) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load penalty outcomes table
sub load_penoutcomes {
# open list file
open(LIST,"lists/penalties-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_penoutcomes(po_desc) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load field position table
sub load_fieldpos {
# open list file
open(LIST,"lists/fieldname-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_fieldnames(posfield_name) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load flank name table
sub load_flankpos {
# open list file
open(LIST,"lists/flankname-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_flanknames(posflank_name) VALUES (?)");
# execute
while (<LIST>) {
chomp;
my (@cstr) = $_;
if ($cstr[0] eq "undef") {
$cstr[0] = undef;
}
$sth->execute($cstr[0]);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load positions table
sub load_positions {
# open list file -- 2 columns (space-delimited)
open(LIST, "lists/position-list.dat");
# prepare query and insertion statements
$sth = $dbh->prepare("INSERT INTO tbl_positions(posfield_id,posflank_id) VALUES (?,?)");
$qth[0] = $dbh->prepare("SELECT posflank_id FROM tbl_flanknames WHERE posflank_name=?");
$qth[1] = $dbh->prepare("SELECT posfield_id FROM tbl_fieldnames WHERE posfield_name=?");
$null = $dbh->prepare("SELECT posflank_id FROM tbl_flanknames WHERE posflank_name IS NULL");
$null->execute();
@nullval = $null->fetchrow_array;
# execute
while(<LIST>) {
chomp;
my (@cstr) = split;
$qth[0]->execute($cstr[0]) || die "Could not execute query: " . $qth[0]->$errstr;
$qth[1]->execute($cstr[1]) || die "Could not execute query: " . $qth[1]->$errstr;
my @data1, @data2;
while (@data2 = $qth[1]->fetchrow_array) {
my $flankid, $fieldid;
if ($cstr[0] eq "undef") {
$flankid = $nullval[0];
} else {
@data1 = $qth[0]->fetchrow_array;
$flankid = $data1[0];
}
$fieldid = $data2[0];
$sth->execute($fieldid,$flankid);
}
$qth[0]->finish();
$qth[1]->finish();
}
$null->finish();
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load weather conditions table
sub load_wxconditions {
# open list file
open(LIST,"lists/wxcond-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_weather(wx_conditiondesc) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load goal strikes table
sub load_goalstrikes {
# open list file
open(LIST,"lists/goalstrike-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_goalstrikes(gts_desc) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load goal events table
sub load_goalevents {
# open list file
open(LIST,"lists/goaltype-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_goalevents(gte_desc) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load cards table
sub load_cards {
# open list file
open(LIST,"lists/card-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_cards(card_type) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# load fouls table
sub load_fouls {
# open list file
open(LIST,"lists/fouls-list.dat");
# prepare
$sth = $dbh->prepare("INSERT INTO tbl_fouls(foul_desc) VALUES (?)");
# execute
while (<LIST>) {
chomp;
$sth->execute($_);
}
# commit
$dbh->commit();
# close list file
close(LIST);
}
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Right trim function to remove trailing whitespace
sub rtrim($)
{
my $string = shift;
$string =~ s/\s+$//;
return $string;
}