diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-11-17 17:52:22 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-11-17 17:52:22 +0100 |
commit | 5feedd7154afcd66d4d16923bdfe4ecbfd5cb700 (patch) | |
tree | d273af6d9eac57439022d7c05ba0cae07e8bc598 /src | |
parent | d97f57a3b5743f854b4e4af43f011a159a7e5a45 (diff) | |
download | sync-5feedd7154afcd66d4d16923bdfe4ecbfd5cb700.tar.gz sync-5feedd7154afcd66d4d16923bdfe4ecbfd5cb700.tar.bz2 sync-5feedd7154afcd66d4d16923bdfe4ecbfd5cb700.zip |
finish syncdb implementation (untested)
Diffstat (limited to 'src')
-rw-r--r-- | src/include/sync_database_plugin.h | 3 | ||||
-rw-r--r-- | src/sync/sync-httpd_backup_post.c | 22 | ||||
-rw-r--r-- | src/syncdb/plugin_syncdb_postgres.c | 112 |
3 files changed, 129 insertions, 8 deletions
diff --git a/src/include/sync_database_plugin.h b/src/include/sync_database_plugin.h index 15545bc..0321ce2 100644 --- a/src/include/sync_database_plugin.h +++ b/src/include/sync_database_plugin.h @@ -22,6 +22,7 @@ #define SYNC_DATABASE_PLUGIN_H #include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> #include "sync_service.h" #include <jansson.h> #include <taler/taler_util.h> @@ -179,7 +180,7 @@ struct SYNC_DatabasePlugin * @param it_cls closure for @a it * @return transaction status */ - enum SYNC_DB_QueryStatus + enum GNUNET_DB_QueryStatus (*lookup_pending_payments_by_account_TR)(void *cls, const struct SYNC_AccountPublicKeyP *account_pub, diff --git a/src/sync/sync-httpd_backup_post.c b/src/sync/sync-httpd_backup_post.c index 8cdb58a..58d517a 100644 --- a/src/sync/sync-httpd_backup_post.c +++ b/src/sync/sync-httpd_backup_post.c @@ -307,11 +307,25 @@ static int begin_payment (struct BackupContext *bc) { json_t *order; + enum GNUNET_DB_QueryStatus qs; - db->lookup_pending_payments_by_account_TR (db->cls, - &bc->account, - &ongoing_payment_cb, - bc); + qs = db->lookup_pending_payments_by_account_TR (db->cls, + &bc->account, + &ongoing_payment_cb, + bc); + if (qs < 0) + { + struct MHD_Response *resp; + int ret; + + resp = SH_RESPONSE_make_error (TALER_EC_SYNC_PAYMENT_CHECK_ORDER_DB_ERROR, + "Failed to check for existing orders in sync database"); + ret = MHD_queue_response (bc->con, + MHD_HTTP_INTERNAL_SERVER_ERROR, + resp); + MHD_destroy_response (resp); + return ret; + } if (NULL != bc->existing_order_id) { // FIXME: this is incorrect, we should FIRST check diff --git a/src/syncdb/plugin_syncdb_postgres.c b/src/syncdb/plugin_syncdb_postgres.c index 2eea1d5..7cef697 100644 --- a/src/syncdb/plugin_syncdb_postgres.c +++ b/src/syncdb/plugin_syncdb_postgres.c @@ -48,6 +48,10 @@ struct PostgresClosure */ const char *transaction_name; + /** + * Currency we accept payments in. + */ + char *currency; }; @@ -301,6 +305,85 @@ postgres_store_payment (void *cls, /** + * Closure for #payment_by_account_cb. + */ +struct PaymentIteratorContext +{ + /** + * Function to call on each result + */ + SYNC_DB_PaymentPendingIterator it; + + /** + * Closure for @e it. + */ + void *it_cls; + + /** + * Plugin context. + */ + struct PostgresClosure *pg; + + /** + * Query status to return. + */ + enum GNUNET_DB_QueryStatus qs; + +}; + + +/** + * Helper function for #postgres_lookup_pending_payments_by_account(). + * To be called with the results of a SELECT statement + * that has returned @a num_results results. + * + * @param cls closure of type `struct PaymentIteratorContext *` + * @param result the postgres result + * @param num_result the number of results in @a result + */ +static void +payment_by_account_cb (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct PaymentIteratorContext *pic = cls; + + for (unsigned int i = 0; i < num_results; i++) + { + struct GNUNET_TIME_Absolute timestamp; + char *order_id; + struct TALER_Amount amount; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_absolute_time ("timestamp", + ×tamp), + GNUNET_PQ_result_spec_string ("order_id", + &order_id), + TALER_PQ_result_spec_amount ("amount", + pic->pg->currency, + &amount), + GNUNET_PQ_result_spec_end + }; + + if (GNUNET_OK != + GNUNET_PQ_extract_result (result, + rs, + i)) + { + GNUNET_break (0); + pic->qs = GNUNET_DB_STATUS_HARD_ERROR; + return; + } + pic->qs = i + 1; + pic->it (pic->it_cls, + timestamp, + order_id, + &amount); + GNUNET_PQ_cleanup_result (rs); + } +} + + +/** * Lookup pending payments by account. * * @param cls closure @@ -309,14 +392,36 @@ postgres_store_payment (void *cls, * @param it_cls closure for @a it * @return transaction status */ -static enum SYNC_DB_QueryStatus +static enum GNUNET_DB_QueryStatus postgres_lookup_pending_payments_by_account (void *cls, const struct SYNC_AccountPublicKeyP *account_pub, SYNC_DB_PaymentPendingIterator it, void *it_cls) { - // FIXME: use payments_select_by_account + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_end + }; + struct PaymentIteratorContext pic = { + .it = it, + .it_cls = it_cls, + .pg = pg + }; + enum GNUNET_DB_QueryStatus qs; + + check_connection (pg); + postgres_preflight (pg); + qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, + "payments_select_by_account", + params, + &payment_by_account_cb, + &pic); + if (qs > 0) + return pic.qs; + GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); + return qs; } @@ -998,7 +1103,8 @@ libsync_plugin_db_postgres_init (void *cls) 0), GNUNET_PQ_make_prepare ("payments_select_by_account", "SELECT" - " order_id" + " timestamp" + ",order_id" ",amount_val" ",amount_frac" "FROM" |