* @param[out] dst where to store the result
* @return
* #GNUNET_YES if all results could be extracted
- * #GNUNET_NO if at least one result was NULL
* #GNUNET_SYSERR if a result was invalid (non-existing field)
- */
+ */
static int
extract_varsize_blob (void *cls,
PGresult *result,
const char *res;
void *idst;
int fnum;
-
+
fnum = PQfnumber (result,
fname);
if (fnum < 0)
if (PQgetisnull (result,
row,
fnum))
- return GNUNET_NO;
-
+ return GNUNET_SYSERR;
+
/* if a field is null, continue but
* remember that we now return a different result */
len = PQgetlength (result,
{
struct GNUNET_PQ_ResultSpec res =
{ &extract_varsize_blob,
- &clean_varsize_blob, NULL,
+ &clean_varsize_blob, NULL,
(void *) (dst), 0, name, sptr };
return res;
}
* @param[out] dst where to store the result
* @return
* #GNUNET_YES if all results could be extracted
- * #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 or NULL)
+ */
static int
extract_fixed_blob (void *cls,
PGresult *result,
size_t len;
const char *res;
int fnum;
-
+
fnum = PQfnumber (result,
fname);
if (fnum < 0)
if (PQgetisnull (result,
row,
fnum))
- return GNUNET_NO;
-
+ return GNUNET_SYSERR;
+
/* if a field is null, continue but
* remember that we now return a different result */
len = PQgetlength (result,
row,
fnum);
- if (*dst_size != len)
+ if (*dst_size != len)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Field `%s' has wrong size (got %u, expected %u)\n",
{
struct GNUNET_PQ_ResultSpec res =
{ &extract_fixed_blob,
- NULL, NULL,
+ NULL, NULL,
(dst), dst_size, name, NULL };
return res;
}
* @param[out] dst where to store the result
* @return
* #GNUNET_YES if all results could be extracted
- * #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 or NULL)
+ */
static int
extract_rsa_public_key (void *cls,
PGresult *result,
size_t *dst_size,
void *dst)
{
- struct GNUNET_CRYPTO_rsa_PublicKey **pk = dst;
+ struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
size_t len;
const char *res;
int fnum;
if (PQgetisnull (result,
row,
fnum))
- return GNUNET_NO;
+ return GNUNET_SYSERR;
/* if a field is null, continue but
* remember that we now return a different result */
clean_rsa_public_key (void *cls,
void *rd)
{
- struct GNUNET_CRYPTO_rsa_PublicKey **pk = rd;
-
+ struct GNUNET_CRYPTO_RsaPublicKey **pk = rd;
+
if (NULL != *pk)
{
GNUNET_CRYPTO_rsa_public_key_free (*pk);
*/
struct GNUNET_PQ_ResultSpec
GNUNET_PQ_result_spec_rsa_public_key (const char *name,
- struct GNUNET_CRYPTO_rsa_PublicKey **rsa)
+ struct GNUNET_CRYPTO_RsaPublicKey **rsa)
{
struct GNUNET_PQ_ResultSpec res =
{ &extract_rsa_public_key,
* @param[out] dst where to store the result
* @return
* #GNUNET_YES if all results could be extracted
- * #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 or NULL)
+ */
static int
extract_rsa_signature (void *cls,
PGresult *result,
size_t *dst_size,
void *dst)
{
- struct GNUNET_CRYPTO_rsa_Signature **sig = dst;
+ struct GNUNET_CRYPTO_RsaSignature **sig = dst;
size_t len;
const char *res;
int fnum;
-
+
*sig = NULL;
fnum = PQfnumber (result,
fname);
if (PQgetisnull (result,
row,
fnum))
- return GNUNET_NO;
+ return GNUNET_SYSERR;
/* if a field is null, continue but
* remember that we now return a different result */
clean_rsa_signature (void *cls,
void *rd)
{
- struct GNUNET_CRYPTO_rsa_Signature **sig = rd;
+ struct GNUNET_CRYPTO_RsaSignature **sig = rd;
if (NULL != *sig)
{
*/
struct GNUNET_PQ_ResultSpec
GNUNET_PQ_result_spec_rsa_signature (const char *name,
- struct GNUNET_CRYPTO_rsa_Signature **sig)
+ struct GNUNET_CRYPTO_RsaSignature **sig)
{
struct GNUNET_PQ_ResultSpec res =
{ &extract_rsa_signature,
}
+/**
+ * Extract data from a Postgres database @a result at row @a row.
+ *
+ * @param cls closure
+ * @param result where to extract data from
+ * @param int row to extract data from
+ * @param fname name (or prefix) of the fields to extract from
+ * @param[in,out] dst_size where to store size of result, may be NULL
+ * @param[out] dst where to store the result
+ * @return
+ * #GNUNET_YES if all results could be extracted
+ * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
+ */
+static int
+extract_string (void *cls,
+ PGresult *result,
+ int row,
+ const char *fname,
+ size_t *dst_size,
+ void *dst)
+{
+ char **str = dst;
+ size_t len;
+ const char *res;
+ int fnum;
+
+ *str = NULL;
+ fnum = PQfnumber (result,
+ fname);
+ if (fnum < 0)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Field `%s' does not exist in result\n",
+ fname);
+ return GNUNET_SYSERR;
+ }
+ if (PQgetisnull (result,
+ row,
+ fnum))
+ return GNUNET_SYSERR;
+
+ /* if a field is null, continue but
+ * remember that we now return a different result */
+ len = PQgetlength (result,
+ row,
+ fnum);
+ res = PQgetvalue (result,
+ row,
+ fnum);
+ *str = GNUNET_strndup (res,
+ len);
+ if (NULL == *str)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Field `%s' contains bogus value (fails to decode)\n",
+ fname);
+ return GNUNET_SYSERR;
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Function called to clean up memory allocated
+ * by a #GNUNET_PQ_ResultConverter.
+ *
+ * @param cls closure
+ * @param rd result data to clean up
+ */
+static void
+clean_string (void *cls,
+ void *rd)
+{
+ char **str = rd;
+
+ if (NULL != *str)
+ {
+ GNUNET_free (*str);
+ *str = NULL;
+ }
+}
+
+
+/**
+ * 0-terminated string expected.
+ *
+ * @param name name of the field in the table
+ * @param[out] dst where to store the result, allocated
+ * @return array entry for the result specification to use
+ */
+struct GNUNET_PQ_ResultSpec
+GNUNET_PQ_result_spec_string (const char *name,
+ char **dst)
+{
+ struct GNUNET_PQ_ResultSpec res =
+ { &extract_string,
+ &clean_string,
+ NULL,
+ (void *) dst, 0, (name), NULL };
+ return res;
+}
+
+
/**
* Absolute time expected.
*
* @param[out] dst where to store the result
* @return
* #GNUNET_YES if all results could be extracted
- * #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 or NULL)
+ */
static int
extract_uint16 (void *cls,
PGresult *result,
uint16_t *udst = dst;
const uint16_t *res;
int fnum;
-
+
fnum = PQfnumber (result,
fname);
if (fnum < 0)
if (PQgetisnull (result,
row,
fnum))
- return GNUNET_NO;
+ return GNUNET_SYSERR;
GNUNET_assert (NULL != dst);
if (sizeof (uint16_t) != *dst_size)
{
* @param[out] dst where to store the result
* @return
* #GNUNET_YES if all results could be extracted
- * #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 or NULL)
+ */
static int
extract_uint32 (void *cls,
PGresult *result,
uint32_t *udst = dst;
const uint32_t *res;
int fnum;
-
+
fnum = PQfnumber (result,
fname);
if (fnum < 0)
if (PQgetisnull (result,
row,
fnum))
- return GNUNET_NO;
+ return GNUNET_SYSERR;
GNUNET_assert (NULL != dst);
if (sizeof (uint32_t) != *dst_size)
{
uint32_t *u32)
{
struct GNUNET_PQ_ResultSpec res =
- { &extract_uint32,
+ { &extract_uint32,
NULL,
NULL,
(void *) u32, sizeof (*u32), (name), NULL };
* @param[out] dst where to store the result
* @return
* #GNUNET_YES if all results could be extracted
- * #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 or NULL)
+ */
static int
extract_uint64 (void *cls,
PGresult *result,
uint64_t *udst = dst;
const uint64_t *res;
int fnum;
-
+
fnum = PQfnumber (result,
fname);
if (fnum < 0)
if (PQgetisnull (result,
row,
fnum))
- return GNUNET_NO;
+ return GNUNET_SYSERR;
GNUNET_assert (NULL != dst);
if (sizeof (uint64_t) != *dst_size)
{