exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

pg_select_purse_merges_above_serial_id.c (6032B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022 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_select_purse_merges_above_serial_id.c
     18  * @brief Implementation of the select_purse_merges_above_serial_id function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include "taler/taler_error_codes.h"
     23 #include "taler/taler_dbevents.h"
     24 #include "taler/taler_pq_lib.h"
     25 #include "pg_select_purse_merges_above_serial_id.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #purse_deposit_serial_helper_cb().
     31  */
     32 struct PurseMergeSerialContext
     33 {
     34 
     35   /**
     36    * Callback to call.
     37    */
     38   TALER_EXCHANGEDB_PurseMergeCallback cb;
     39 
     40   /**
     41    * Closure for @e cb.
     42    */
     43   void *cb_cls;
     44 
     45   /**
     46    * Plugin context.
     47    */
     48   struct PostgresClosure *pg;
     49 
     50   /**
     51    * Status code, set to #GNUNET_SYSERR on hard errors.
     52    */
     53   enum GNUNET_GenericReturnValue status;
     54 };
     55 
     56 
     57 /**
     58  * Helper function to be called with the results of a SELECT statement
     59  * that has returned @a num_results results.
     60  *
     61  * @param cls closure of type `struct PurseMergeSerialContext`
     62  * @param result the postgres result
     63  * @param num_results the number of results in @a result
     64  */
     65 static void
     66 purse_merges_serial_helper_cb (void *cls,
     67                                PGresult *result,
     68                                unsigned int num_results)
     69 {
     70   struct PurseMergeSerialContext *dsc = cls;
     71   struct PostgresClosure *pg = dsc->pg;
     72 
     73   for (unsigned int i = 0; i<num_results; i++)
     74   {
     75     uint64_t rowid;
     76     char *partner_base_url = NULL;
     77     struct TALER_Amount amount;
     78     struct TALER_Amount balance;
     79     uint32_t flags32;
     80     enum TALER_WalletAccountMergeFlags flags;
     81     struct TALER_PurseMergePublicKeyP merge_pub;
     82     struct TALER_ReservePublicKeyP reserve_pub;
     83     struct TALER_PurseMergeSignatureP merge_sig;
     84     struct TALER_PurseContractPublicKeyP purse_pub;
     85     struct GNUNET_TIME_Timestamp merge_timestamp;
     86     struct GNUNET_PQ_ResultSpec rs[] = {
     87       TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
     88                                    &amount),
     89       TALER_PQ_RESULT_SPEC_AMOUNT ("balance",
     90                                    &balance),
     91       GNUNET_PQ_result_spec_allow_null (
     92         GNUNET_PQ_result_spec_string ("partner_base_url",
     93                                       &partner_base_url),
     94         NULL),
     95       GNUNET_PQ_result_spec_uint32 ("flags",
     96                                     &flags32),
     97       GNUNET_PQ_result_spec_timestamp ("merge_timestamp",
     98                                        &merge_timestamp),
     99       GNUNET_PQ_result_spec_auto_from_type ("purse_pub",
    100                                             &purse_pub),
    101       GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
    102                                             &reserve_pub),
    103       GNUNET_PQ_result_spec_auto_from_type ("merge_sig",
    104                                             &merge_sig),
    105       GNUNET_PQ_result_spec_auto_from_type ("merge_pub",
    106                                             &merge_pub),
    107       GNUNET_PQ_result_spec_uint64 ("purse_merge_request_serial_id",
    108                                     &rowid),
    109       GNUNET_PQ_result_spec_end
    110     };
    111     enum GNUNET_GenericReturnValue ret;
    112 
    113     if (GNUNET_OK !=
    114         GNUNET_PQ_extract_result (result,
    115                                   rs,
    116                                   i))
    117     {
    118       GNUNET_break (0);
    119       dsc->status = GNUNET_SYSERR;
    120       return;
    121     }
    122     flags = (enum TALER_WalletAccountMergeFlags) flags32;
    123     ret = dsc->cb (dsc->cb_cls,
    124                    rowid,
    125                    partner_base_url,
    126                    &amount,
    127                    &balance,
    128                    flags,
    129                    &merge_pub,
    130                    &reserve_pub,
    131                    &merge_sig,
    132                    &purse_pub,
    133                    merge_timestamp);
    134     GNUNET_PQ_cleanup_result (rs);
    135     if (GNUNET_OK != ret)
    136       break;
    137   }
    138 }
    139 
    140 
    141 enum GNUNET_DB_QueryStatus
    142 TEH_PG_select_purse_merges_above_serial_id (
    143   void *cls,
    144   uint64_t serial_id,
    145   TALER_EXCHANGEDB_PurseMergeCallback cb,
    146   void *cb_cls)
    147 {
    148   struct PostgresClosure *pg = cls;
    149   struct GNUNET_PQ_QueryParam params[] = {
    150     GNUNET_PQ_query_param_uint64 (&serial_id),
    151     GNUNET_PQ_query_param_end
    152   };
    153   struct PurseMergeSerialContext dsc = {
    154     .cb = cb,
    155     .cb_cls = cb_cls,
    156     .pg = pg,
    157     .status = GNUNET_OK
    158   };
    159   enum GNUNET_DB_QueryStatus qs;
    160 
    161   PREPARE (pg,
    162            "audit_get_purse_merge_incr",
    163            "SELECT"
    164            " pm.purse_merge_request_serial_id"
    165            ",partner_base_url"
    166            ",pr.amount_with_fee"
    167            ",pr.balance"
    168            ",pr.flags"
    169            ",pr.merge_pub"
    170            ",pm.reserve_pub"
    171            ",pm.merge_sig"
    172            ",pm.purse_pub"
    173            ",pm.merge_timestamp"
    174            " FROM purse_merges pm"
    175            " JOIN purse_requests pr USING (purse_pub)"
    176            " LEFT JOIN partners USING (partner_serial_id)"
    177            " WHERE ("
    178            "  (purse_merge_request_serial_id>=$1)"
    179            " )"
    180            " ORDER BY purse_merge_request_serial_id ASC;");
    181 
    182   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    183                                              "audit_get_purse_merge_incr",
    184                                              params,
    185                                              &purse_merges_serial_helper_cb,
    186                                              &dsc);
    187   if (GNUNET_OK != dsc.status)
    188     return GNUNET_DB_STATUS_HARD_ERROR;
    189   return qs;
    190 }