/*
   This file is part of Challenger
   Copyright (C) 2023 Taler Systems SA

   Challenger is free software; you can redistribute it and/or modify it under the
   terms of the GNU General Public License as published by the Free Software
   Foundation; either version 3, or (at your option) any later version.

   Challenger is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along with
   Challenger; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file src/challengerdb/gc.c
 * @brief Implementation of the gc function for Postgres
 * @author Christian Grothoff
 */
#include "platform.h"
#include <taler/taler_pq_lib.h>
#include "challenger-database/gc.h"
#include "challenger-database/preflight.h"
#include "pg_helper.h"


enum GNUNET_DB_QueryStatus
CHALLENGERDB_gc (struct CHALLENGERDB_PostgresContext *pg,
                 struct GNUNET_TIME_Absolute expire)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_absolute_time (&expire),
    GNUNET_PQ_query_param_end
  };
  enum GNUNET_DB_QueryStatus qs;

  CHALLENGERDB_preflight (pg);
  PREPARE (pg,
           "gc_validations",
           "DELETE FROM validations"
           " WHERE expiration_time < $1;");
  PREPARE (pg,
           "gc_tokens",
           "DELETE FROM tokens"
           " WHERE token_expiration_time < $1;");
  qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                           "gc_validations",
                                           params);
  if (qs < 0)
    return qs;
  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "gc_tokens",
                                             params);
}
