summaryrefslogtreecommitdiff
path: root/src/pq
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-08-17 21:35:21 +0200
committerChristian Grothoff <christian@grothoff.org>2019-08-17 21:35:21 +0200
commit4bf425c902c2047e766e0333e54188ee67a673f5 (patch)
tree8a8e1311a22b8cfafd475eded311b103834451de /src/pq
parentcbf6281ca2c4ee0fd77eda4a6d943e3a60a21e0e (diff)
downloadexchange-4bf425c902c2047e766e0333e54188ee67a673f5.tar.gz
exchange-4bf425c902c2047e766e0333e54188ee67a673f5.tar.bz2
exchange-4bf425c902c2047e766e0333e54188ee67a673f5.zip
remove currency field from databases
Diffstat (limited to 'src/pq')
-rw-r--r--src/pq/pq_query_helper.c76
-rw-r--r--src/pq/pq_result_helper.c229
-rw-r--r--src/pq/test_pq.c44
3 files changed, 165 insertions, 184 deletions
diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c
index 6386749c..1a03c2d4 100644
--- a/src/pq/pq_query_helper.c
+++ b/src/pq/pq_query_helper.c
@@ -42,19 +42,19 @@
*/
static int
qconv_amount_nbo (void *cls,
- const void *data,
- size_t data_len,
- void *param_values[],
- int param_lengths[],
- int param_formats[],
- unsigned int param_length,
- void *scratch[],
- unsigned int scratch_length)
+ const void *data,
+ size_t data_len,
+ void *param_values[],
+ int param_lengths[],
+ int param_formats[],
+ unsigned int param_length,
+ void *scratch[],
+ unsigned int scratch_length)
{
const struct TALER_AmountNBO *amount = data;
unsigned int off = 0;
- GNUNET_assert (3 == param_length);
+ GNUNET_assert (2 == param_length);
param_values[off] = (void *) &amount->value;
param_lengths[off] = sizeof (amount->value);
param_formats[off] = 1;
@@ -62,10 +62,6 @@ qconv_amount_nbo (void *cls,
param_values[off] = (void *) &amount->fraction;
param_lengths[off] = sizeof (amount->fraction);
param_formats[off] = 1;
- off++;
- param_values[off] = (void *) amount->currency;
- param_lengths[off] = strlen (amount->currency);
- param_formats[off] = 1;
return 0;
}
@@ -83,7 +79,7 @@ struct GNUNET_PQ_QueryParam
TALER_PQ_query_param_amount_nbo (const struct TALER_AmountNBO *x)
{
struct GNUNET_PQ_QueryParam res =
- { &qconv_amount_nbo, NULL, x, sizeof (*x), 3 };
+ { &qconv_amount_nbo, NULL, x, sizeof (*x), 2 };
return res;
}
@@ -104,14 +100,14 @@ TALER_PQ_query_param_amount_nbo (const struct TALER_AmountNBO *x)
*/
static int
qconv_amount (void *cls,
- const void *data,
- size_t data_len,
- void *param_values[],
- int param_lengths[],
- int param_formats[],
- unsigned int param_length,
- void *scratch[],
- unsigned int scratch_length)
+ const void *data,
+ size_t data_len,
+ void *param_values[],
+ int param_lengths[],
+ int param_formats[],
+ unsigned int param_length,
+ void *scratch[],
+ unsigned int scratch_length)
{
const struct TALER_Amount *amount_hbo = data;
struct TALER_AmountNBO *amount;
@@ -119,16 +115,16 @@ qconv_amount (void *cls,
amount = GNUNET_new (struct TALER_AmountNBO);
scratch[0] = amount;
TALER_amount_hton (amount,
- amount_hbo);
+ amount_hbo);
qconv_amount_nbo (cls,
- amount,
- sizeof (struct TALER_AmountNBO),
- param_values,
- param_lengths,
- param_formats,
- param_length,
- &scratch[1],
- scratch_length - 1);
+ amount,
+ sizeof (struct TALER_AmountNBO),
+ param_values,
+ param_lengths,
+ param_formats,
+ param_length,
+ &scratch[1],
+ scratch_length - 1);
return 1;
}
@@ -146,7 +142,7 @@ struct GNUNET_PQ_QueryParam
TALER_PQ_query_param_amount (const struct TALER_Amount *x)
{
struct GNUNET_PQ_QueryParam res =
- { &qconv_amount, NULL, x, sizeof (*x), 3 };
+ { &qconv_amount, NULL, x, sizeof (*x), 2 };
return res;
}
@@ -167,14 +163,14 @@ TALER_PQ_query_param_amount (const struct TALER_Amount *x)
*/
static int
qconv_json (void *cls,
- const void *data,
- size_t data_len,
- void *param_values[],
- int param_lengths[],
- int param_formats[],
- unsigned int param_length,
- void *scratch[],
- unsigned int scratch_length)
+ const void *data,
+ size_t data_len,
+ void *param_values[],
+ int param_lengths[],
+ int param_formats[],
+ unsigned int param_length,
+ void *scratch[],
+ unsigned int scratch_length)
{
const json_t *json = data;
char *str;
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c
index c1395b7c..92b8e8f4 100644
--- a/src/pq/pq_result_helper.c
+++ b/src/pq/pq_result_helper.c
@@ -40,15 +40,14 @@
*/
static int
extract_amount_nbo_helper (PGresult *result,
- int row,
- const char *val_name,
- const char *frac_name,
- const char *curr_name,
- struct TALER_AmountNBO *r_amount_nbo)
+ int row,
+ const char *currency,
+ const char *val_name,
+ const char *frac_name,
+ struct TALER_AmountNBO *r_amount_nbo)
{
int val_num;
int frac_num;
- int curr_num;
int len;
/* These checks are simply to check that clients obey by our naming
@@ -59,9 +58,6 @@ extract_amount_nbo_helper (PGresult *result,
GNUNET_assert (NULL !=
strstr (frac_name,
"_frac"));
- GNUNET_assert (NULL !=
- strstr (curr_name,
- "_curr"));
/* Set return value to invalid in case we don't finish */
memset (r_amount_nbo,
0,
@@ -70,27 +66,18 @@ extract_amount_nbo_helper (PGresult *result,
val_name);
frac_num = PQfnumber (result,
frac_name);
- curr_num = PQfnumber (result,
- curr_name);
if (val_num < 0)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Field `%s' does not exist in result\n",
- val_name);
+ "Field `%s' does not exist in result\n",
+ val_name);
return GNUNET_SYSERR;
}
if (frac_num < 0)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Field `%s' does not exist in result\n",
- frac_name);
- return GNUNET_SYSERR;
- }
- if (curr_num < 0)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Field `%s' does not exist in result\n",
- curr_name);
+ "Field `%s' does not exist in result\n",
+ frac_name);
return GNUNET_SYSERR;
}
if ( (PQgetisnull (result,
@@ -98,10 +85,7 @@ extract_amount_nbo_helper (PGresult *result,
val_num)) ||
(PQgetisnull (result,
row,
- frac_num)) ||
- (PQgetisnull (result,
- row,
- curr_num)) )
+ frac_num)) )
{
GNUNET_break (0);
return GNUNET_NO;
@@ -115,13 +99,9 @@ extract_amount_nbo_helper (PGresult *result,
row,
frac_num);
len = GNUNET_MIN (TALER_CURRENCY_LEN - 1,
- PQgetlength (result,
- row,
- curr_num));
+ strlen (currency));
memcpy (r_amount_nbo->currency,
- PQgetvalue (result,
- row,
- curr_num),
+ currency,
len);
return GNUNET_OK;
}
@@ -130,7 +110,7 @@ extract_amount_nbo_helper (PGresult *result,
/**
* Extract data from a Postgres database @a result at row @a row.
*
- * @param cls closure
+ * @param cls closure, a `const char *` giving the currency
* @param result where to extract data from
* @param row row to extract data from
* @param fname name (or prefix) of the fields to extract from
@@ -143,35 +123,31 @@ extract_amount_nbo_helper (PGresult *result,
*/
static int
extract_amount_nbo (void *cls,
- PGresult *result,
- int row,
- const char *fname,
- size_t *dst_size,
- void *dst)
+ PGresult *result,
+ int row,
+ const char *fname,
+ size_t *dst_size,
+ void *dst)
{
+ const char *currency = cls;
char *val_name;
char *frac_name;
- char *curr_name;
int ret;
GNUNET_asprintf (&val_name,
- "%s_val",
- fname);
+ "%s_val",
+ fname);
GNUNET_asprintf (&frac_name,
- "%s_frac",
- fname);
- GNUNET_asprintf (&curr_name,
- "%s_curr",
- fname);
+ "%s_frac",
+ fname);
ret = extract_amount_nbo_helper (result,
- row,
- val_name,
- frac_name,
- curr_name,
- dst);
+ row,
+ currency,
+ val_name,
+ frac_name,
+ dst);
GNUNET_free (val_name);
GNUNET_free (frac_name);
- GNUNET_free (curr_name);
return ret;
}
@@ -185,12 +161,17 @@ extract_amount_nbo (void *cls,
*/
struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_amount_nbo (const char *name,
- struct TALER_AmountNBO *amount)
+ const char *currency,
+ struct TALER_AmountNBO *amount)
{
- struct GNUNET_PQ_ResultSpec res =
- { &extract_amount_nbo, NULL, NULL,
- (void *) amount, sizeof (*amount),
- name, NULL };
+ struct GNUNET_PQ_ResultSpec res = {
+ .conv = &extract_amount_nbo,
+ .cls = (void *) currency,
+ .dst = (void *) amount,
+ .dst_size = sizeof (*amount),
+ .fname = name
+ };
+
return res;
}
@@ -198,7 +179,7 @@ TALER_PQ_result_spec_amount_nbo (const char *name,
/**
* Extract data from a Postgres database @a result at row @a row.
*
- * @param cls closure
+ * @param cls closure, a `const char *` giving the currency
* @param result where to extract data from
* @param row row to extract data from
* @param fname name (or prefix) of the fields to extract from
@@ -211,39 +192,35 @@ TALER_PQ_result_spec_amount_nbo (const char *name,
*/
static int
extract_amount (void *cls,
- PGresult *result,
- int row,
- const char *fname,
- size_t *dst_size,
- void *dst)
+ PGresult *result,
+ int row,
+ const char *fname,
+ size_t *dst_size,
+ void *dst)
{
+ const char *currency = cls;
struct TALER_Amount *r_amount = dst;
char *val_name;
char *frac_name;
- char *curr_name;
struct TALER_AmountNBO amount_nbo;
int ret;
GNUNET_asprintf (&val_name,
- "%s_val",
- fname);
+ "%s_val",
+ fname);
GNUNET_asprintf (&frac_name,
- "%s_frac",
- fname);
- GNUNET_asprintf (&curr_name,
- "%s_curr",
- fname);
+ "%s_frac",
+ fname);
ret = extract_amount_nbo_helper (result,
- row,
- val_name,
- frac_name,
- curr_name,
- &amount_nbo);
+ row,
+ currency,
+ val_name,
+ frac_name,
+ &amount_nbo);
TALER_amount_ntoh (r_amount,
&amount_nbo);
GNUNET_free (val_name);
GNUNET_free (frac_name);
- GNUNET_free (curr_name);
return ret;
}
@@ -252,17 +229,23 @@ extract_amount (void *cls,
* Currency amount expected.
*
* @param name name of the field in the table
+ * @param currency the currency the amount is in
* @param[out] amount where to store the result
* @return array entry for the result specification to use
*/
struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_amount (const char *name,
- struct TALER_Amount *amount)
+ const char *currency,
+ struct TALER_Amount *amount)
{
- struct GNUNET_PQ_ResultSpec res =
- { &extract_amount, NULL, NULL,
- (void *) amount, sizeof (*amount),
- name, NULL };
+ struct GNUNET_PQ_ResultSpec res = {
+ .conv = &extract_amount,
+ .cls = (void *) currency,
+ .dst = (void *) amount,
+ .dst_size = sizeof (*amount),
+ .fname = name
+ };
+
return res;
}
@@ -283,11 +266,11 @@ TALER_PQ_result_spec_amount (const char *name,
*/
static int
extract_json (void *cls,
- PGresult *result,
- int row,
- const char *fname,
- size_t *dst_size,
- void *dst)
+ PGresult *result,
+ int row,
+ const char *fname,
+ size_t *dst_size,
+ void *dst)
{
json_t **j_dst = dst;
const char *res;
@@ -296,33 +279,33 @@ extract_json (void *cls,
size_t slen;
fnum = PQfnumber (result,
- fname);
+ fname);
if (fnum < 0)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Field `%s' does not exist in result\n",
- fname);
+ "Field `%s' does not exist in result\n",
+ fname);
return GNUNET_SYSERR;
}
if (PQgetisnull (result,
- row,
- fnum))
+ row,
+ fnum))
return GNUNET_NO;
slen = PQgetlength (result,
- row,
- fnum);
+ row,
+ fnum);
res = (const char *) PQgetvalue (result,
- row,
- fnum);
+ row,
+ fnum);
*j_dst = json_loadb (res,
- slen,
- JSON_REJECT_DUPLICATES,
- &json_error);
+ slen,
+ JSON_REJECT_DUPLICATES,
+ &json_error);
if (NULL == *j_dst)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Failed to parse JSON result for field `%s': %s (%s)\n",
- fname,
+ "Failed to parse JSON result for field `%s': %s (%s)\n",
+ fname,
json_error.text,
json_error.source);
return GNUNET_SYSERR;
@@ -340,7 +323,7 @@ extract_json (void *cls,
*/
static void
clean_json (void *cls,
- void *rd)
+ void *rd)
{
json_t **dst = rd;
@@ -363,15 +346,17 @@ struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_json (const char *name,
json_t **jp)
{
- struct GNUNET_PQ_ResultSpec res =
- { &extract_json, &clean_json, NULL,
- (void *) jp, 0,
- name, NULL };
+ struct GNUNET_PQ_ResultSpec res = {
+ .conv = &extract_json,
+ .cleaner = &clean_json,
+ .dst = (void *) jp,
+ .fname = name
+ };
+
return res;
}
-
/**
* Extract data from a Postgres database @a result at row @a row.
*
@@ -399,7 +384,7 @@ extract_round_time (void *cls,
int fnum;
fnum = PQfnumber (result,
- fname);
+ fname);
if (fnum < 0)
{
GNUNET_break (0);
@@ -443,10 +428,13 @@ struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_absolute_time (const char *name,
struct GNUNET_TIME_Absolute *at)
{
- struct GNUNET_PQ_ResultSpec res =
- { &extract_round_time, NULL, NULL,
- (void *) at, sizeof (struct GNUNET_TIME_Absolute),
- name, NULL };
+ struct GNUNET_PQ_ResultSpec res = {
+ .conv = &extract_round_time,
+ .dst = (void *) at,
+ .dst_size = sizeof (struct GNUNET_TIME_Absolute),
+ .fname = name
+ };
+
return res;
}
@@ -478,15 +466,15 @@ extract_round_time_nbo (void *cls,
int fnum;
fnum = PQfnumber (result,
- fname);
+ fname);
if (fnum < 0)
{
GNUNET_break (0);
return GNUNET_SYSERR;
}
if (PQgetisnull (result,
- row,
- fnum))
+ row,
+ fnum))
{
GNUNET_break (0);
return GNUNET_SYSERR;
@@ -522,10 +510,13 @@ struct GNUNET_PQ_ResultSpec
TALER_PQ_result_spec_absolute_time_nbo (const char *name,
struct GNUNET_TIME_AbsoluteNBO *at)
{
- struct GNUNET_PQ_ResultSpec res =
- { &extract_round_time_nbo, NULL, NULL,
- (void *) at, sizeof (struct GNUNET_TIME_AbsoluteNBO),
- name, NULL };
+ struct GNUNET_PQ_ResultSpec res = {
+ .conv = &extract_round_time_nbo,
+ .dst = (void *) at,
+ .dst_size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
+ .fname = name
+ };
+
return res;
}
diff --git a/src/pq/test_pq.c b/src/pq/test_pq.c
index 6f7de0db..471326f0 100644
--- a/src/pq/test_pq.c
+++ b/src/pq/test_pq.c
@@ -50,23 +50,18 @@ postgres_prepare (PGconn *db_conn)
"INSERT INTO test_pq ("
" hamount_val"
",hamount_frac"
- ",hamount_curr"
",namount_val"
",namount_frac"
- ",namount_curr"
",json"
") VALUES "
- "($1, $2, $3, $4, $5, $6,"
- "$7);",
- 7, NULL);
+ "($1, $2, $3, $4, $5);",
+ 5, NULL);
PREPARE ("test_select",
"SELECT"
" hamount_val"
",hamount_frac"
- ",hamount_curr"
",namount_val"
",namount_frac"
- ",namount_curr"
",json"
" FROM test_pq;",
0, NULL);
@@ -113,8 +108,8 @@ run_queries (PGconn *conn)
};
result = GNUNET_PQ_exec_prepared (conn,
- "test_insert",
- params_insert);
+ "test_insert",
+ params_insert);
if (PGRES_COMMAND_OK != PQresultStatus (result))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -131,10 +126,10 @@ run_queries (PGconn *conn)
};
result = GNUNET_PQ_exec_prepared (conn,
- "test_select",
- params_select);
+ "test_select",
+ params_select);
if (1 !=
- PQntuples (result))
+ PQntuples (result))
{
GNUNET_break (0);
PQclear (result);
@@ -144,8 +139,8 @@ run_queries (PGconn *conn)
{
struct GNUNET_PQ_ResultSpec results_select[] = {
- TALER_PQ_result_spec_amount ("hamount", &hamount2),
- TALER_PQ_result_spec_amount_nbo ("namount", &namount2),
+ TALER_PQ_result_spec_amount ("hamount", "EUR", &hamount2),
+ TALER_PQ_result_spec_amount_nbo ("namount", "EUR", &namount2),
TALER_PQ_result_spec_json ("json", &json2),
GNUNET_PQ_result_spec_end
};
@@ -163,8 +158,9 @@ run_queries (PGconn *conn)
&namount2);
GNUNET_break (0 ==
TALER_amount_cmp (&hamount,
- &hamount2));
- GNUNET_break (42 == json_integer_value (json_object_get (json2, "foo")));
+ &hamount2));
+ GNUNET_break (42 ==
+ json_integer_value (json_object_get (json2, "foo")));
GNUNET_PQ_cleanup_result (results_select);
PQclear (result);
}
@@ -185,8 +181,8 @@ main(int argc,
int ret;
GNUNET_log_setup ("test-pq",
- "WARNING",
- NULL);
+ "WARNING",
+ NULL);
conn = PQconnectdb ("postgres:///talercheck");
if (CONNECTION_OK != PQstatus (conn))
{
@@ -202,17 +198,15 @@ main(int argc,
"CREATE TEMPORARY TABLE IF NOT EXISTS test_pq ("
" hamount_val INT8 NOT NULL"
",hamount_frac INT4 NOT NULL"
- ",hamount_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL"
",namount_val INT8 NOT NULL"
",namount_frac INT4 NOT NULL"
- ",namount_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL"
",json VARCHAR NOT NULL"
")");
if (PGRES_COMMAND_OK != PQresultStatus (result))
{
fprintf (stderr,
- "Failed to create table: %s\n",
- PQerrorMessage (conn));
+ "Failed to create table: %s\n",
+ PQerrorMessage (conn));
PQclear (result);
PQfinish (conn);
return 1;
@@ -227,12 +221,12 @@ main(int argc,
}
ret = run_queries (conn);
result = PQexec (conn,
- "DROP TABLE test_pq");
+ "DROP TABLE test_pq");
if (PGRES_COMMAND_OK != PQresultStatus (result))
{
fprintf (stderr,
- "Failed to create table: %s\n",
- PQerrorMessage (conn));
+ "Failed to create table: %s\n",
+ PQerrorMessage (conn));
PQclear (result);
PQfinish (conn);
return 1;