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
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @brief helper functions for libpq (PostGres) interactions
23 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
24 * @author Florian Dold
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_pq_lib.h"
33 * Execute a prepared statement.
35 * @param db_conn database connection
36 * @param name name of the prepared statement
37 * @param params parameters to the statement
38 * @return postgres result
41 GNUNET_PQ_exec_prepared (PGconn *db_conn,
43 const struct GNUNET_PQ_QueryParam *params)
48 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
49 "Running prepared statement `%s' on %p\n",
52 /* count the number of parameters */
54 for (i=0;0 != params[i].num_params;i++)
55 len += params[i].num_params;
57 /* new scope to allow stack allocation without alloca */
59 /* Scratch buffer for temporary storage */
61 /* Parameter array we are building for the query */
62 void *param_values[len];
63 int param_lengths[len];
64 int param_formats[len];
66 /* How many entries in the scratch buffer are in use? */
73 for (i=0;0 != params[i].num_params;i++)
75 const struct GNUNET_PQ_QueryParam *x = ¶ms[i];
77 ret = x->conv (x->conv_cls,
88 for (off = 0; off < soff; off++)
89 GNUNET_free (scratch[off]);
95 GNUNET_assert (off == len);
96 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
98 "Executing prepared SQL statement `%s'\n",
100 res = PQexecPrepared (db_conn,
103 (const char **) param_values,
107 for (off = 0; off < soff; off++)
108 GNUNET_free (scratch[off]);
115 * Free all memory that was allocated in @a rs during
116 * #GNUNET_PQ_extract_result().
118 * @param rs reult specification to clean up
121 GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs)
125 for (i=0; NULL != rs[i].conv; i++)
126 if (NULL != rs[i].cleaner)
127 rs[i].cleaner (rs[i].cls,
133 * Extract results from a query result according to the given
136 * @param result result to process
137 * @param[in,out] rs result specification to extract for
138 * @param row row from the result to extract
140 * #GNUNET_YES if all results could be extracted
141 * #GNUNET_SYSERR if a result was invalid (non-existing field)
144 GNUNET_PQ_extract_result (PGresult *result,
145 struct GNUNET_PQ_ResultSpec *rs,
151 for (i=0; NULL != rs[i].conv; i++)
153 struct GNUNET_PQ_ResultSpec *spec;
156 ret = spec->conv (spec->cls,
162 if (GNUNET_OK != ret)
164 GNUNET_PQ_cleanup_result (rs);
165 return GNUNET_SYSERR;
167 if (NULL != spec->result_size)
168 *spec->result_size = spec->dst_size;