summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcello Stanisci <stanisci.m@gmail.com>2018-12-14 19:00:37 +0100
committerMarcello Stanisci <stanisci.m@gmail.com>2018-12-14 19:00:37 +0100
commitd52b2b0c7ff423ba84f94f53affd6ec793469807 (patch)
treeb02db82fb65c57b951b2ae7879be226563ae0032
parent5cab5dc7b7a8dac374bb17108a041ac41d2bb4a9 (diff)
downloadmerchant-d52b2b0c7ff423ba84f94f53affd6ec793469807.tar.gz
merchant-d52b2b0c7ff423ba84f94f53affd6ec793469807.tar.bz2
merchant-d52b2b0c7ff423ba84f94f53affd6ec793469807.zip
/history API mod.
Provide option to return results in ascending order.
-rw-r--r--src/backend/taler-merchant-httpd_history.c10
-rw-r--r--src/backenddb/plugin_merchantdb_postgres.c42
-rw-r--r--src/backenddb/test_merchantdb.c2
-rw-r--r--src/include/taler_merchantdb_plugin.h2
4 files changed, 54 insertions, 2 deletions
diff --git a/src/backend/taler-merchant-httpd_history.c b/src/backend/taler-merchant-httpd_history.c
index abab7cd0..ce234285 100644
--- a/src/backend/taler-merchant-httpd_history.c
+++ b/src/backend/taler-merchant-httpd_history.c
@@ -125,6 +125,7 @@ MH_handler_history (struct TMH_RequestHandler *rh,
struct GNUNET_TIME_Absolute date;
json_t *response;
int ret;
+ unsigned int ascending = GNUNET_NO;
unsigned long long seconds;
struct MerchantInstance *mi;
unsigned long long start = UINT64_MAX;
@@ -247,12 +248,21 @@ MH_handler_history (struct TMH_RequestHandler *rh,
pcc.response = response;
pcc.failure = GNUNET_NO;
+
+ str = MHD_lookup_connection_value (connection,
+ MHD_GET_ARGUMENT_KIND,
+ "ordering");
+ if ((NULL != str) && (0 == strcmp ("ascending",
+ str)))
+ ascending = GNUNET_YES;
+
qs = db->find_contract_terms_by_date_and_range (db->cls,
date,
&mi->pubkey,
start,
llabs (delta),
delta < 0 ? GNUNET_YES : GNUNET_NO,
+ ascending,
&pd_cb,
&pcc);
if ( (0 > qs) ||
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c
index 5fd07d0a..beeeb4eb 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -434,6 +434,22 @@ postgres_initialize (void *cls)
" WHERE merchant_pub=$1"
" AND h_contract_terms=$2",
2),
+
+ GNUNET_PQ_make_prepare ("find_contract_terms_by_date_and_range_asc",
+ "SELECT"
+ " contract_terms"
+ ",order_id"
+ ",row_id"
+ " FROM merchant_contract_terms"
+ " WHERE"
+ " timestamp>$1"
+ " AND merchant_pub=$2"
+ " AND row_id>$3"
+ " AND paid=TRUE"
+ " ORDER BY row_id ASC, timestamp ASC"
+ " LIMIT $4",
+ 4),
+
GNUNET_PQ_make_prepare ("find_contract_terms_by_date_and_range",
"SELECT"
" contract_terms"
@@ -448,6 +464,21 @@ postgres_initialize (void *cls)
" ORDER BY row_id DESC, timestamp DESC"
" LIMIT $4",
4),
+
+ GNUNET_PQ_make_prepare ("find_contract_terms_by_date_and_range_past_asc",
+ "SELECT"
+ " contract_terms"
+ ",order_id"
+ ",row_id"
+ " FROM merchant_contract_terms"
+ " WHERE"
+ " timestamp<$1"
+ " AND merchant_pub=$2"
+ " AND row_id<$3"
+ " AND paid=TRUE"
+ " ORDER BY row_id ASC, timestamp ASC"
+ " LIMIT $4",
+ 4),
GNUNET_PQ_make_prepare ("find_contract_terms_by_date_and_range_past",
"SELECT"
" contract_terms"
@@ -1365,6 +1396,7 @@ find_contracts_cb (void *cls,
* the same timestamp and just go behind in history by tuning `start`.
* @param nrows only nrows rows are returned.
* @param past if set to #GNUNET_YES, retrieves rows older than `date`.
+ * @param ascending if GNUNET_YES, results will be sorted in chronological order.
* This is tipically used to show live updates on the merchant's backoffice
* Web interface.
* @param cb function to call with transaction data, can be NULL.
@@ -1378,6 +1410,7 @@ postgres_find_contract_terms_by_date_and_range (void *cls,
uint64_t start,
uint64_t nrows,
int past,
+ unsigned int ascending,
TALER_MERCHANTDB_ProposalDataCallback cb,
void *cb_cls)
{
@@ -1398,10 +1431,15 @@ postgres_find_contract_terms_by_date_and_range (void *cls,
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"DB serving /history with date %s\n",
GNUNET_STRINGS_absolute_time_to_string (date));
- if (GNUNET_YES == past)
+ if ((GNUNET_YES == past) && (GNUNET_YES == ascending))
+ stmt = "find_contract_terms_by_date_and_range_past_asc";
+ else if ((GNUNET_YES == past) && (GNUNET_NO == ascending))
stmt = "find_contract_terms_by_date_and_range_past";
- else
+
+ else if ((GNUNET_NO == past) && (GNUNET_NO == ascending))
stmt = "find_contract_terms_by_date_and_range";
+ else
+ stmt = "find_contract_terms_by_date_and_range_asc";
check_connection (pg);
qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
stmt,
diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c
index d60d1e9f..aa3db96a 100644
--- a/src/backenddb/test_merchantdb.c
+++ b/src/backenddb/test_merchantdb.c
@@ -905,6 +905,7 @@ run (void *cls)
2,
1,
GNUNET_YES,
+ GNUNET_NO,
&pd_cb,
NULL));
timestamp = GNUNET_TIME_absolute_get ();
@@ -935,6 +936,7 @@ run (void *cls)
0,
5,
GNUNET_NO,
+ GNUNET_NO,
&pd_cb,
NULL));
diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h
index 7791582d..f8a95019 100644
--- a/src/include/taler_merchantdb_plugin.h
+++ b/src/include/taler_merchantdb_plugin.h
@@ -321,6 +321,7 @@ struct TALER_MERCHANTDB_Plugin
* @param start only rows with serial id less than start are returned.
* @param nrows only nrows rows are returned.
* @param past if set to #GNUNET_YES, retrieves rows older than `date`.
+ * @param ascending if GNUNET_YES, then results will be sorted with youngest first.
* This is typically used to show live updates on the merchant's backoffice
* @param cb function to call with transaction data, can be NULL.
* @param cb_cls closure for @a cb
@@ -333,6 +334,7 @@ struct TALER_MERCHANTDB_Plugin
uint64_t start,
uint64_t nrows,
int past,
+ unsigned int ascending,
TALER_MERCHANTDB_ProposalDataCallback cb,
void *cb_cls);