2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
18 * @brief helper functions for libpq (PostGres) interactions
19 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20 * @author Florian Dold
21 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
25 #include "gnunet_pq_lib.h"
29 * Execute a prepared statement.
31 * @param db_conn database connection
32 * @param name name of the prepared statement
33 * @param params parameters to the statement
34 * @return postgres result
37 GNUNET_PQ_exec_prepared (PGconn *db_conn,
39 const struct GNUNET_PQ_QueryParam *params)
44 /* count the number of parameters */
46 for (i=0;0 != params[i].num_params;i++)
47 len += params[i].num_params;
49 /* new scope to allow stack allocation without alloca */
51 /* Scratch buffer for temporary storage */
53 /* Parameter array we are building for the query */
54 void *param_values[len];
55 int param_lengths[len];
56 int param_formats[len];
58 /* How many entries in the scratch buffer are in use? */
65 for (i=0;0 != params[i].num_params;i++)
67 const struct GNUNET_PQ_QueryParam *x = ¶ms[i];
69 ret = x->conv (x->conv_cls,
80 for (off = 0; off < soff; off++)
81 GNUNET_free (scratch[off]);
87 GNUNET_assert (off == len);
88 res = PQexecPrepared (db_conn,
91 (const char **) param_values,
95 for (off = 0; off < soff; off++)
96 GNUNET_free (scratch[off]);
103 * Free all memory that was allocated in @a rs during
104 * #GNUNET_PQ_extract_result().
106 * @param rs reult specification to clean up
109 GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs)
113 for (i=0; NULL != rs[i].conv; i++)
114 if (NULL != rs[i].cleaner)
115 rs[i].cleaner (rs[i].cls,
121 * Extract results from a query result according to the given
122 * specification. If colums are NULL, the destination is not
123 * modified, and #GNUNET_NO is returned.
125 * @param result result to process
126 * @param[in,out] rs result specification to extract for
127 * @param row row from the result to extract
129 * #GNUNET_YES if all results could be extracted
130 * #GNUNET_NO if at least one result was NULL
131 * #GNUNET_SYSERR if a result was invalid (non-existing field)
134 GNUNET_PQ_extract_result (PGresult *result,
135 struct GNUNET_PQ_ResultSpec *rs,
139 int had_null = GNUNET_NO;
142 for (i=0; NULL != rs[i].conv; i++)
144 struct GNUNET_PQ_ResultSpec *spec;
147 ret = spec->conv (spec->cls,
153 if (GNUNET_SYSERR == ret)
155 GNUNET_PQ_cleanup_result (rs);
156 return GNUNET_SYSERR;
158 if (NULL != spec->result_size)
159 *spec->result_size = spec->dst_size;
161 if (GNUNET_YES == had_null)