1 #include "fetch_top_chosen_songs_for_tournament.h"
3 // small utility function so we can stick TopChosenSongs in a std::set
4 bool TopChosen::operator< (const TopChosen &other) const
6 if (title != other.title)
7 return (title < other.title);
8 return (frequency < other.frequency);
11 FetchTopChosenSongsForTournament::FetchTopChosenSongsForTournament(unsigned tournament, unsigned num, std::vector<TopChosen> *chosen)
12 : tournament(tournament), num(num), chosen(chosen) {}
14 void FetchTopChosenSongsForTournament::operator() (pqxx::transaction<> &t)
16 chosen->erase(chosen->begin(), chosen->end());
18 // Again, this will break if a song has more than one short title
19 pqxx::result res( t.exec(
20 "SELECT title, shorttitle, COUNT(*) AS frequency FROM scores NATURAL JOIN songs NATURAL LEFT JOIN songshorttitles WHERE tournament="
21 + pqxx::to_string(tournament) + " AND chosen='t' AND song is NOT NULL GROUP BY title,shorttitle ORDER BY COUNT(*) DESC LIMIT "
22 + pqxx::to_string(num)) );
24 for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
27 ts.title = i["title"].as(ts.title);
28 ts.shorttitle = i["shorttitle"].as(ts.shorttitle);
29 ts.frequency = i["frequency"].as(ts.frequency);
31 chosen->push_back(ts);