pg_insert_product.c (4643B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022-2025 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 backenddb/pg_insert_product.c 18 * @brief Implementation of the insert_product function for Postgres 19 * @author Christian Grothoff 20 * @author Iván Ávalos 21 */ 22 #include "taler/platform.h" 23 #include <taler/taler_error_codes.h> 24 #include <taler/taler_dbevents.h> 25 #include <taler/taler_pq_lib.h> 26 #include "pg_insert_product.h" 27 #include "pg_helper.h" 28 29 30 enum GNUNET_DB_QueryStatus 31 TMH_PG_insert_product (void *cls, 32 const char *instance_id, 33 const char *product_id, 34 const struct TALER_MERCHANTDB_ProductDetails *pd, 35 size_t num_cats, 36 const uint64_t *cats, 37 bool *no_instance, 38 bool *conflict, 39 ssize_t *no_cat, 40 bool *no_group, 41 bool *no_pot) 42 { 43 struct PostgresClosure *pg = cls; 44 struct GNUNET_PQ_QueryParam params[] = { 45 GNUNET_PQ_query_param_string (instance_id), 46 GNUNET_PQ_query_param_string (product_id), 47 GNUNET_PQ_query_param_string (pd->description), 48 TALER_PQ_query_param_json (pd->description_i18n), /* $4 */ 49 GNUNET_PQ_query_param_string (pd->unit), 50 GNUNET_PQ_query_param_string (pd->image), 51 TALER_PQ_query_param_json (pd->taxes), /* $7 */ 52 TALER_PQ_query_param_array_amount_with_currency ( 53 pd->price_array_length, 54 pd->price_array, 55 pg->conn), /* $8 */ 56 GNUNET_PQ_query_param_uint64 (&pd->total_stock), /* $9 */ 57 GNUNET_PQ_query_param_uint32 (&pd->total_stock_frac), /* $10 */ 58 GNUNET_PQ_query_param_bool (pd->allow_fractional_quantity), 59 GNUNET_PQ_query_param_uint32 (&pd->fractional_precision_level), 60 TALER_PQ_query_param_json (pd->address), /* $13 */ 61 GNUNET_PQ_query_param_timestamp (&pd->next_restock), 62 GNUNET_PQ_query_param_uint32 (&pd->minimum_age), 63 GNUNET_PQ_query_param_array_uint64 (num_cats, 64 cats, 65 pg->conn), /* $16 */ 66 GNUNET_PQ_query_param_string (pd->product_name), 67 (0 == pd->product_group_id) 68 ? GNUNET_PQ_query_param_null () 69 : GNUNET_PQ_query_param_uint64 (&pd->product_group_id), /* $18 */ 70 (0 == pd->money_pot_id) 71 ? GNUNET_PQ_query_param_null () 72 : GNUNET_PQ_query_param_uint64 (&pd->money_pot_id), /* $19 */ 73 GNUNET_PQ_query_param_bool (pd->price_is_net), /* $20 */ 74 GNUNET_PQ_query_param_end 75 }; 76 uint64_t ncat; 77 bool cats_found = true; 78 struct GNUNET_PQ_ResultSpec rs[] = { 79 GNUNET_PQ_result_spec_bool ("conflict", 80 conflict), 81 GNUNET_PQ_result_spec_bool ("no_instance", 82 no_instance), 83 GNUNET_PQ_result_spec_allow_null ( 84 GNUNET_PQ_result_spec_uint64 ("no_cat", 85 &ncat), 86 &cats_found), 87 GNUNET_PQ_result_spec_bool ("no_group", 88 no_group), 89 GNUNET_PQ_result_spec_bool ("no_pot", 90 no_pot), 91 GNUNET_PQ_result_spec_end 92 }; 93 enum GNUNET_DB_QueryStatus qs; 94 95 check_connection (pg); 96 PREPARE (pg, 97 "insert_product", 98 "SELECT" 99 " out_conflict AS conflict" 100 ",out_no_instance AS no_instance" 101 ",out_no_cat AS no_cat" 102 ",out_no_group AS no_group" 103 ",out_no_pot AS no_pot" 104 " FROM merchant_do_insert_product" 105 "($1, $2, $3, $4::TEXT::JSONB, $5, $6, $7::TEXT::JSONB, $8" 106 ",$9, $10, $11, $12, $13::TEXT::JSONB, $14, $15, $16" 107 ",$17, $18, $19, $20);"); 108 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 109 "insert_product", 110 params, 111 rs); 112 GNUNET_PQ_cleanup_query_params_closures (params); 113 *no_cat = (cats_found) ? -1 : (ssize_t) ncat; 114 return qs; 115 }