aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-01-26 11:04:46 +0100
committerChristian Grothoff <christian@grothoff.org>2015-01-26 11:04:46 +0100
commitd4506f8a041385f7695b04b1ddfacb894d05da5c (patch)
treea4a0c87cbe822427e214e803a94a5a42551e9511
parentc2b32e75dc84814c5f7a03e54c4d18213119b4b4 (diff)
downloadexchange-d4506f8a041385f7695b04b1ddfacb894d05da5c.tar.gz
exchange-d4506f8a041385f7695b04b1ddfacb894d05da5c.zip
support variable-size results
-rw-r--r--src/include/taler_db_lib.h90
-rw-r--r--src/util/db.c57
2 files changed, 111 insertions, 36 deletions
diff --git a/src/include/taler_db_lib.h b/src/include/taler_db_lib.h
index 41b46264e..6e2b2b2c0 100644
--- a/src/include/taler_db_lib.h
+++ b/src/include/taler_db_lib.h
@@ -13,13 +13,12 @@
13 You should have received a copy of the GNU General Public License along with 13 You should have received a copy of the GNU General Public License along with
14 TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> 14 TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
15*/ 15*/
16
17
18/** 16/**
19 * @file include/taler_db_lib.h 17 * @file include/taler_db_lib.h
20 * @brief helper functions for DB interactions 18 * @brief helper functions for DB interactions
21 * @author Sree Harsha Totakura <sreeharsha@totakura.in> 19 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
22 * @author Florian Dold 20 * @author Florian Dold
21 * @author Christian Grothoff
23 */ 22 */
24 23
25#ifndef TALER_DB_LIB_H_ 24#ifndef TALER_DB_LIB_H_
@@ -28,16 +27,6 @@
28#include <libpq-fe.h> 27#include <libpq-fe.h>
29#include "taler_util.h" 28#include "taler_util.h"
30 29
31#define TALER_DB_QUERY_PARAM_END { NULL, 0, 0 }
32#define TALER_DB_QUERY_PARAM_PTR(x) { (x), sizeof (*(x)), 1 }
33#define TALER_DB_QUERY_PARAM_PTR_SIZED(x, s) { (x), (s), 1 }
34
35
36#define TALER_DB_RESULT_SPEC_END { NULL, 0, NULL }
37#define TALER_DB_RESULT_SPEC(name, dst) { (void *) (dst), sizeof (*(dst)), (name) }
38#define TALER_DB_RESULT_SPEC_SIZED(name, dst, s) { (void *) (dst), (s), (name) }
39
40
41/** 30/**
42 * Description of a DB query parameter. 31 * Description of a DB query parameter.
43 */ 32 */
@@ -47,10 +36,12 @@ struct TALER_DB_QueryParam
47 * Data or NULL 36 * Data or NULL
48 */ 37 */
49 const void *data; 38 const void *data;
39
50 /** 40 /**
51 * Size of 'data' 41 * Size of @e data
52 */ 42 */
53 size_t size; 43 size_t size;
44
54 /** 45 /**
55 * Non-null if this is not the last parameter. 46 * Non-null if this is not the last parameter.
56 * This allows for null as sentinal value. 47 * This allows for null as sentinal value.
@@ -58,6 +49,27 @@ struct TALER_DB_QueryParam
58 int more; 49 int more;
59}; 50};
60 51
52/**
53 * End of query parameter specification.
54 */
55#define TALER_DB_QUERY_PARAM_END { NULL, 0, 0 }
56
57/**
58 * Generate fixed-size query parameter with size given explicitly.
59 *
60 * @param x pointer to the query parameter to pass
61 * @param s number of bytes of @a x to use for the query
62 */
63#define TALER_DB_QUERY_PARAM_PTR_SIZED(x, s) { (x), (s), 1 }
64
65/**
66 * Generate fixed-size query parameter with size determined
67 * by variable type.
68 *
69 * @param x pointer to the query parameter to pass.
70 */
71#define TALER_DB_QUERY_PARAM_PTR(x) TALER_DB_QUERY_PARAM_PTR_SIZED(x, sizeof (*(x)))
72
61 73
62/** 74/**
63 * Description of a DB result cell. 75 * Description of a DB result cell.
@@ -70,7 +82,9 @@ struct TALER_DB_ResultSpec
70 void *dst; 82 void *dst;
71 83
72 /** 84 /**
73 * Allowed size for the data. 85 * Allowed size for the data, 0 for variable-size
86 * (in this case, the type of @e dst is a `void **`
87 * and we need to allocate a buffer of the right size).
74 */ 88 */
75 size_t dst_size; 89 size_t dst_size;
76 90
@@ -78,10 +92,48 @@ struct TALER_DB_ResultSpec
78 * Field name of the desired result. 92 * Field name of the desired result.
79 */ 93 */
80 char *fname; 94 char *fname;
95
96 /**
97 * Actual size of the result.
98 */
99 size_t *result_size;
100
81}; 101};
82 102
83 103
84/** 104/**
105 * End of result parameter specification.
106 */
107#define TALER_DB_RESULT_SPEC_END { NULL, 0, NULL, NULL }
108
109/**
110 * We expect a fixed-size result, with size given explicitly
111 *
112 * @param name name of the field in the table
113 * @param dst point to where to store the result
114 * @param s number of bytes we should use in @a dst
115 */
116#define TALER_DB_RESULT_SPEC_SIZED(name, dst, s) { (void *) (dst), (s), (name), NULL }
117
118/**
119 * We expect a fixed-size result, with size determined by the type of `* dst`
120 *
121 * @param name name of the field in the table
122 * @param dst point to where to store the result, type fits expected result size
123 */
124#define TALER_DB_RESULT_SPEC(name, dst) TALER_DB_RESULT_SPEC_SIZED(name, dst, sizeof (*(dst)))
125
126/**
127 * Variable-size result expected.
128 *
129 * @param name name of the field in the table
130 * @param dst where to store the result (of type void **), to be allocated
131 * @param sptr pointer to a `size_t` for where to store the size of @a dst
132 */
133#define TALER_DB_RESULT_SPEC_VAR(name, dst, sptr) { (void *) (dst), 0, (name), sptr }
134
135
136/**
85 * Execute a prepared statement. 137 * Execute a prepared statement.
86 */ 138 */
87PGresult * 139PGresult *
@@ -96,12 +148,14 @@ TALER_DB_exec_prepared (PGconn *db_conn,
96 * is returned. 148 * is returned.
97 * 149 *
98 * @return 150 * @return
99 * GNUNET_YES if all results could be extracted 151 * #GNUNET_YES if all results could be extracted
100 * GNUNET_NO if at least one result was NULL 152 * #GNUNET_NO if at least one result was NULL
101 * GNUNET_SYSERR if a result was invalid (non-existing field) 153 * #GNUNET_SYSERR if a result was invalid (non-existing field)
102 */ 154 */
103int 155int
104TALER_DB_extract_result (PGresult *result, struct TALER_DB_ResultSpec *rs, int row); 156TALER_DB_extract_result (PGresult *result,
157 struct TALER_DB_ResultSpec *rs,
158 int row);
105 159
106 160
107int 161int
diff --git a/src/util/db.c b/src/util/db.c
index a0b234a06..c048a30ff 100644
--- a/src/util/db.c
+++ b/src/util/db.c
@@ -13,15 +13,13 @@
13 You should have received a copy of the GNU General Public License along with 13 You should have received a copy of the GNU General Public License along with
14 TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> 14 TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
15*/ 15*/
16
17
18/** 16/**
19 * @file util/db.c 17 * @file util/db.c
20 * @brief helper functions for DB interactions 18 * @brief helper functions for DB interactions
21 * @author Sree Harsha Totakura <sreeharsha@totakura.in> 19 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
22 * @author Florian Dold 20 * @author Florian Dold
21 * @author Christian Grothoff
23 */ 22 */
24
25#include "platform.h" 23#include "platform.h"
26#include <gnunet/gnunet_util_lib.h> 24#include <gnunet/gnunet_util_lib.h>
27#include "taler_db_lib.h" 25#include "taler_db_lib.h"
@@ -39,12 +37,11 @@ TALER_DB_exec_prepared (PGconn *db_conn,
39 unsigned i; 37 unsigned i;
40 38
41 /* count the number of parameters */ 39 /* count the number of parameters */
42
43 { 40 {
44 const struct TALER_DB_QueryParam *x; 41 const struct TALER_DB_QueryParam *x;
45 for (len = 0, x = params; 42 for (len = 0, x = params;
46 x->more; 43 x->more;
47 len +=1, x += 1); 44 len++, x++);
48 } 45 }
49 46
50 /* new scope to allow stack allocation without alloca */ 47 /* new scope to allow stack allocation without alloca */
@@ -61,20 +58,22 @@ TALER_DB_exec_prepared (PGconn *db_conn,
61 param_formats[i] = 1; 58 param_formats[i] = 1;
62 } 59 }
63 return PQexecPrepared (db_conn, name, len, 60 return PQexecPrepared (db_conn, name, len,
64 (const char **) param_values, param_lengths, param_formats, 1); 61 (const char **) param_values,
62 param_lengths,
63 param_formats, 1);
65 } 64 }
66} 65}
67 66
68 67
69/** 68/**
70 * Extract results from a query result according to the given specification. 69 * Extract results from a query result according to the given specification.
71 * If colums are NULL, the destination is not modified, and GNUNET_NO 70 * If colums are NULL, the destination is not modified, and #GNUNET_NO
72 * is returned. 71 * is returned.
73 * 72 *
74 * @return 73 * @return
75 * GNUNET_YES if all results could be extracted 74 * #GNUNET_YES if all results could be extracted
76 * GNUNET_NO if at least one result was NULL 75 * #GNUNET_NO if at least one result was NULL
77 * GNUNET_SYSERR if a result was invalid (non-existing field) 76 * #GNUNET_SYSERR if a result was invalid (non-existing field)
78 */ 77 */
79int 78int
80TALER_DB_extract_result (PGresult *result, 79TALER_DB_extract_result (PGresult *result,
@@ -82,35 +81,56 @@ TALER_DB_extract_result (PGresult *result,
82 int row) 81 int row)
83{ 82{
84 int had_null = GNUNET_NO; 83 int had_null = GNUNET_NO;
84 size_t len;
85 unsigned int i;
86 unsigned int j;
85 87
86 for (; NULL != rs->fname; rs += 1) 88 for (i=0; NULL != rs[i].fname; i++)
87 { 89 {
88 int fnum; 90 int fnum;
89 fnum = PQfnumber (result, rs->fname); 91
92 fnum = PQfnumber (result, rs[i].fname);
90 if (fnum < 0) 93 if (fnum < 0)
91 { 94 {
92 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "field '%s' does not exist in result\n", rs->fname); 95 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
96 "field '%s' does not exist in result\n",
97 rs->fname);
93 return GNUNET_SYSERR; 98 return GNUNET_SYSERR;
94 } 99 }
95 100
96 /* if a field is null, continue but 101 /* if a field is null, continue but
97 * remember that we now return a different result */ 102 * remember that we now return a different result */
98
99 if (PQgetisnull (result, row, fnum)) 103 if (PQgetisnull (result, row, fnum))
100 { 104 {
101 had_null = GNUNET_YES; 105 had_null = GNUNET_YES;
102 continue; 106 continue;
103 } 107 }
104 const char *res; 108 const char *res;
105 if (rs->dst_size != PQgetlength (result, row, fnum)) 109 len = PQgetlength (result, row, fnum);
110 if ( (0 != rs[i].dst_size) &&
111 (rs[i].dst_size != len) )
106 { 112 {
107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "field '%s' has wrong size (got %u, expected %u)\n", 113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
108 rs->fname, (int) PQgetlength (result, row, fnum), (int) rs->dst_size); 114 "field '%s' has wrong size (got %d, expected %d)\n",
115 rs[i].fname,
116 (int) len,
117 (int) rs->dst_size);
118 for (j=0;j<i;j++)
119 if (0 == rs[i].dst_size)
120 {
121 GNUNET_free (rs[i].dst);
122 rs[i].dst = NULL;
123 *rs[i].result_size = 0;
124 }
109 return GNUNET_SYSERR; 125 return GNUNET_SYSERR;
110 } 126 }
111 res = PQgetvalue (result, row, fnum); 127 res = PQgetvalue (result, row, fnum);
112 GNUNET_assert (NULL != res); 128 GNUNET_assert (NULL != res);
113 memcpy (rs->dst, res, rs->dst_size); 129 if (0 == rs->dst_size)
130 *(void**) rs->dst = GNUNET_malloc (*rs->result_size = len);
131 memcpy (rs->dst,
132 res,
133 len);
114 } 134 }
115 if (GNUNET_YES == had_null) 135 if (GNUNET_YES == had_null)
116 return GNUNET_NO; 136 return GNUNET_NO;
@@ -124,6 +144,7 @@ TALER_DB_field_isnull (PGresult *result,
124 const char *fname) 144 const char *fname)
125{ 145{
126 int fnum; 146 int fnum;
147
127 fnum = PQfnumber (result, fname); 148 fnum = PQfnumber (result, fname);
128 GNUNET_assert (fnum >= 0); 149 GNUNET_assert (fnum >= 0);
129 if (PQgetisnull (result, row, fnum)) 150 if (PQgetisnull (result, row, fnum))