summaryrefslogtreecommitdiff
path: root/src/backenddb
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2020-06-20 18:22:33 +0200
committerChristian Grothoff <christian@grothoff.org>2020-06-20 18:22:33 +0200
commitda5f393967e9a4e6b7c78b72f1c9f74a49bb52c6 (patch)
tree95e94f74d7c9661865a5dfe11c73fdc16147a188 /src/backenddb
parent9bae03573f31f22893839bbbbdaeba94821e3a57 (diff)
downloadmerchant-da5f393967e9a4e6b7c78b72f1c9f74a49bb52c6.tar.gz
merchant-da5f393967e9a4e6b7c78b72f1c9f74a49bb52c6.tar.bz2
merchant-da5f393967e9a4e6b7c78b72f1c9f74a49bb52c6.zip
start with reserve processing logic
Diffstat (limited to 'src/backenddb')
-rw-r--r--src/backenddb/plugin_merchantdb_postgres.c134
1 files changed, 133 insertions, 1 deletions
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c
index f218e438..12dc3498 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -4239,7 +4239,7 @@ postgres_activate_reserve (void *cls,
/**
- * Closure for #lookup_accounts_cb.
+ * Closure for #lookup_reserves_cb.
*/
struct LookupReservesContext
{
@@ -4420,6 +4420,123 @@ postgres_lookup_reserves (void *cls,
/**
+ * Closure for #lookup_pending_reserves_cb.
+ */
+struct LookupPendingReservesContext
+{
+ /**
+ * Postgres context.
+ */
+ struct PostgresClosure *pg;
+
+ /**
+ * Function to call with the results
+ */
+ TALER_MERCHANTDB_PendingReservesCallback cb;
+
+ /**
+ * Closure for @e cb
+ */
+ void *cb_cls;
+
+ /**
+ * Set in case of errors.
+ */
+ enum GNUNET_DB_QueryStatus qs;
+
+};
+
+
+/**
+ * Function to be called with the results of a SELECT statement
+ * that has returned @a num_results results about accounts.
+ *
+ * @param[in,out] cls of type `struct LookupReservesContext *`
+ * @param result the postgres result
+ * @param num_result the number of results in @a result
+ */
+static void
+lookup_pending_reserves_cb (void *cls,
+ PGresult *result,
+ unsigned int num_results)
+{
+ struct LookupPendingReservesContext *lrc = cls;
+ struct PostgresClosure *pg = lrc->pg;
+
+ for (unsigned int i = 0; i < num_results; i++)
+ {
+ struct TALER_ReservePublicKeyP reserve_pub;
+ struct TALER_Amount merchant_initial_balance;
+ char *exchange_url;
+ char *instance_id;
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
+ &reserve_pub),
+ GNUNET_PQ_result_spec_string ("exchange_url",
+ &exchange_url),
+ GNUNET_PQ_result_spec_string ("instance_id",
+ &instance_id),
+ TALER_PQ_RESULT_SPEC_AMOUNT ("merchant_initial_balance",
+ &merchant_initial_balance),
+ GNUNET_PQ_result_spec_end
+ };
+
+ if (GNUNET_OK !=
+ GNUNET_PQ_extract_result (result,
+ rs,
+ i))
+ {
+ GNUNET_break (0);
+ lrc->qs = GNUNET_DB_STATUS_HARD_ERROR;
+ return;
+ }
+ lrc->cb (lrc->cb_cls,
+ instance_id,
+ exchange_url,
+ &reserve_pub,
+ &merchant_initial_balance);
+ GNUNET_PQ_cleanup_result (rs);
+ }
+}
+
+
+/**
+ * Lookup reserves pending activation across all instances.
+ *
+ * @param cls closure
+ * @param cb function to call with reserve summary data
+ * @param cb_cls closure for @a cb
+ * @return transaction status
+ */
+static enum GNUNET_DB_QueryStatus
+postgres_lookup_pending_reserves (void *cls,
+ TALER_MERCHANTDB_PendingReservesCallback cb,
+ void *cb_cls)
+{
+ struct PostgresClosure *pg = cls;
+ struct LookupPendingReservesContext lrc = {
+ .pg = pg,
+ .cb = cb,
+ .cb_cls = cb_cls
+ };
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_end
+ };
+ enum GNUNET_DB_QueryStatus qs;
+
+ check_connection (pg);
+ qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
+ "lookup_pending_reserves",
+ params,
+ &lookup_pending_reserves_cb,
+ &lrc);
+ if (lrc.qs < 0)
+ return lrc.qs;
+ return qs;
+}
+
+
+/**
* Closure for #lookup_reserve_tips_cb().
*/
struct LookupTipsContext
@@ -7572,6 +7689,20 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
" FROM merchant_instances"
" WHERE merchant_id=$1)",
2),
+ /* For postgres_lookup_pending_reserves() */
+ GNUNET_PQ_make_prepare ("lookup_pending_reserves",
+ "SELECT"
+ " reserve_pub"
+ ",merchant_id"
+ ",exchange_url"
+ ",merchant_initial_balance_val"
+ ",merchant_initial_balance_frac"
+ " FROM merchant_tip_reserves"
+ " JOIN merchant_instances USING (merchant_serial)"
+ " JOIN merchant_tip_reserve_keys USING (reserve_serial)"
+ " WHERE exchange_initial_balance_val=0"
+ " AND exchange_initial_balance_frac=0",
+ 0),
/* For postgres_lookup_reserve() */
GNUNET_PQ_make_prepare ("lookup_reserve",
"SELECT"
@@ -8011,6 +8142,7 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
plugin->insert_reserve = &postgres_insert_reserve;
plugin->activate_reserve = &postgres_activate_reserve;
plugin->lookup_reserves = &postgres_lookup_reserves;
+ plugin->lookup_pending_reserves = &postgres_lookup_pending_reserves;
plugin->lookup_reserve = &postgres_lookup_reserve;
plugin->delete_reserve = &postgres_delete_reserve;
plugin->purge_reserve = &postgres_purge_reserve;