aboutsummaryrefslogtreecommitdiff
path: root/src/exchangedb/pg_select_aml_process.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-12-30 21:28:19 +0100
committerChristian Grothoff <christian@grothoff.org>2022-12-30 21:28:19 +0100
commitb1d9745545ff528729b53312947ba768c336a1d8 (patch)
tree114bc999f01ac638dc0110167babda305d2fd7cb /src/exchangedb/pg_select_aml_process.c
parent5754adc414eaca8dcc2de47b6fcc12e9d1979c12 (diff)
downloadexchange-b1d9745545ff528729b53312947ba768c336a1d8.tar.xz
-work on new DB queries
Diffstat (limited to 'src/exchangedb/pg_select_aml_process.c')
-rw-r--r--src/exchangedb/pg_select_aml_process.c132
1 files changed, 130 insertions, 2 deletions
diff --git a/src/exchangedb/pg_select_aml_process.c b/src/exchangedb/pg_select_aml_process.c
index 9b5dedb39..2105308fc 100644
--- a/src/exchangedb/pg_select_aml_process.c
+++ b/src/exchangedb/pg_select_aml_process.c
@@ -26,6 +26,87 @@
#include "pg_helper.h"
+/**
+ * Closure for #handle_aml_result.
+ */
+struct AmlProcessResultContext
+{
+ /**
+ * Function to call on each result.
+ */
+ TALER_EXCHANGEDB_AmlStatusCallback cb;
+
+ /**
+ * Closure for @e cb.
+ */
+ void *cb_cls;
+
+ /**
+ * Plugin context.
+ */
+ struct PostgresClosure *pg;
+
+ /**
+ * Set to #GNUNET_SYSERR on serious errors.
+ */
+ enum GNUNET_GenericReturnValue status;
+};
+
+
+/**
+ * Function to be called with the results of a SELECT statement
+ * that has returned @a num_results results. Helper function
+ * for #TEH_PG_select_aml_process().
+ *
+ * @param cls closure of type `struct AmlProcessResultContext *`
+ * @param result the postgres result
+ * @param num_results the number of results in @a result
+ */
+static void
+handle_aml_result (void *cls,
+ PGresult *result,
+ unsigned int num_results)
+{
+ struct AmlProcessResultContext *ctx = cls;
+ struct PostgresClosure *pg = ctx->pg;
+
+ for (unsigned int i = 0; i<num_results; i++)
+ {
+ struct TALER_PaytoHashP h_payto;
+ struct TALER_Amount threshold;
+ uint64_t rowid;
+ uint32_t sv;
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ GNUNET_PQ_result_spec_uint64 ("aml_status_serial_id",
+ &rowid),
+ GNUNET_PQ_result_spec_auto_from_type ("h_payto",
+ &h_payto),
+ TALER_PQ_RESULT_SPEC_AMOUNT ("threshold",
+ &threshold),
+ GNUNET_PQ_result_spec_uint32 ("status",
+ &sv),
+ GNUNET_PQ_result_spec_end
+ };
+
+ if (GNUNET_OK !=
+ GNUNET_PQ_extract_result (result,
+ rs,
+ i))
+ {
+ GNUNET_break (0);
+ ctx->status = GNUNET_SYSERR;
+ return;
+ }
+ ctx->cb (ctx->cb_cls,
+ rowid,
+ &h_payto,
+ &threshold,
+ (enum TALER_AmlDecisionState) sv);
+ GNUNET_PQ_cleanup_result (rs);
+ }
+}
+
+
enum GNUNET_DB_QueryStatus
TEH_PG_select_aml_process (
void *cls,
@@ -35,6 +116,53 @@ TEH_PG_select_aml_process (
TALER_EXCHANGEDB_AmlStatusCallback cb,
void *cb_cls)
{
- GNUNET_break (0); // FIXME: not implemeted!
- return GNUNET_DB_STATUS_HARD_ERROR;
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_uint32 (&decision),
+ GNUNET_PQ_query_param_uint64 (&row_off),
+ GNUNET_PQ_query_param_end
+ };
+ struct AmlProcessResultContext ctx = {
+ .cb = cb,
+ .cb_cls = cb_cls,
+ .pg = pg,
+ .status = GNUNET_OK
+ };
+ enum GNUNET_DB_QueryStatus qs;
+ const char *stmt = forward
+ ? "select_aml_process_inc"
+ : "select_aml_process_dec";
+
+ PREPARE (pg,
+ "select_aml_process_inc",
+ "SELECT"
+ " aml_status_serial_id"
+ ",h_payto"
+ ",threshold_var"
+ ",threshold_frac"
+ ",status"
+ " FROM aml_status"
+ " WHERE aml_status_serial_id > $2"
+ " AND $1 = status & $1"
+ " ORDER BY aml_status_serial_id INC");
+ PREPARE (pg,
+ "select_aml_process_dec",
+ "SELECT"
+ " aml_status_serial_id"
+ ",h_payto"
+ ",threshold_var"
+ ",threshold_frac"
+ ",status"
+ " FROM aml_status"
+ " WHERE aml_status_serial_id < $2"
+ " AND $1 = status & $1"
+ " ORDER BY aml_status_serial_id DESC");
+ qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
+ stmt,
+ params,
+ &handle_aml_result,
+ &ctx);
+ if (GNUNET_OK != ctx.status)
+ return GNUNET_DB_STATUS_HARD_ERROR;
+ return qs;
}