/*
   This file is part of TALER
   Copyright (C) 2024 Taler Systems SA

   TALER 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.

   TALER 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
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file src/donaudb/commit.c
 * @brief Implementation of the commit function for Postgres
 * @author Christian Grothoff
 */
#include <donau_config.h>
#include "commit.h"
#include "helper.h"


/**
 * Commit the current transaction of a database connection.
 *
 * @param ctx the `struct DONAUDB_PostgresContext` with the plugin-specific state
 * @return final transaction status
 */
enum GNUNET_DB_QueryStatus
DONAUDB_commit (struct DONAUDB_PostgresContext *ctx)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_end
  };
  enum GNUNET_DB_QueryStatus qs;

  GNUNET_break (NULL != ctx->transaction_name);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Committing transaction `%s'\n",
              ctx->transaction_name);
  PREPARE (ctx,
           "commit",
           "COMMIT");

  {
    PGresult *result;

    result = GNUNET_PQ_exec_prepared (ctx->conn,
                                      "commit",
                                      params);
    qs = GNUNET_PQ_eval_result (ctx->conn,
                                "commit",
                                result);
    /* PostgreSQL accepts COMMIT on a transaction it has already aborted: it
       silently rolls the transaction back and answers PGRES_COMMAND_OK with the
       command tag ROLLBACK.  GNUNET_PQ_eval_result() cannot tell that apart from
       a real commit, so every caller took the success path for a transaction
       whose writes are gone.  Report it as a soft error instead: sessions run
       SERIALIZABLE, so the overwhelmingly likely cause is an unhandled
       40001/40P01 for which retrying is right; a caller with a retry loop gives
       up after MAX_RETRIES and fails visibly, one without fails immediately.

       The *success* path deliberately keeps returning exactly what
       GNUNET_PQ_eval_prepared_non_select() returned before.  That function is
       GNUNET_PQ_exec_prepared() + GNUNET_PQ_eval_result() plus a
       strtol(PQcmdTuples()) step that only runs when the status is already
       GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, and PQcmdTuples() on a COMMIT command
       tag is the empty string -- so that step yields 0, which is
       GNUNET_DB_STATUS_SUCCESS_NO_RESULTS again. */
    if ( (0 <= qs) &&
         (NULL != result) &&
         (0 == strcmp ("ROLLBACK",
                       PQcmdStatus (result))) )
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "Transaction `%s' was rolled back by the database instead of committed; a previous statement must have failed without being handled\n",
                  ctx->transaction_name);
      qs = GNUNET_DB_STATUS_SOFT_ERROR;
    }
    PQclear (result);
  }
  ctx->transaction_name = NULL;
  return qs;
}
