diff options
-rw-r--r-- | src/backenddb/Makefile.am | 2 | ||||
-rw-r--r-- | src/backenddb/pg_helper.h | 116 | ||||
-rw-r--r-- | src/backenddb/pg_template.c | 26 | ||||
-rw-r--r-- | src/backenddb/pg_template.h | 29 | ||||
-rwxr-xr-x | src/backenddb/pg_template.sh | 21 | ||||
-rw-r--r-- | src/backenddb/plugin_merchantdb_postgres.c | 77 |
6 files changed, 197 insertions, 74 deletions
diff --git a/src/backenddb/Makefile.am b/src/backenddb/Makefile.am index a8fefb40..c1080024 100644 --- a/src/backenddb/Makefile.am +++ b/src/backenddb/Makefile.am @@ -35,7 +35,7 @@ lib_LTLIBRARIES = \ libtalermerchantdb_la_SOURCES = \ merchantdb_plugin.c \ - merchantdb_helper.c + merchantdb_helper.c pg_helper.h libtalermerchantdb_la_LIBADD = \ $(LIBGCRYPT_LIBS) \ diff --git a/src/backenddb/pg_helper.h b/src/backenddb/pg_helper.h new file mode 100644 index 00000000..d2e9035b --- /dev/null +++ b/src/backenddb/pg_helper.h @@ -0,0 +1,116 @@ +/* + This file is part of TALER + Copyright (C) 2023 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ +/** + * @file pg_helper.h + * @brief shared internal definitions for postgres DB plugin + * @author Christian Grothoff + */ +#ifndef PG_HELPER_H +#define PG_HELPER_H + + +/** + * Type of the "cls" argument given to each of the functions in + * our API. + */ +struct PostgresClosure +{ + + /** + * Postgres connection handle. + */ + struct GNUNET_PQ_Context *conn; + + /** + * Which currency do we deal in? + */ + char *currency; + + /** + * Directory with SQL statements to run to create tables. + */ + char *sql_dir; + + /** + * Underlying configuration. + */ + const struct GNUNET_CONFIGURATION_Handle *cfg; + + /** + * Name of the currently active transaction, NULL if none is active. + */ + const char *transaction_name; + + +}; + + +/** + * Prepares SQL statement @a sql under @a name for + * connection @a pg once. + * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. + * + * @param pg a `struct PostgresClosure` + * @param name name to prepare the statement under + * @param sql actual SQL text + */ +#define PREPARE(pg,name,sql) \ + do { \ + static unsigned long long gen; \ + \ + if (gen < pg->prep_gen) \ + { \ + struct GNUNET_PQ_PreparedStatement ps[] = { \ + GNUNET_PQ_make_prepare (name, sql), \ + GNUNET_PQ_PREPARED_STATEMENT_END \ + }; \ + \ + if (GNUNET_OK != \ + GNUNET_PQ_prepare_statements (pg->conn, \ + ps)) \ + { \ + GNUNET_break (0); \ + return GNUNET_DB_STATUS_HARD_ERROR; \ + } \ + gen = pg->prep_gen; \ + } \ + } while (0) + + +/** + * Wrapper macro to add the currency from the plugin's state + * when fetching amounts from the database. + * + * @param field name of the database field to fetch amount from + * @param[out] amountp pointer to amount to set + */ +#define TALER_PQ_RESULT_SPEC_AMOUNT(field,amountp) TALER_PQ_result_spec_amount ( \ + field,pg->currency,amountp) + + +/** + * Wrapper macro to add the currency from the plugin's state + * when fetching amounts from the database. NBO variant. + * + * @param field name of the database field to fetch amount from + * @param[out] amountp pointer to amount to set + */ +#define TALER_PQ_RESULT_SPEC_AMOUNT_NBO(field, \ + amountp) TALER_PQ_result_spec_amount_nbo ( \ + field,pg->currency,amountp) + + +#endif diff --git a/src/backenddb/pg_template.c b/src/backenddb/pg_template.c new file mode 100644 index 00000000..30ea64d0 --- /dev/null +++ b/src/backenddb/pg_template.c @@ -0,0 +1,26 @@ +/* + This file is part of TALER + Copyright (C) 2022 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_template.c + * @brief Implementation of the template function for Postgres + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler_error_codes.h" +#include "taler_dbevents.h" +#include "taler_pq_lib.h" +#include "pg_template.h" +#include "pg_helper.h" diff --git a/src/backenddb/pg_template.h b/src/backenddb/pg_template.h new file mode 100644 index 00000000..81415a41 --- /dev/null +++ b/src/backenddb/pg_template.h @@ -0,0 +1,29 @@ +/* + This file is part of TALER + Copyright (C) 2022 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ +/** + * @file backenddb/pg_template.h + * @brief implementation of the template function for Postgres + * @author Christian Grothoff + */ +#ifndef PG_TEMPLATE_H +#define PG_TEMPLATE_H + +#include "taler_util.h" +#include "taler_json_lib.h" +#include "taler_merchantdb_plugin.h" + + +#endif diff --git a/src/backenddb/pg_template.sh b/src/backenddb/pg_template.sh new file mode 100755 index 00000000..087d6ab8 --- /dev/null +++ b/src/backenddb/pg_template.sh @@ -0,0 +1,21 @@ +#!/bin/sh +# This file is in the public domain. +# +# Instantiates pg_template for a particular function. + +for n in $* +do + NCAPS=`echo $n | tr a-z A-Z` + if test ! -e pg_$n.c + then + cat pg_template.c | sed -e s/template/$n/g -e s/TEMPLATE/$NCAPS/g > pg_$n.c + cat pg_template.h | sed -e s/template/$n/g -e s/TEMPLATE/$NCAPS/g > pg_$n.h + echo " plugin->$n\n = &TMH_PG_$n;" >> tmpl.c + echo "#include \"pg_$n.h\"" >> tmpl.inc + echo " pg_$n.h pg_$n.c \\" >> tmpl.am + fi +done + +echo "Add lines from tmpl.am to Makefile.am" +echo "Add lines from tmpl.inc to plugin_merchantdb_postgres.c at the beginning" +echo "Add lines from tmpl.c to plugin_merchantdb_postgres.c at the end" diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index b9c18b4f..ff7e1dd3 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -29,6 +29,7 @@ #include <taler/taler_json_lib.h> #include <taler/taler_mhd_lib.h> #include "taler_merchantdb_plugin.h" +#include "pg_helper.h" /** * How often do we re-try if we run into a DB serialization error? @@ -37,76 +38,6 @@ /** - * Wrapper macro to add the currency from the plugin's state - * when fetching amounts from the database. - * - * @param field name of the database field to fetch amount from - * @param[out] amountp pointer to amount to set - */ -#define TALER_PQ_RESULT_SPEC_AMOUNT(field,amountp) \ - TALER_PQ_result_spec_amount ( \ - field,pg->currency,amountp) - -/** - * Wrapper macro to add the currency from the plugin's state - * when fetching amounts from the database. NBO variant. - * - * @param field name of the database field to fetch amount from - * @param[out] amountp pointer to amount to set - */ -#define TALER_PQ_RESULT_SPEC_AMOUNT_NBO(field, amountp) \ - TALER_PQ_result_spec_amount_nbo ( \ - field,pg->currency,amountp) - - -/** - * Wrapper macro to add the currency from the plugin's state - * when fetching amounts from the database. - * - * @param field name of the database field to fetch amount from - * @param[out] amountp pointer to amount to set - */ -#define TALER_PQ_RESULT_SPEC_AMOUNT(field,amountp) \ - TALER_PQ_result_spec_amount ( \ - field,pg->currency,amountp) - - -/** - * Type of the "cls" argument given to each of the functions in - * our API. - */ -struct PostgresClosure -{ - - /** - * Postgres connection handle. - */ - struct GNUNET_PQ_Context *conn; - - /** - * Which currency do we deal in? - */ - char *currency; - - /** - * Directory with SQL statements to run to create tables. - */ - char *sql_dir; - - /** - * Underlying configuration. - */ - const struct GNUNET_CONFIGURATION_Handle *cfg; - - /** - * Name of the currently active transaction, NULL if none is active. - */ - const char *transaction_name; - -}; - - -/** * Drop all Taler tables. This should only be used by testcases. * * @param cls the `struct PostgresClosure` with the plugin-specific state @@ -7176,9 +7107,9 @@ postgres_lookup_template (void *cls, check_connection (pg); td->pos_key = NULL; qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "lookup_template", - params, - rs); + "lookup_template", + params, + rs); td->pos_algorithm = (enum TALER_MerchantConfirmationAlgorithm) pos32; return qs; } |