+#include "fetch_needs_update.h"
+
+FetchNeedsUpdate::FetchNeedsUpdate(struct timeval last_updated, unsigned tournament, unsigned round, unsigned parallel, bool *result)
+ : last_updated(last_updated), tournament(tournament), round(round), parallel(parallel), result(result)
+{
+}
+
+void FetchNeedsUpdate::operator() (pqxx::transaction<> &t)
+{
+ // as we don't seem to have proper handling for timestamps in libpqxx,
+ // let's ask the database do to the work for us
+ char buf[256];
+ time_t lu = last_updated.tv_sec;
+ strftime(buf, 256, "%F %T", localtime(&lu));
+
+ pqxx::result res( t.exec(std::string("SELECT (last_updated > '") + buf + "') AS needs_update FROM bigscreen.active_groups WHERE "
+ "tournament=" + pqxx::to_string(tournament) + " AND "
+ "round=" + pqxx::to_string(round) + " AND "
+ "parallel=" + pqxx::to_string(parallel)) );
+
+ try {
+ *result = res.at(0)["needs_update"].as(*result);
+ } catch (PGSTD::out_of_range &e) {
+ *result = false;
+ }
+}