/*
  This file is part of Anastasis
  Copyright (C) 2020-2022 Anastasis SARL

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

  Anastasis 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file stasis/get_last_auth_iban_in_row.c
 * @brief Anastasis database: get last auth iban in row
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/get_last_auth_iban_in_row.h"
#include "anastasis/anastasis-database/transaction.h"
#include "anastasis/anastasis-database/preflight.h"
#include <taler/taler_pq_lib.h>


/**
 * Function to check the last known IBAN payment.
 *
 * @param credit_account which credit account to check
 * @param[out] last_row set to the last known row
 * @return transaction status,
 *    #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a cb
 *      returned 'true' once
 *    #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if no
 *      wire transfers existed for which @a cb returned true
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_get_last_auth_iban_in_row (
  const char *credit_account,
  uint64_t *last_row)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (credit_account),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_uint64 ("wire_reference",
                                  last_row),
    GNUNET_PQ_result_spec_end
  };

  PREPARE ("get_last_auth_iban_in_row",
           "SELECT "
           " wire_reference"
           " FROM anastasis_auth_iban_in"
           " WHERE credit_account_details=$1"
           " ORDER BY wire_reference DESC"
           " LIMIT 1;");
  return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                   "get_last_auth_iban_in_row",
                                                   params,
                                                   rs);
}


/* end of get_last_auth_iban_in_row.c */
