Fetch all the "max score for song" entries in one go instead of one at a time.
[ccbs] / bigscreen / fetch_max_score_for_player.cpp
1 #include "fetch_max_score_for_player.h"
2
3 FetchMaxScoreForPlayer::FetchMaxScoreForPlayer(unsigned tournament, unsigned player, unsigned round, unsigned *score)
4         : tournament(tournament), player(player), round(round), score(score) {}
5         
6 void FetchMaxScoreForPlayer::operator() (pqxx::transaction<> &t)
7 {
8         pqxx::result res( t.exec("SELECT MAX(feetrating)*1000 AS max_score FROM songratings WHERE " 
9                 "machine=( SELECT machine FROM tournaments WHERE tournament=" + pqxx::to_string(tournament) + ") AND "
10                 "song NOT IN ( SELECT song FROM scores WHERE tournament=" + pqxx::to_string(tournament) + " AND song IS NOT NULL AND ( "
11                 "parallel=0 OR ( player=" + pqxx::to_string(player) + " AND ( chosen='t' OR round=" + pqxx::to_string(round) + " ) ) ) )") );
12         
13         *score = res.at(0)["max_score"].as(*score);
14 }