-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.pl
141 lines (110 loc) · 3.98 KB
/
import.pl
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
#!/usr/bin/perl
use strict;
use DBD::mysql;
use XML::XPath;
use setup;
our $dbh;
my ($file, $race_id, $class, $classInFile, $eventRaceId) = @ARGV;
print "** Importing $race_id / $class from $file\n";
my $xp = XML::XPath->new(filename => $file);
my $ns = $xp->find("/ResultList/ClassResult[EventClass/ClassShortName='$classInFile']/PersonResult");
my $st = $dbh->prepare("delete from result where race_id=? and class=?");
$st->execute($race_id, $class);
my $sti = $dbh->prepare("insert into result (id, race_id, person_id, time, status, class, time_sec) values (0, ?,?,?,?,?,?)");
my $stf = $dbh->prepare("select id from person where eventor_id=?");
my $stip = $dbh->prepare("insert into person (id, family_name, given_name, club, club_id, eventor_id, created) values (0,?,?,?,?,?,null)");
my %aookClub = (26=>26,32=>32,33=>33,57=>57,58=>58,62=>62,71=>71,78=>78,80=>80,83=>83,89=>89,91=>91,107=>107,108=>108,110=>110,114=>114,125=>125,127=>127,128=>128,144=>144,150=>150,163=>163,172=>172,176=>176,181=>181,182=>182,188=>188,203=>203,205=>205,209=>209,227=>227,229=>229,232=>232,234=>234,236=>236,240=>240,245=>245,258=>258,268=>268,271=>271,272=>272,273=>273,281=>281,290=>290,308=>308,310=>310,313=>313,323=>323,342=>342,343=>343,362=>362,363=>363,373=>373,388=>388,400=>400,401=>401,402=>402,533=>533);
foreach my $node ($ns->get_nodelist) {
my $given = $node->findvalue("Person/PersonName/Given");
my $famname = $node->findvalue("Person/PersonName/Family");
my $id = $node->findvalue("Person/PersonId");
my $club = $node->findvalue("Organisation/Name");
my $clubId = $node->findvalue("Organisation/OrganisationId");
my $time; my @status; my $status;
next unless $aookClub{$clubId};
if (defined $eventRaceId) {
$time = $node->findvalue("RaceResult[EventRace/EventRaceId=$eventRaceId]/Result/Time");
@status = $node->findnodes("RaceResult[EventRace/EventRaceId=$eventRaceId]/Result/CompetitorStatus")->get_nodelist;
if (!@status) {
warn "*** Problems with runner, no status in the race: $given $famname - $club ($id)\n";
next;
}
$status = $status[0]->getAttribute("value");
} else {
$time = $node->findvalue("Result/Time");
@status = $node->findnodes("Result/CompetitorStatus")->get_nodelist;
if (!@status) {
warn "*** Problems with runner, no status: $given $famname ($id)\n";
next;
}
$status = $status[0]->getAttribute("value");
}
if (!$id) {
$id = int(100000 + rand(1000000));
warn "*** No person Id! $given $famname , $club => using $id " ;
}
my $myId;
$stf->execute($id);
my $h = $stf->fetchrow_hashref;
if ($h->{id}) {
$myId = $h->{id};
} else {
$stip->execute($famname, $given, $club,$clubId, $id);
$myId = $dbh->{'mysql_insertid'};
}
print "$famname, $given (E$id - $myId)\t$club ($clubId)\t \t$time \t$status\n";
$sti->execute($race_id, $myId, $time, $status, $class, hms2sec($time));
}
sub hms2sec {
my ($t)=@_;
if ($t=~ /^(\d+):(\d+)$/) {
return $2+$1*60;
}
if ($t=~ /(\d+):(\d+):(\d+)$/) {
return $3+$2*60+$1*60*60;
}
return 0;
}
#
#PersonResult
# Person/PersonName/Family
# Person/PersonName/Given
# Person/PersonId
# Result/Time
# Result/CompetitorStatus
__END__
create table person (
id int auto_increment primary key,
family_name varchar(255),
given_name varchar(255),
club varchar(255),
club_id int,
created timestamp
);
create table race(
id int auto_increment primary key,
name varchar(255),
race_date date,
season int(4)
);
create table result (
id int auto_increment primary key,
race_id int,
person_id int,
time varchar(255),
status varchar(20),
points float,
foreign key (race_id) references race,
foreign key (person_id) references person
);
create table series (
id int auto_increment primary key,
name varchar(255)
);
create table series_race (
id int auto_increment primary key,
series_id int,
race_id int,
foreign key (series_id) references series,
foreign key (race_id) references race
);