LCOV - code coverage report
Current view: top level - backenddb - pg_helper.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 59.6 % 52 31
Test Date: 2025-12-02 20:17:27 Functions: 75.0 % 4 3

            Line data    Source code
       1              : /*
       2              :    This file is part of TALER
       3              :    Copyright (C) 2023 Taler Systems SA
       4              : 
       5              :    TALER is free software; you can redistribute it and/or modify it under the
       6              :    terms of the GNU General Public License as published by the Free Software
       7              :    Foundation; either version 3, or (at your option) any later version.
       8              : 
       9              :    TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10              :    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11              :    A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12              : 
      13              :    You should have received a copy of the GNU General Public License along with
      14              :    TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15              :  */
      16              : /**
      17              :  * @file pg_helper.c
      18              :  * @brief shared internal definitions for postgres DB plugin
      19              :  * @author Christian Grothoff
      20              :  */
      21              : #include "platform.h"
      22              : #include "pg_helper.h"
      23              : #include <gnunet/gnunet_util_lib.h>
      24              : #include <gnunet/gnunet_pq_lib.h>
      25              : #include <taler/taler_util.h>
      26              : #include <taler/taler_pq_lib.h>
      27              : #include <taler/taler_json_lib.h>
      28              : #include <taler/taler_mhd_lib.h>
      29              : 
      30              : 
      31              : enum GNUNET_GenericReturnValue
      32          364 : TMH_PG_start (void *cls,
      33              :               const char *name)
      34              : {
      35          364 :   struct PostgresClosure *pg = cls;
      36          364 :   struct GNUNET_PQ_ExecuteStatement es[] = {
      37          364 :     GNUNET_PQ_make_execute ("START TRANSACTION ISOLATION LEVEL SERIALIZABLE"),
      38              :     GNUNET_PQ_EXECUTE_STATEMENT_END
      39              :   };
      40              : 
      41          364 :   GNUNET_assert (NULL != name);
      42          364 :   check_connection (pg);
      43          364 :   postgres_preflight (pg);
      44          364 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      45              :               "Starting merchant DB transaction `%s'\n",
      46              :               name);
      47          364 :   if (GNUNET_OK !=
      48          364 :       GNUNET_PQ_exec_statements (pg->conn,
      49              :                                  es))
      50              :   {
      51            0 :     TALER_LOG_ERROR ("Failed to start transaction\n");
      52            0 :     GNUNET_break (0);
      53            0 :     return GNUNET_SYSERR;
      54              :   }
      55          364 :   pg->transaction_name = name;
      56          364 :   return GNUNET_OK;
      57              : }
      58              : 
      59              : 
      60              : enum GNUNET_GenericReturnValue
      61            0 : TMH_PG_start_read_committed (void *cls,
      62              :                              const char *name)
      63              : {
      64            0 :   struct PostgresClosure *pg = cls;
      65            0 :   struct GNUNET_PQ_ExecuteStatement es[] = {
      66            0 :     GNUNET_PQ_make_execute ("START TRANSACTION ISOLATION LEVEL READ COMMITTED"),
      67              :     GNUNET_PQ_EXECUTE_STATEMENT_END
      68              :   };
      69              : 
      70            0 :   GNUNET_assert (NULL != name);
      71            0 :   check_connection (pg);
      72            0 :   postgres_preflight (pg);
      73            0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      74              :               "Starting merchant DB transaction %s (READ COMMITTED)\n",
      75              :               name);
      76            0 :   if (GNUNET_OK !=
      77            0 :       GNUNET_PQ_exec_statements (pg->conn,
      78              :                                  es))
      79              :   {
      80            0 :     TALER_LOG_ERROR ("Failed to start transaction\n");
      81            0 :     GNUNET_break (0);
      82            0 :     return GNUNET_SYSERR;
      83              :   }
      84            0 :   pg->transaction_name = name;
      85            0 :   return GNUNET_OK;
      86              : }
      87              : 
      88              : 
      89              : void
      90          111 : TMH_PG_rollback (void *cls)
      91              : {
      92          111 :   struct PostgresClosure *pg = cls;
      93          111 :   struct GNUNET_PQ_ExecuteStatement es[] = {
      94          111 :     GNUNET_PQ_make_execute ("ROLLBACK"),
      95              :     GNUNET_PQ_EXECUTE_STATEMENT_END
      96              :   };
      97              : 
      98          111 :   if (NULL == pg->transaction_name)
      99           30 :     return;
     100           81 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     101              :               "Rolling back merchant DB transaction `%s'\n",
     102              :               pg->transaction_name);
     103           81 :   GNUNET_break (GNUNET_OK ==
     104              :                 GNUNET_PQ_exec_statements (pg->conn,
     105              :                                            es));
     106           81 :   pg->transaction_name = NULL;
     107              : }
     108              : 
     109              : 
     110              : enum GNUNET_DB_QueryStatus
     111          283 : TMH_PG_commit (void *cls)
     112              : {
     113          283 :   struct PostgresClosure *pg = cls;
     114          283 :   struct GNUNET_PQ_QueryParam params[] = {
     115              :     GNUNET_PQ_query_param_end
     116              :   };
     117              :   enum GNUNET_DB_QueryStatus qs;
     118              : 
     119          283 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     120              :               "Committing merchant DB transaction %s\n",
     121              :               pg->transaction_name);
     122          283 :   check_connection (pg);
     123          283 :   PREPARE (pg,
     124              :            "merchant_commit",
     125              :            "COMMIT");
     126          283 :   qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
     127              :                                            "merchant_commit",
     128              :                                            params);
     129          283 :   if (qs < 0)
     130              :   {
     131            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     132              :                 "Failed to commit transaction\n");
     133            0 :     TMH_PG_rollback (pg);
     134            0 :     return qs;
     135              :   }
     136          283 :   pg->transaction_name = NULL;
     137          283 :   return qs;
     138              : }
        

Generated by: LCOV version 2.0-1