5 require "../html/ccbs.pm";
13 # Not really a parser, but lets us export entire tournaments from one CCBS
14 # database to another in a simple way without having to worry about the
19 die "Missing tournament number (give on command line)." if (!defined($tno));
23 my $dbh = ccbs::db_connect();
25 # The tournament itself
26 my $tournament = $dbh->selectrow_hashref('SELECT * FROM tournaments NATURAL JOIN countries NATURAL JOIN seasons NATURAL JOIN machines NATURAL JOIN scoringsystems WHERE tournament=?', undef, $tno);
28 print "INSERT INTO tournaments \n";
29 print " (season, tournamentname, country, location, \"date\", machine, scoringsystem) VALUES (\n";
30 print " (SELECT season FROM seasons WHERE seasonname='$tournament->{'seasonname'}' ),\n";
31 print " '$tournament->{'tournamentname'}',\n";
32 print " (SELECT country FROM countries WHERE countryname='$tournament->{'countryname'}'),\n";
33 print " '$tournament->{'location'}',\n";
34 print " '$tournament->{'date'}',\n";
35 print " (SELECT machine FROM machines WHERE machinename='$tournament->{'machinename'}'),\n";
36 print " (SELECT scoringsystem FROM scoringsystems WHERE scoringsystemname='$tournament->{'scoringsystemname'}')\n";
40 my $tsql = "(SELECT tournament FROM tournaments WHERE tournamentname='$tournament->{'tournamentname'}' AND season=( SELECT season FROM seasons WHERE seasonname='$tournament->{'seasonname'}' ))";
42 # Rankings, and players (FIXME: do clubs as well)
43 my $rankings = ccbs::db_fetch_all($dbh, 'SELECT * FROM tournamentrankings NATURAL JOIN players NATURAL JOIN countries WHERE tournament=?', $tno);
44 for my $rank (@$rankings) {
45 printf "INSERT INTO players SELECT nextval('players_player_seq') AS player, '%s' AS nick, ( SELECT country FROM countries WHERE countryname='%s' ) AS country WHERE '%s' NOT IN ( SELECT nick FROM players );\n", $rank->{'nick'}, $rank->{'countryname'}, $rank->{'nick'};
47 my $p = $rank->{'points'};
48 $p = 'NULL' if (!defined($p));
50 print "INSERT INTO tournamentrankings (tournament, ranking, player, points) VALUES (\n";
52 print " $rank->{'ranking'},\n";
53 print " (SELECT player FROM players WHERE nick='$rank->{'nick'}'),\n";
57 # Skip tournamentparticipation, it is not needed except during the tournament.
60 my $rounds = ccbs::db_fetch_all($dbh, 'SELECT * FROM rounds WHERE tournament=?', $tno);
61 for my $round (@$rounds) {
62 print "INSERT INTO rounds ( tournament, round, randomsongs, chosensongs, numqualifying ) VALUES (\n";
64 print " $round->{'round'},\n";
65 print " $round->{'randomsongs'},\n";
66 print " $round->{'chosensongs'},\n";
67 print " $round->{'numqualifying'});\n";
71 my $groups = ccbs::db_fetch_all($dbh, 'SELECT * FROM groups WHERE tournament=?', $tno);
72 for my $group (@$groups) {
73 print "INSERT INTO groups ( tournament, round, parallel ) VALUES (\n";
75 print " $group->{'round'},\n";
76 print " $group->{'parallel'});\n";
80 my $rrs = ccbs::db_fetch_all($dbh, 'SELECT * FROM roundrandomsongs NATURAL JOIN songs WHERE tournament=?', $tno);
82 (my $title = $rr->{'title'}) =~ s/'/\\'/g;
84 print "INSERT INTO roundrandomsongs ( tournament, round, parallel, song ) VALUES (\n";
86 print " $rr->{'round'},\n";
87 print " $rr->{'parallel'},\n";
88 print " ( SELECT song FROM songs WHERE lower(title)=lower('$title') ));\n";
92 my $rps = ccbs::db_fetch_all($dbh, 'SELECT * FROM roundparticipation NATURAL JOIN players WHERE tournament=?', $tno);
94 print "INSERT INTO roundparticipation ( tournament, round, parallel, player, position ) VALUES (\n";
96 print " $rp->{'round'},\n";
97 print " $rp->{'parallel'},\n";
98 print " ( SELECT player FROM players WHERE nick='$rp->{'nick'}' ),\n";
99 print " $rp->{'position'});\n";
102 # And finally, scores
103 my $scores = ccbs::db_fetch_all($dbh, 'SELECT * FROM scores NATURAL JOIN songs NATURAL JOIN players WHERE tournament=?', $tno);
104 for my $score (@$scores) {
105 my $chosen = $score->{'chosen'} ? 't' : 'f';
106 (my $title = $score->{'title'}) =~ s/'/\\'/g;
108 print "INSERT INTO scores ( tournament, round, parallel, player, songnumber, song, playmode, difficulty, chosen, score ) VALUES (\n";
110 print " $score->{'round'},\n";
111 print " $score->{'parallel'},\n";
112 print " ( SELECT player FROM players WHERE nick='$score->{'nick'}' ),\n";
113 print " $score->{'songnumber'},\n";
114 print " ( SELECT song FROM songs WHERE lower(title)=lower('$title') ),\n";
115 print " '$score->{'playmode'}',\n";
116 print " '$score->{'difficulty'}',\n";
117 print " '$chosen',\n";
118 print " $score->{'score'});\n";