2 This file is part of GNUnet
3 Copyright (C) 2017, 2019 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
21 * @file pq/pq_prepare.c
22 * @brief functions to connect to libpq (PostGres)
23 * @author Christian Grothoff
30 * Create a `struct GNUNET_PQ_PreparedStatement`.
32 * @param name name of the statement
33 * @param sql actual SQL statement
34 * @param num_args number of arguments in the statement
35 * @return initialized struct
37 struct GNUNET_PQ_PreparedStatement
38 GNUNET_PQ_make_prepare (const char *name,
40 unsigned int num_args)
42 struct GNUNET_PQ_PreparedStatement ps = {
45 .num_arguments = num_args
53 * Request creation of prepared statements @a ps from Postgres.
55 * @param db database to prepare the statements for
56 * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
58 * @return #GNUNET_OK on success,
59 * #GNUNET_SYSERR on error
62 GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
63 const struct GNUNET_PQ_PreparedStatement *ps)
67 /* add 'ps' to list db->ps of prepared statements to run on reconnect! */
68 unsigned int olen = 0; /* length of existing 'db->ps' array */
69 unsigned int nlen = 0; /* length of 'ps' array */
70 struct GNUNET_PQ_PreparedStatement *rps; /* combined array */
73 while (NULL != db->ps[olen].name)
75 while (NULL != ps[nlen].name)
77 rps = GNUNET_new_array (olen + nlen + 1,
78 struct GNUNET_PQ_PreparedStatement);
82 olen * sizeof (struct GNUNET_PQ_PreparedStatement));
85 nlen * sizeof (struct GNUNET_PQ_PreparedStatement));
86 GNUNET_free_non_null (db->ps);
90 /* actually prepare statements */
91 for (unsigned int i = 0; NULL != ps[i].name; i++)
95 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
97 "Preparing SQL statement `%s' as `%s'\n",
100 ret = PQprepare (db->conn,
105 if (PGRES_COMMAND_OK != PQresultStatus (ret))
107 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
109 _ ("PQprepare (`%s' as `%s') failed with error: %s\n"),
112 PQerrorMessage (db->conn));
114 return GNUNET_SYSERR;
122 /* end of pq/pq_prepare.c */