From 6cf8a87be1ec8cb846a361d35b68c5602e174fa5 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 8 May 2023 22:25:45 -0600 Subject: Factor out {lookup,delete,insert,update,lock}_product(s) (shit job) --- src/backenddb/plugin_merchantdb_postgres.c | 477 ++--------------------------- 1 file changed, 18 insertions(+), 459 deletions(-) (limited to 'src/backenddb/plugin_merchantdb_postgres.c') diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index 0e0e7abb..0e5f921f 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -53,6 +53,12 @@ #include "pg_update_instance_auth.h" #include "pg_inactivate_account.h" #include "pg_activate_account.h" +#include "pg_lookup_products.h" +#include "pg_lookup_product.h" +#include "pg_delete_product.h" +#include "pg_insert_product.h" +#include "pg_update_product.h" +#include "pg_lock_product.h" #include "pg_set_transfer_status_to_confirmed.h" @@ -330,346 +336,6 @@ postgres_commit (void *cls) } -/** - * Context used for postgres_lookup_products(). - */ -struct LookupProductsContext -{ - /** - * Function to call with the results. - */ - TALER_MERCHANTDB_ProductsCallback cb; - - /** - * Closure for @a cb. - */ - void *cb_cls; - - /** - * Did database result extraction fail? - */ - bool extract_failed; -}; - - -/** - * Function to be called with the results of a SELECT statement - * that has returned @a num_results results about products. - * - * @param[in,out] cls of type `struct LookupProductsContext *` - * @param result the postgres result - * @param num_results the number of results in @a result - */ -static void -lookup_products_cb (void *cls, - PGresult *result, - unsigned int num_results) -{ - struct LookupProductsContext *plc = cls; - - for (unsigned int i = 0; i < num_results; i++) - { - char *product_id; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_string ("product_id", - &product_id), - GNUNET_PQ_result_spec_end - }; - - if (GNUNET_OK != - GNUNET_PQ_extract_result (result, - rs, - i)) - { - GNUNET_break (0); - plc->extract_failed = true; - return; - } - plc->cb (plc->cb_cls, - product_id); - GNUNET_PQ_cleanup_result (rs); - } -} - - -/** - * Lookup all of the products the given instance has configured. - * - * @param cls closure - * @param instance_id instance to lookup products for - * @param cb function to call on all products found - * @param cb_cls closure for @a cb - * @return database result code - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_products (void *cls, - const char *instance_id, - TALER_MERCHANTDB_ProductsCallback cb, - void *cb_cls) -{ - struct PostgresClosure *pg = cls; - struct LookupProductsContext plc = { - .cb = cb, - .cb_cls = cb_cls, - /* Can be overwritten by the lookup_products_cb */ - .extract_failed = false, - }; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_end - }; - enum GNUNET_DB_QueryStatus qs; - - check_connection (pg); - qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, - "lookup_products", - params, - &lookup_products_cb, - &plc); - /* If there was an error inside lookup_products_cb, return a hard error. */ - if (plc.extract_failed) - return GNUNET_DB_STATUS_HARD_ERROR; - return qs; -} - - -/** - * Lookup details about a particular product. - * - * @param cls closure - * @param instance_id instance to lookup products for - * @param product_id product to lookup - * @param[out] pd set to the product details on success, can be NULL - * (in that case we only want to check if the product exists) - * @return database result code - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_product (void *cls, - const char *instance_id, - const char *product_id, - struct TALER_MERCHANTDB_ProductDetails *pd) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_string (product_id), - GNUNET_PQ_query_param_end - }; - - if (NULL == pd) - { - struct GNUNET_PQ_ResultSpec rs_null[] = { - GNUNET_PQ_result_spec_end - }; - - check_connection (pg); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "lookup_product", - params, - rs_null); - } - else - { - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_string ("description", - &pd->description), - TALER_PQ_result_spec_json ("description_i18n", - &pd->description_i18n), - GNUNET_PQ_result_spec_string ("unit", - &pd->unit), - TALER_PQ_RESULT_SPEC_AMOUNT ("price", - &pd->price), - TALER_PQ_result_spec_json ("taxes", - &pd->taxes), - GNUNET_PQ_result_spec_uint64 ("total_stock", - &pd->total_stock), - GNUNET_PQ_result_spec_uint64 ("total_sold", - &pd->total_sold), - GNUNET_PQ_result_spec_uint64 ("total_lost", - &pd->total_lost), - GNUNET_PQ_result_spec_string ("image", - &pd->image), - TALER_PQ_result_spec_json ("address", - &pd->address), - GNUNET_PQ_result_spec_timestamp ("next_restock", - &pd->next_restock), - GNUNET_PQ_result_spec_uint32 ("minimum_age", - &pd->minimum_age), - GNUNET_PQ_result_spec_end - }; - - check_connection (pg); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "lookup_product", - params, - rs); - } -} - - -/** - * Delete information about a product. Note that the transaction must - * enforce that no stocks are currently locked. - * - * @param cls closure - * @param instance_id instance to delete product of - * @param product_id product to delete - * @return DB status code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS - * if locks prevent deletion OR product unknown - */ -static enum GNUNET_DB_QueryStatus -postgres_delete_product (void *cls, - const char *instance_id, - const char *product_id) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_string (product_id), - GNUNET_PQ_query_param_end - }; - - check_connection (pg); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "delete_product", - params); -} - - -/** - * Insert details about a particular product. - * - * @param cls closure - * @param instance_id instance to insert product for - * @param product_id product identifier of product to insert - * @param pd the product details to insert - * @return database result code - */ -static enum GNUNET_DB_QueryStatus -postgres_insert_product (void *cls, - const char *instance_id, - const char *product_id, - const struct TALER_MERCHANTDB_ProductDetails *pd) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_string (product_id), - GNUNET_PQ_query_param_string (pd->description), - TALER_PQ_query_param_json (pd->description_i18n), - GNUNET_PQ_query_param_string (pd->unit), - GNUNET_PQ_query_param_string (pd->image), - TALER_PQ_query_param_json (pd->taxes), - TALER_PQ_query_param_amount (&pd->price), - GNUNET_PQ_query_param_uint64 (&pd->total_stock), - TALER_PQ_query_param_json (pd->address), - GNUNET_PQ_query_param_timestamp (&pd->next_restock), - GNUNET_PQ_query_param_uint32 (&pd->minimum_age), - GNUNET_PQ_query_param_end - }; - - check_connection (pg); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "insert_product", - params); -} - - -/** - * Update details about a particular product. Note that the - * transaction must enforce that the sold/stocked/lost counters - * are not reduced (i.e. by expanding the WHERE clause on the existing - * values). - * - * @param cls closure - * @param instance_id instance to lookup products for - * @param product_id product to lookup - * @param[out] pd set to the product details on success, can be NULL - * (in that case we only want to check if the product exists) - * total_sold in @a pd is ignored, total_lost must not - * exceed total_stock minus the existing total_sold; - * total_sold and total_stock must be larger or equal to - * the existing value; - * @return database result code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the - * non-decreasing constraints are not met *or* if the product - * does not yet exist. - */ -static enum GNUNET_DB_QueryStatus -postgres_update_product (void *cls, - const char *instance_id, - const char *product_id, - const struct TALER_MERCHANTDB_ProductDetails *pd) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), /* $1 */ - GNUNET_PQ_query_param_string (product_id), - GNUNET_PQ_query_param_string (pd->description), - TALER_PQ_query_param_json (pd->description_i18n), - GNUNET_PQ_query_param_string (pd->unit), - GNUNET_PQ_query_param_string (pd->image), /* $6 */ - TALER_PQ_query_param_json (pd->taxes), - TALER_PQ_query_param_amount (&pd->price), /* $8+$9 */ - GNUNET_PQ_query_param_uint64 (&pd->total_stock), /* $10 */ - GNUNET_PQ_query_param_uint64 (&pd->total_lost), - TALER_PQ_query_param_json (pd->address), - GNUNET_PQ_query_param_timestamp (&pd->next_restock), - GNUNET_PQ_query_param_uint32 (&pd->minimum_age), - GNUNET_PQ_query_param_end - }; - - if ( (pd->total_stock < pd->total_lost + pd->total_sold) || - (pd->total_lost < pd->total_lost - + pd->total_sold) /* integer overflow */) - { - GNUNET_break (0); - return GNUNET_DB_STATUS_HARD_ERROR; - } - check_connection (pg); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "update_product", - params); -} - - -/** - * Lock stocks of a particular product. Note that the transaction must - * enforce that the "stocked-sold-lost >= locked" constraint holds. - * - * @param cls closure - * @param instance_id instance to lookup products for - * @param product_id product to lookup - * @param uuid the UUID that holds the lock - * @param quantity how many units should be locked - * @param expiration_time when should the lock expire - * @return database result code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the - * product is unknown OR if there insufficient stocks remaining - */ -static enum GNUNET_DB_QueryStatus -postgres_lock_product (void *cls, - const char *instance_id, - const char *product_id, - const struct GNUNET_Uuid *uuid, - uint64_t quantity, - struct GNUNET_TIME_Timestamp expiration_time) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_string (product_id), - GNUNET_PQ_query_param_auto_from_type (uuid), - GNUNET_PQ_query_param_uint64 (&quantity), - GNUNET_PQ_query_param_timestamp (&expiration_time), - GNUNET_PQ_query_param_end - }; - - check_connection (pg); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "lock_product", - params); -} - - /** * Release all expired product locks, including * those from expired offers -- across all @@ -6540,119 +6206,6 @@ postgres_connect (void *cls) struct GNUNET_PQ_PreparedStatement ps[] = { GNUNET_PQ_make_prepare ("end_transaction", "COMMIT"), - /* for postgres_lookup_products() */ - GNUNET_PQ_make_prepare ("lookup_products", - "SELECT" - " product_id" - " FROM merchant_inventory" - " JOIN merchant_instances" - " USING (merchant_serial)" - " WHERE merchant_instances.merchant_id=$1"), - /* for postgres_lookup_product() */ - GNUNET_PQ_make_prepare ("lookup_product", - "SELECT" - " description" - ",description_i18n" - ",unit" - ",price_val" - ",price_frac" - ",taxes" - ",total_stock" - ",total_sold" - ",total_lost" - ",image" - ",merchant_inventory.address" - ",next_restock" - ",minimum_age" - " FROM merchant_inventory" - " JOIN merchant_instances" - " USING (merchant_serial)" - " WHERE merchant_instances.merchant_id=$1" - " AND merchant_inventory.product_id=$2"), - /* for postgres_delete_product() */ - GNUNET_PQ_make_prepare ("delete_product", - "DELETE" - " FROM merchant_inventory" - " WHERE merchant_inventory.merchant_serial=" - " (SELECT merchant_serial " - " FROM merchant_instances" - " WHERE merchant_id=$1)" - " AND merchant_inventory.product_id=$2" - " AND product_serial NOT IN " - " (SELECT product_serial FROM merchant_order_locks)" - " AND product_serial NOT IN " - " (SELECT product_serial FROM merchant_inventory_locks)"), - /* for postgres_insert_product() */ - GNUNET_PQ_make_prepare ("insert_product", - "INSERT INTO merchant_inventory" - "(merchant_serial" - ",product_id" - ",description" - ",description_i18n" - ",unit" - ",image" - ",taxes" - ",price_val" - ",price_frac" - ",total_stock" - ",address" - ",next_restock" - ",minimum_age" - ")" - " SELECT merchant_serial," - " $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13" - " FROM merchant_instances" - " WHERE merchant_id=$1"), - /* for postgres_update_product() */ - GNUNET_PQ_make_prepare ("update_product", - "UPDATE merchant_inventory SET" - " description=$3" - ",description_i18n=$4" - ",unit=$5" - ",image=$6" - ",taxes=$7" - ",price_val=$8" - ",price_frac=$9" - ",total_stock=$10" - ",total_lost=$11" - ",address=$12" - ",next_restock=$13" - ",minimum_age=$14" - " WHERE merchant_serial=" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)" - " AND product_id=$2" - " AND total_stock <= $10" - " AND total_lost <= $11"), - - /* for postgres_lock_product() */ - GNUNET_PQ_make_prepare ("lock_product", - "WITH ps AS" - " (SELECT product_serial" - " FROM merchant_inventory" - " WHERE product_id=$2" - " AND merchant_serial=" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1))" - "INSERT INTO merchant_inventory_locks" - "(product_serial" - ",lock_uuid" - ",total_locked" - ",expiration)" - " SELECT product_serial, $3, $4, $5" - " FROM merchant_inventory" - " JOIN ps USING (product_serial)" - " WHERE " - " total_stock - total_sold - total_lost - $4 >= " - " (SELECT COALESCE(SUM(total_locked), 0)" - " FROM merchant_inventory_locks" - " WHERE product_serial=ps.product_serial) + " - " (SELECT COALESCE(SUM(total_locked), 0)" - " FROM merchant_order_locks" - " WHERE product_serial=ps.product_serial)"), - /* for postgres_expire_locks() */ GNUNET_PQ_make_prepare ("unlock_products", "DELETE FROM merchant_inventory_locks" @@ -8861,12 +8414,18 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) = &TMH_PG_inactivate_account; plugin->update_transfer_status = &TMH_PG_update_transfer_status; - plugin->lookup_products = &postgres_lookup_products; - plugin->lookup_product = &postgres_lookup_product; - plugin->delete_product = &postgres_delete_product; - plugin->insert_product = &postgres_insert_product; - plugin->update_product = &postgres_update_product; - plugin->lock_product = &postgres_lock_product; + plugin->lookup_products + = &TMH_PG_lookup_products; + plugin->lookup_product + = &TMH_PG_lookup_product; + plugin->delete_product + = &TMH_PG_delete_product; + plugin->insert_product + = &TMH_PG_insert_product; + plugin->update_product + = &TMH_PG_update_product; + plugin->lock_product + = &TMH_PG_lock_product; plugin->expire_locks = &postgres_expire_locks; plugin->delete_order = &postgres_delete_order; plugin->lookup_order = &postgres_lookup_order; -- cgit v1.2.3