aboutsummaryrefslogtreecommitdiff
path: root/src/exchangedb/pg_helper.h
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-10-03 12:46:30 +0200
committerChristian Grothoff <christian@grothoff.org>2022-10-03 12:46:30 +0200
commitf4c8eb6a9c460a9a9b8ea1c4358e1ce87097a8f1 (patch)
treec1688e1d45ccd35484f4a7fc39567dddf9d48a3e /src/exchangedb/pg_helper.h
parent87ec6916c8d018566451ff941157d89e71dcd75a (diff)
downloadexchange-f4c8eb6a9c460a9a9b8ea1c4358e1ce87097a8f1.tar.xz
-skeletons for new PG functions
Diffstat (limited to 'src/exchangedb/pg_helper.h')
-rw-r--r--src/exchangedb/pg_helper.h149
1 files changed, 149 insertions, 0 deletions
diff --git a/src/exchangedb/pg_helper.h b/src/exchangedb/pg_helper.h
new file mode 100644
index 000000000..e0a4be49d
--- /dev/null
+++ b/src/exchangedb/pg_helper.h
@@ -0,0 +1,149 @@
+/*
+ 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 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
+{
+
+ /**
+ * Our configuration.
+ */
+ const struct GNUNET_CONFIGURATION_Handle *cfg;
+
+ /**
+ * Directory with SQL statements to run to create tables.
+ */
+ char *sql_dir;
+
+ /**
+ * After how long should idle reserves be closed?
+ */
+ struct GNUNET_TIME_Relative idle_reserve_expiration_time;
+
+ /**
+ * After how long should reserves that have seen withdraw operations
+ * be garbage collected?
+ */
+ struct GNUNET_TIME_Relative legal_reserve_expiration_time;
+
+ /**
+ * What delay should we introduce before ready transactions
+ * are actually aggregated?
+ */
+ struct GNUNET_TIME_Relative aggregator_shift;
+
+ /**
+ * Which currency should we assume all amounts to be in?
+ */
+ char *currency;
+
+ /**
+ * Our base URL.
+ */
+ char *exchange_url;
+
+ /**
+ * Postgres connection handle.
+ */
+ struct GNUNET_PQ_Context *conn;
+
+ /**
+ * Name of the current transaction, for debugging.
+ */
+ const char *transaction_name;
+
+ /**
+ * Counts how often we have established a fresh @e conn
+ * to the database. Used to re-prepare statements.
+ */
+ unsigned long long prep_gen;
+
+ /**
+ * Did we initialize the prepared statements
+ * for this session? (To be replaced with @e prep_gen.)
+ */
+ bool init;
+
+};
+
+
+/**
+ * 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 prep_cnt; \
+ \
+ if (prep_cnt < pg->prep_gen) \
+ { \
+ struct GNUNET_PQ_PreparedStatement ps[] = { \
+ GNUNET_PQ_make_prepare (name, sql, 0), \
+ GNUNET_PQ_PREPARED_STATEMENT_END \
+ }; \
+ \
+ if (GNUNET_OK != \
+ GNUNET_PQ_prepare_statements (pg->conn, \
+ ps)) \
+ { \
+ GNUNET_break (0); \
+ return GNUNET_DB_STATUS_HARD_ERROR; \
+ } \
+ prep_cnt = 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