$dbh->{AutoCommit} = 0;
$dbh->{RaiseError} = 1;
+$dbh->do('SET client_min_messages TO WARNING');
+
# Find last completely done season
my $ref = $dbh->selectrow_hashref('SELECT sesong FROM fotballserier GROUP BY sesong HAVING COUNT(*)=COUNT(avgjort=1 OR NULL) ORDER BY sesong DESC LIMIT 1');
my $last_season = $ref->{'sesong'};
}
close DATA;
-$dbh->do('DELETE FROM ratings');
-my $iq = $dbh->prepare('INSERT INTO ratings ( id, rating, rating_stddev ) VALUES (?, ?, ?)');
+my @ratings = ();
+my @covariances = ();
open RATINGS, "$config::base_dir/bayeswf < $tmpnam |"
or die "bayeswf: $!";
while (<RATINGS>) {
- /(.*) (.*) (.*)/ or next;
- $iq->execute($3, $1, $2);
+ chomp;
+ my @x = split;
+ if ($x[0] eq 'covariance') {
+ push @covariances, (join("\t", @x[1..3]));
+ } else {
+ push @ratings, ($x[2] . "\t" . $x[0] . "\t" . $x[1]);
+ }
}
+$dbh->do('TRUNCATE ratings');
+$dbh->do('COPY ratings ( id, rating, rating_stddev ) FROM STDIN');
+$dbh->pg_putcopydata(join("\n", @ratings));
+$dbh->pg_putcopyend();
+
+$dbh->do('CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )');
+$dbh->do('COPY new_covariance ( player1, player2, cov ) FROM STDIN');
+$dbh->pg_putcopydata(join("\n", @covariances));
+$dbh->pg_putcopyend();
+$dbh->do('ALTER TABLE new_covariance ADD PRIMARY KEY ( player1, player2 );');
+$dbh->do('DROP TABLE IF EXISTS covariance');
+$dbh->do('ALTER TABLE new_covariance RENAME TO covariance');
+
$dbh->commit;
unlink($tmpnam);