pg.c (2898B)
1 /* 2 This file is part of TALER 3 (C) 2014--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 Lesser 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 src/backenddb/pg.c 18 * @brief database helper functions for postgres used by the merchant 19 * @author Sree Harsha Totakura <sreeharsha@totakura.in> 20 * @author Christian Grothoff 21 * @author Marcello Stanisci 22 * @author Priscilla Huang 23 * @author Iván Ávalos 24 */ 25 #include "platform.h" 26 #include <gnunet/gnunet_util_lib.h> 27 #include <gnunet/gnunet_pq_lib.h> 28 #include <taler/taler_util.h> 29 #include <taler/taler_pq_lib.h> 30 #include <taler/taler_json_lib.h> 31 #include <taler/taler_mhd_lib.h> 32 #include "merchantdb_lib.h" 33 #include "helper.h" 34 35 36 void 37 check_connection (struct TALER_MERCHANTDB_PostgresContext *pg) 38 { 39 if (NULL != pg->transaction_name) 40 return; 41 GNUNET_PQ_reconnect_if_down (pg->conn); 42 } 43 44 45 struct TALER_MERCHANTDB_PostgresContext * 46 TALER_MERCHANTDB_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) 47 { 48 struct TALER_MERCHANTDB_PostgresContext *pg; 49 struct GNUNET_PQ_ExecuteStatement es[] = { 50 GNUNET_PQ_make_try_execute ("SET search_path TO merchant;"), 51 GNUNET_PQ_EXECUTE_STATEMENT_END 52 }; 53 54 pg = GNUNET_new (struct TALER_MERCHANTDB_PostgresContext); 55 pg->cfg = cfg; 56 if (GNUNET_OK != 57 GNUNET_CONFIGURATION_get_value_filename (cfg, 58 "merchantdb-postgres", 59 "SQL_DIR", 60 &pg->sql_dir)) 61 { 62 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 63 "merchantdb-postgres", 64 "SQL_DIR"); 65 GNUNET_free (pg); 66 return NULL; 67 } 68 pg->conn = GNUNET_PQ_connect_with_cfg2 (pg->cfg, 69 "merchantdb-postgres", 70 "merchant-", 71 es, 72 NULL, /* prepared statemetns */ 73 GNUNET_PQ_FLAG_CHECK_CURRENT); 74 pg->prep_gen++; 75 if (NULL == pg->conn) 76 { 77 TALER_MERCHANTDB_disconnect (pg); 78 return NULL; 79 } 80 return pg; 81 } 82 83 84 void 85 TALER_MERCHANTDB_disconnect (struct TALER_MERCHANTDB_PostgresContext *pg) 86 { 87 GNUNET_PQ_disconnect (pg->conn); 88 GNUNET_free (pg); 89 } 90 91 92 /* end of pg.c */