pg.c (3275B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2014-2024 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 pg.c 18 * @brief Low-level (statement-level) Postgres database access for the auditor 19 * @author Christian Grothoff 20 * @author Gabor X Toth 21 */ 22 struct TALER_AUDITORDB_PostgresContext; 23 #define GNUNET_PQ_RECONNECT_CALLBACK_CLOSURE \ 24 struct TALER_AUDITORDB_PostgresContext 25 #include "taler/taler_pq_lib.h" 26 #include <pthread.h> 27 #include "auditordb_lib.h" 28 #include "pg_helper.h" 29 30 31 /** 32 * Function called each time we connect or reconnect to the 33 * database. Gives the application a chance to run some 34 * per-connection initialization logic. 35 * 36 * @param pg database conntext in the auditor 37 * @param pq database connection handle 38 */ 39 static void 40 reconnect_cb (struct TALER_AUDITORDB_PostgresContext *pg, 41 struct GNUNET_PQ_Context *pq) 42 { 43 struct GNUNET_PQ_ExecuteStatement es[] = { 44 GNUNET_PQ_make_try_execute ("SET search_path TO auditor;"), 45 GNUNET_PQ_EXECUTE_STATEMENT_END 46 }; 47 48 if (GNUNET_OK != 49 GNUNET_PQ_exec_statements (pq, 50 es)) 51 { 52 GNUNET_break (0); 53 return; 54 } 55 pg->prep_gen++; 56 } 57 58 59 /** 60 * Connect to the db if the connection does not exist yet. 61 * 62 * @param[in,out] pg the database state 63 * @return #GNUNET_OK on success 64 */ 65 static enum GNUNET_GenericReturnValue 66 setup_connection (struct TALER_AUDITORDB_PostgresContext *pg) 67 { 68 struct GNUNET_PQ_Context *db_conn; 69 70 if (NULL != pg->conn) 71 return GNUNET_OK; 72 db_conn = GNUNET_PQ_init (pg->cfg, 73 "auditordb-postgres", 74 &reconnect_cb, 75 pg); 76 if (NULL == db_conn) 77 return GNUNET_SYSERR; 78 if (0 == pg->prep_gen) 79 { 80 GNUNET_PQ_disconnect (db_conn); 81 return GNUNET_SYSERR; 82 } 83 pg->conn = db_conn; 84 return GNUNET_OK; 85 } 86 87 88 struct TALER_AUDITORDB_PostgresContext * 89 TALER_AUDITORDB_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) 90 { 91 struct TALER_AUDITORDB_PostgresContext *pg; 92 93 pg = GNUNET_new (struct TALER_AUDITORDB_PostgresContext); 94 pg->cfg = cfg; 95 if (GNUNET_OK != 96 TALER_config_get_currency (cfg, 97 "exchange", 98 &pg->currency)) 99 { 100 GNUNET_free (pg); 101 return NULL; 102 } 103 if (GNUNET_OK != 104 setup_connection (pg)) 105 { 106 TALER_AUDITORDB_disconnect (pg); 107 return NULL; 108 } 109 return pg; 110 } 111 112 113 void 114 TALER_AUDITORDB_disconnect (struct TALER_AUDITORDB_PostgresContext *pg) 115 { 116 if (NULL == pg) 117 return; 118 if (NULL != pg->conn) 119 GNUNET_PQ_disconnect (pg->conn); 120 GNUNET_free (pg->currency); 121 GNUNET_free (pg); 122 } 123 124 125 /* end of plugin_auditordb_postgres.c */