support variable-size results
This commit is contained in:
parent
c2b32e75dc
commit
d4506f8a04
@ -13,13 +13,12 @@
|
|||||||
You should have received a copy of the GNU General Public License along with
|
You should have received a copy of the GNU General Public License along with
|
||||||
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
|
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file include/taler_db_lib.h
|
* @file include/taler_db_lib.h
|
||||||
* @brief helper functions for DB interactions
|
* @brief helper functions for DB interactions
|
||||||
* @author Sree Harsha Totakura <sreeharsha@totakura.in>
|
* @author Sree Harsha Totakura <sreeharsha@totakura.in>
|
||||||
* @author Florian Dold
|
* @author Florian Dold
|
||||||
|
* @author Christian Grothoff
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef TALER_DB_LIB_H_
|
#ifndef TALER_DB_LIB_H_
|
||||||
@ -28,16 +27,6 @@
|
|||||||
#include <libpq-fe.h>
|
#include <libpq-fe.h>
|
||||||
#include "taler_util.h"
|
#include "taler_util.h"
|
||||||
|
|
||||||
#define TALER_DB_QUERY_PARAM_END { NULL, 0, 0 }
|
|
||||||
#define TALER_DB_QUERY_PARAM_PTR(x) { (x), sizeof (*(x)), 1 }
|
|
||||||
#define TALER_DB_QUERY_PARAM_PTR_SIZED(x, s) { (x), (s), 1 }
|
|
||||||
|
|
||||||
|
|
||||||
#define TALER_DB_RESULT_SPEC_END { NULL, 0, NULL }
|
|
||||||
#define TALER_DB_RESULT_SPEC(name, dst) { (void *) (dst), sizeof (*(dst)), (name) }
|
|
||||||
#define TALER_DB_RESULT_SPEC_SIZED(name, dst, s) { (void *) (dst), (s), (name) }
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description of a DB query parameter.
|
* Description of a DB query parameter.
|
||||||
*/
|
*/
|
||||||
@ -47,10 +36,12 @@ struct TALER_DB_QueryParam
|
|||||||
* Data or NULL
|
* Data or NULL
|
||||||
*/
|
*/
|
||||||
const void *data;
|
const void *data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Size of 'data'
|
* Size of @e data
|
||||||
*/
|
*/
|
||||||
size_t size;
|
size_t size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Non-null if this is not the last parameter.
|
* Non-null if this is not the last parameter.
|
||||||
* This allows for null as sentinal value.
|
* This allows for null as sentinal value.
|
||||||
@ -58,6 +49,27 @@ struct TALER_DB_QueryParam
|
|||||||
int more;
|
int more;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* End of query parameter specification.
|
||||||
|
*/
|
||||||
|
#define TALER_DB_QUERY_PARAM_END { NULL, 0, 0 }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate fixed-size query parameter with size given explicitly.
|
||||||
|
*
|
||||||
|
* @param x pointer to the query parameter to pass
|
||||||
|
* @param s number of bytes of @a x to use for the query
|
||||||
|
*/
|
||||||
|
#define TALER_DB_QUERY_PARAM_PTR_SIZED(x, s) { (x), (s), 1 }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate fixed-size query parameter with size determined
|
||||||
|
* by variable type.
|
||||||
|
*
|
||||||
|
* @param x pointer to the query parameter to pass.
|
||||||
|
*/
|
||||||
|
#define TALER_DB_QUERY_PARAM_PTR(x) TALER_DB_QUERY_PARAM_PTR_SIZED(x, sizeof (*(x)))
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description of a DB result cell.
|
* Description of a DB result cell.
|
||||||
@ -70,7 +82,9 @@ struct TALER_DB_ResultSpec
|
|||||||
void *dst;
|
void *dst;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allowed size for the data.
|
* Allowed size for the data, 0 for variable-size
|
||||||
|
* (in this case, the type of @e dst is a `void **`
|
||||||
|
* and we need to allocate a buffer of the right size).
|
||||||
*/
|
*/
|
||||||
size_t dst_size;
|
size_t dst_size;
|
||||||
|
|
||||||
@ -78,9 +92,47 @@ struct TALER_DB_ResultSpec
|
|||||||
* Field name of the desired result.
|
* Field name of the desired result.
|
||||||
*/
|
*/
|
||||||
char *fname;
|
char *fname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Actual size of the result.
|
||||||
|
*/
|
||||||
|
size_t *result_size;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* End of result parameter specification.
|
||||||
|
*/
|
||||||
|
#define TALER_DB_RESULT_SPEC_END { NULL, 0, NULL, NULL }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We expect a fixed-size result, with size given explicitly
|
||||||
|
*
|
||||||
|
* @param name name of the field in the table
|
||||||
|
* @param dst point to where to store the result
|
||||||
|
* @param s number of bytes we should use in @a dst
|
||||||
|
*/
|
||||||
|
#define TALER_DB_RESULT_SPEC_SIZED(name, dst, s) { (void *) (dst), (s), (name), NULL }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We expect a fixed-size result, with size determined by the type of `* dst`
|
||||||
|
*
|
||||||
|
* @param name name of the field in the table
|
||||||
|
* @param dst point to where to store the result, type fits expected result size
|
||||||
|
*/
|
||||||
|
#define TALER_DB_RESULT_SPEC(name, dst) TALER_DB_RESULT_SPEC_SIZED(name, dst, sizeof (*(dst)))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variable-size result expected.
|
||||||
|
*
|
||||||
|
* @param name name of the field in the table
|
||||||
|
* @param dst where to store the result (of type void **), to be allocated
|
||||||
|
* @param sptr pointer to a `size_t` for where to store the size of @a dst
|
||||||
|
*/
|
||||||
|
#define TALER_DB_RESULT_SPEC_VAR(name, dst, sptr) { (void *) (dst), 0, (name), sptr }
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute a prepared statement.
|
* Execute a prepared statement.
|
||||||
*/
|
*/
|
||||||
@ -96,12 +148,14 @@ TALER_DB_exec_prepared (PGconn *db_conn,
|
|||||||
* is returned.
|
* is returned.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* GNUNET_YES if all results could be extracted
|
* #GNUNET_YES if all results could be extracted
|
||||||
* GNUNET_NO if at least one result was NULL
|
* #GNUNET_NO if at least one result was NULL
|
||||||
* GNUNET_SYSERR if a result was invalid (non-existing field)
|
* #GNUNET_SYSERR if a result was invalid (non-existing field)
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
TALER_DB_extract_result (PGresult *result, struct TALER_DB_ResultSpec *rs, int row);
|
TALER_DB_extract_result (PGresult *result,
|
||||||
|
struct TALER_DB_ResultSpec *rs,
|
||||||
|
int row);
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -13,15 +13,13 @@
|
|||||||
You should have received a copy of the GNU General Public License along with
|
You should have received a copy of the GNU General Public License along with
|
||||||
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
|
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file util/db.c
|
* @file util/db.c
|
||||||
* @brief helper functions for DB interactions
|
* @brief helper functions for DB interactions
|
||||||
* @author Sree Harsha Totakura <sreeharsha@totakura.in>
|
* @author Sree Harsha Totakura <sreeharsha@totakura.in>
|
||||||
* @author Florian Dold
|
* @author Florian Dold
|
||||||
|
* @author Christian Grothoff
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
#include <gnunet/gnunet_util_lib.h>
|
#include <gnunet/gnunet_util_lib.h>
|
||||||
#include "taler_db_lib.h"
|
#include "taler_db_lib.h"
|
||||||
@ -39,12 +37,11 @@ TALER_DB_exec_prepared (PGconn *db_conn,
|
|||||||
unsigned i;
|
unsigned i;
|
||||||
|
|
||||||
/* count the number of parameters */
|
/* count the number of parameters */
|
||||||
|
|
||||||
{
|
{
|
||||||
const struct TALER_DB_QueryParam *x;
|
const struct TALER_DB_QueryParam *x;
|
||||||
for (len = 0, x = params;
|
for (len = 0, x = params;
|
||||||
x->more;
|
x->more;
|
||||||
len +=1, x += 1);
|
len++, x++);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* new scope to allow stack allocation without alloca */
|
/* new scope to allow stack allocation without alloca */
|
||||||
@ -61,20 +58,22 @@ TALER_DB_exec_prepared (PGconn *db_conn,
|
|||||||
param_formats[i] = 1;
|
param_formats[i] = 1;
|
||||||
}
|
}
|
||||||
return PQexecPrepared (db_conn, name, len,
|
return PQexecPrepared (db_conn, name, len,
|
||||||
(const char **) param_values, param_lengths, param_formats, 1);
|
(const char **) param_values,
|
||||||
|
param_lengths,
|
||||||
|
param_formats, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract results from a query result according to the given specification.
|
* Extract results from a query result according to the given specification.
|
||||||
* If colums are NULL, the destination is not modified, and GNUNET_NO
|
* If colums are NULL, the destination is not modified, and #GNUNET_NO
|
||||||
* is returned.
|
* is returned.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* GNUNET_YES if all results could be extracted
|
* #GNUNET_YES if all results could be extracted
|
||||||
* GNUNET_NO if at least one result was NULL
|
* #GNUNET_NO if at least one result was NULL
|
||||||
* GNUNET_SYSERR if a result was invalid (non-existing field)
|
* #GNUNET_SYSERR if a result was invalid (non-existing field)
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
TALER_DB_extract_result (PGresult *result,
|
TALER_DB_extract_result (PGresult *result,
|
||||||
@ -82,35 +81,56 @@ TALER_DB_extract_result (PGresult *result,
|
|||||||
int row)
|
int row)
|
||||||
{
|
{
|
||||||
int had_null = GNUNET_NO;
|
int had_null = GNUNET_NO;
|
||||||
|
size_t len;
|
||||||
|
unsigned int i;
|
||||||
|
unsigned int j;
|
||||||
|
|
||||||
for (; NULL != rs->fname; rs += 1)
|
for (i=0; NULL != rs[i].fname; i++)
|
||||||
{
|
{
|
||||||
int fnum;
|
int fnum;
|
||||||
fnum = PQfnumber (result, rs->fname);
|
|
||||||
|
fnum = PQfnumber (result, rs[i].fname);
|
||||||
if (fnum < 0)
|
if (fnum < 0)
|
||||||
{
|
{
|
||||||
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "field '%s' does not exist in result\n", rs->fname);
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
||||||
|
"field '%s' does not exist in result\n",
|
||||||
|
rs->fname);
|
||||||
return GNUNET_SYSERR;
|
return GNUNET_SYSERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if a field is null, continue but
|
/* if a field is null, continue but
|
||||||
* remember that we now return a different result */
|
* remember that we now return a different result */
|
||||||
|
|
||||||
if (PQgetisnull (result, row, fnum))
|
if (PQgetisnull (result, row, fnum))
|
||||||
{
|
{
|
||||||
had_null = GNUNET_YES;
|
had_null = GNUNET_YES;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const char *res;
|
const char *res;
|
||||||
if (rs->dst_size != PQgetlength (result, row, fnum))
|
len = PQgetlength (result, row, fnum);
|
||||||
|
if ( (0 != rs[i].dst_size) &&
|
||||||
|
(rs[i].dst_size != len) )
|
||||||
{
|
{
|
||||||
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "field '%s' has wrong size (got %u, expected %u)\n",
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
||||||
rs->fname, (int) PQgetlength (result, row, fnum), (int) rs->dst_size);
|
"field '%s' has wrong size (got %d, expected %d)\n",
|
||||||
|
rs[i].fname,
|
||||||
|
(int) len,
|
||||||
|
(int) rs->dst_size);
|
||||||
|
for (j=0;j<i;j++)
|
||||||
|
if (0 == rs[i].dst_size)
|
||||||
|
{
|
||||||
|
GNUNET_free (rs[i].dst);
|
||||||
|
rs[i].dst = NULL;
|
||||||
|
*rs[i].result_size = 0;
|
||||||
|
}
|
||||||
return GNUNET_SYSERR;
|
return GNUNET_SYSERR;
|
||||||
}
|
}
|
||||||
res = PQgetvalue (result, row, fnum);
|
res = PQgetvalue (result, row, fnum);
|
||||||
GNUNET_assert (NULL != res);
|
GNUNET_assert (NULL != res);
|
||||||
memcpy (rs->dst, res, rs->dst_size);
|
if (0 == rs->dst_size)
|
||||||
|
*(void**) rs->dst = GNUNET_malloc (*rs->result_size = len);
|
||||||
|
memcpy (rs->dst,
|
||||||
|
res,
|
||||||
|
len);
|
||||||
}
|
}
|
||||||
if (GNUNET_YES == had_null)
|
if (GNUNET_YES == had_null)
|
||||||
return GNUNET_NO;
|
return GNUNET_NO;
|
||||||
@ -124,6 +144,7 @@ TALER_DB_field_isnull (PGresult *result,
|
|||||||
const char *fname)
|
const char *fname)
|
||||||
{
|
{
|
||||||
int fnum;
|
int fnum;
|
||||||
|
|
||||||
fnum = PQfnumber (result, fname);
|
fnum = PQfnumber (result, fname);
|
||||||
GNUNET_assert (fnum >= 0);
|
GNUNET_assert (fnum >= 0);
|
||||||
if (PQgetisnull (result, row, fnum))
|
if (PQgetisnull (result, row, fnum))
|
||||||
|
Loading…
Reference in New Issue
Block a user