summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpriscilla <priscilla.huang@efrei.net>2022-11-16 08:01:06 -0500
committerpriscilla <priscilla.huang@efrei.net>2022-11-16 08:01:06 -0500
commitec5700106a75b75310c365a92f23b4a1a55bba2c (patch)
treed042bf8bba4980ea7e8672d3d7224b4ab4b6c5c2 /src
parent343a1b107125592445762e41f1262101f5bfc626 (diff)
downloadmerchant-ec5700106a75b75310c365a92f23b4a1a55bba2c.tar.gz
merchant-ec5700106a75b75310c365a92f23b4a1a55bba2c.tar.bz2
merchant-ec5700106a75b75310c365a92f23b4a1a55bba2c.zip
template sql
Diffstat (limited to 'src')
-rw-r--r--src/backenddb/merchant-000.sql30
-rw-r--r--src/backenddb/merchant-0004.sql409
-rw-r--r--src/backenddb/plugin_merchantdb_postgres.c2
3 files changed, 440 insertions, 1 deletions
diff --git a/src/backenddb/merchant-000.sql b/src/backenddb/merchant-000.sql
new file mode 100644
index 00000000..a3c8b484
--- /dev/null
+++ b/src/backenddb/merchant-000.sql
@@ -0,0 +1,30 @@
+--
+-- This file is part of TALER
+-- Copyright (C) 2021 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/>
+--
+
+-- Everything in one big transaction
+BEGIN;
+
+-- Check patch versioning is in place.
+SELECT _v.register_patch('merchant-0003', NULL, NULL);
+
+SET search_path TO merchant;
+
+ALTER TABLE merchant_exchange_wire_fees
+ DROP COLUMN wad_fee_val,
+ DROP COLUMN wad_fee_frac;
+
+-- Complete transaction
+COMMIT;
diff --git a/src/backenddb/merchant-0004.sql b/src/backenddb/merchant-0004.sql
new file mode 100644
index 00000000..f18487cc
--- /dev/null
+++ b/src/backenddb/merchant-0004.sql
@@ -0,0 +1,409 @@
+--
+-- This file is part of TALER
+-- Copyright (C) 2021 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 merchant-004.sql
+ * @brief database helper functions for postgres used by the merchant and funtion for plugin_merchantdb_postgres.c
+ * @author Priscilla Huang
+ */
+
+-- Everything in one big transaction
+BEGIN;
+
+-- Check patch versioning is in place.
+SELECT _v.register_patch('merchant-0004', NULL, NULL);
+
+SET search_path TO merchant;
+
+-- create table here!
+
+CREATE TABLE IF NOT EXISTS merchant_template
+ (template_serial BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
+ ,merchant_serial BIGINT NOT NULL
+ REFERENCES merchant_instances (merchant_serial) ON DELETE CASCADE
+ ,template_id VARCHAR NOT NULL
+ ,template_description VARCHAR NOT NULL
+ ,image BYTEA
+ ,template_contract VARCHAR NOT NULL -- in JSON format
+ ,UNIQUE (merchant_serial, template_id)
+ );
+COMMENT ON TABLE merchant_template
+ IS 'template used by the merchant (may be incomplete, frontend can override)';
+COMMENT ON COLUMN merchant_template.template_description
+ IS 'Human-readable template description';
+COMMENT ON COLUMN merchant_template.image
+ IS 'NOT NULL, but can be 0 bytes; must contain an ImageDataUrl';
+COMMENT ON COLUMN merchant_template.template_contract
+ IS 'The template contract will contains some additional information.'
+
+
+CREATE TABLE IF NOT EXISTS merchant_using_template
+ (using_template_serial BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
+ ,merchant_serial BIGINT NOT NULL
+ REFERENCES merchant_instances (merchant_serial) ON DELETE CASCADE
+ ,subject VARCHAR
+ ,amount_val INT8 VARCHAR
+ ,amount_frac INT4 VARCHAR
+ ,UNIQUE (merchant_serial, template_id)
+ );
+COMMENT ON TABLE merchant_using_template
+ IS 'Public template used by the customer (may be incomplete, frontend can override';
+COMMENT ON COLUMN merchant_using_template.subject
+ IS 'Customer can write the subject of the payment';
+COMMENT ON COLUMN merchant_using_template.amount_val
+ IS 'Current amount that needs to be enter by the customeer';
+
+
+CREATE TABLE IF NOT EXISTS merchant_using_template_response
+ (using_template_response_serial BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
+ ,merchant_serial BIGINT NOT NULL
+ REFERENCES merchant_instances (merchant_serial) ON DELETE CASCADE
+ ,taler_url VARCHAR NOT NULL
+ ,UNIQUE (merchant_serial, template_id)
+ );
+COMMENT ON TABLE merchant_using_template_response
+ IS 'This table is used when the customer needs to finish his payment';
+COMMENT ON COLUMN merchant_using_template.taler_url
+ IS 'Returns an url when the user go through a browser';
+
+
+
+
+-- C CODE
+
+
+
+/**
+ * Delete information about a template. Note that the transaction must
+ * enforce that no stocks are currently locked.
+ *
+ * @param cls closure
+ * @param instance_id instance to delete product of
+ * @param template_id template to delete
+ * @return DB status code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
+ * if locks prevent deletion OR template unknown
+ */
+static enum GNUNET_DB_QueryStatus
+postgres_delete_template (void *cls,
+ const char *instance_id,
+ const char *template_id)
+{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_string (instance_id),
+ GNUNET_PQ_query_param_string (template_id),
+ GNUNET_PQ_query_param_end
+ };
+
+ check_connection (pg);
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "delete_template",
+ params);
+}
+
+/**
+ * Insert details about a particular product.
+ *
+ * @param cls closure
+ * @param instance_id instance to insert product for
+ * @param template_id product identifier of template to insert
+ * @param pd the template details to insert
+ * @return database result code
+ */
+static enum GNUNET_DB_QueryStatus
+postgres_insert_template (void *cls,
+ const char *instance_id,
+ const char *template_id,
+ const struct TALER_MERCHANTDB_TemplateDetails *pd)
+{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_string (instance_id),
+ GNUNET_PQ_query_param_string (template_id),
+ GNUNET_PQ_query_param_string (pd->template_description),
+ GNUNET_PQ_query_param_string (pd->image),
+ GNUNET_PQ_query_param param2[],
+ GNUNET_PQ_query_param_end
+
+ };
+
+ check_connection (pg);
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "insert_template",
+ params);
+}
+
+
+{
+ struct PostgresClosure *pg = cls2;
+ struct GNUNET_PQ_QueryParam param2[] = {
+ TALER_PQ_query_param_amount (&pd->amount),
+ GNUNET_PQ_query_param_string (pd->summary),
+ GNUNET_PQ_query_param_relativetime (&pd->pay_duration),
+ GNUNET_PQ_query_param_uint32 (&pd->minimum_age),
+
+ };
+
+ check_connection (pg);
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "insert_template",
+ params);
+}
+
+/**
+ * Update details about a particular template. Note that the
+ * transaction must enforce that the sold/stocked/lost counters
+ * are not reduced (i.e. by expanding the WHERE clause on the existing
+ * values).
+ *
+ * @param cls closure
+ * @param instance_id instance to lookup template for
+ * @param template_id template to lookup
+ * @param[out] pd set to the template details on success, can be NULL
+ * (in that case we only want to check if the template exists)
+ * @return database result code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the
+ * non-decreasing constraints are not met *or* if the template
+ * does not yet exist.
+ */
+
+static enum GNUNET_DB_QueryStatus
+postgres_update_product (void *cls,
+ const char *instance_id,
+ const char *template_id,
+ const struct TALER_MERCHANTDB_TemplateDetails *pd)
+{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_string (instance_id),
+ GNUNET_PQ_query_param_string (template_id),
+ GNUNET_PQ_query_param_string (pd->template_description),
+ GNUNET_PQ_query_param_string (pd->image),
+ TALER_PQ_query_param_amount (&pd->amount),
+ GNUNET_PQ_query_param param2[],
+ GNUNET_PQ_query_param_end
+ };
+
+ {
+ GNUNET_break (0);
+ return GNUNET_DB_STATUS_HARD_ERROR;
+ }
+ check_connection (pg);
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "update_template",
+ params);
+}
+
+
+
+{
+ struct PostgresClosure *pg = cls2;
+ struct GNUNET_PQ_QueryParam param2[] = {
+ TALER_PQ_query_param_amount (&pd->amount),
+ GNUNET_PQ_query_param_string (pd->summary),
+ GNUNET_PQ_query_param_relativetime (&pd->pay_duration),
+ GNUNET_PQ_query_param_uint32 (&pd->minimum_age),
+
+ };
+ {
+ GNUNET_break (0);
+ return GNUNET_DB_STATUS_HARD_ERROR;
+ }
+ check_connection (pg);
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "update_template",
+ params);
+}
+
+
+
+
+/**
+ * Context used for postgres_lookup_template().
+ */
+struct LookupTemplateContext
+{
+ /**
+ * Function to call with the results.
+ */
+ TALER_MERCHANTDB_TemplateCallback cb;
+
+ /**
+ * Closure for @a cb.
+ */
+ void *cb_cls;
+
+ /**
+ * Did database result extraction fail?
+ */
+ bool extract_failed;
+};
+
+
+/**
+ * Function to be called with the results of a SELECT statement
+ * that has returned @a num_results results about template.
+ *
+ * @param[in,out] cls of type `struct LookupTemplateContext *`
+ * @param result the postgres result
+ * @param num_results the number of results in @a result
+ */
+static void
+lookup_templates_cb (void *cls,
+ PGresult *result,
+ unsigned int num_results)
+{
+ struct LookupTemplateContext *tlc = cls;
+
+ for (unsigned int i = 0; i < num_results; i++)
+ {
+ char *template_id;
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ GNUNET_PQ_result_spec_string ("template_id",
+ &template_id),
+ GNUNET_PQ_result_spec_end
+ };
+
+ if (GNUNET_OK !=
+ GNUNET_PQ_extract_result (result,
+ rs,
+ i))
+ {
+ GNUNET_break (0);
+ tlc->extract_failed = true;
+ return;
+ }
+ tlc->cb (tlc->cb_cls,
+ template_id);
+ GNUNET_PQ_cleanup_result (rs);
+ }
+}
+
+/**
+ * Lookup all of the template the given instance has configured.
+ *
+ * @param cls closure
+ * @param instance_id instance to lookup template for
+ * @param cb function to call on all template found
+ * @param cb_cls closure for @a cb
+ * @return database result code
+ */
+static enum GNUNET_DB_QueryStatus
+postgres_lookup_templates (void *cls,
+ const char *instance_id,
+ TALER_MERCHANTDB_ProductsCallback cb,
+ void *cb_cls)
+{
+ struct PostgresClosure *pg = cls;
+ struct LookupTemplateContext tlc = {
+ .cb = cb,
+ .cb_cls = cb_cls,
+ /* Can be overwritten by the lookup_template_cb */
+ .extract_failed = false,
+ };
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_string (instance_id),
+ GNUNET_PQ_query_param_end
+ };
+ enum GNUNET_DB_QueryStatus qs;
+
+ check_connection (pg);
+ qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
+ "lookup_templates",
+ params,
+ &lookup_templates_cb,
+ &tlc);
+ /* If there was an error inside lookup_template_cb, return a hard error. */
+ if (tlc.extract_failed)
+ return GNUNET_DB_STATUS_HARD_ERROR;
+ return qs;
+}
+
+
+/**
+ * Lookup details about a particular template.
+ *
+ * @param cls closure
+ * @param instance_id instance to lookup template for
+ * @param template_id template to lookup
+ * @param[out] pd set to the product details on success, can be NULL
+ * (in that case we only want to check if the template exists)
+ * @return database result code
+ */
+static enum GNUNET_DB_QueryStatus
+postgres_lookup_template (void *cls,
+ const char *instance_id,
+ const char *template_id,
+ struct TALER_MERCHANTDB_EtemplateDetails *pd)
+{
+ struct PostgresClosure *pg = cls;
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_string (instance_id),
+ GNUNET_PQ_query_param_string (template_id),
+ GNUNET_PQ_query_param_end
+ };
+
+ if (NULL == pd)
+ {
+ struct GNUNET_PQ_ResultSpec rs_null[] = {
+ GNUNET_PQ_result_spec_end
+ };
+
+ check_connection (pg);
+ return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
+ "lookup_template",
+ params,
+ rs_null);
+ }
+ else
+ {
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ GNUNET_PQ_result_spec_string ("template_description",
+ &pd->template_description),
+ GNUNET_PQ_result_spec_string ("image",
+ &pd->image),
+ GNUNET_PQ_ResultSpec rs2[],
+ GNUNET_PQ_result_spec_end
+ };
+
+ check_connection (pg);
+ return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
+ "lookup_template",
+ params,
+ rs);
+ struct GNUNET_PQ_ResultSpec rs2[] = {
+ GNUNET_PQ_result_spec_string ("Summary",
+ &pd->summary),
+ TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
+ &pd->amount),
+ GNUNET_PQ_result_spec_uint64 ("minimum_age",
+ &pd->minimum_age),
+ GNUNET_PQ_result_spec_relativetime ("pay_duration",
+ &pd->pay_duration),
+ GNUNET_PQ_result_spec_end
+
+ };
+
+ check_connection (pg);
+ return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
+ "lookup_template",
+ params,
+ rs);
+ }
+}
+
+
+
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c
index 8d69551c..ca977c76 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -1298,7 +1298,6 @@ lookup_products_cb (void *cls,
}
}
-
/**
* Lookup all of the products the given instance has configured.
*
@@ -1340,6 +1339,7 @@ postgres_lookup_products (void *cls,
}
+
/**
* Lookup details about a particular product.
*