d557410736ec54f338d8d1d47baf4619c7f2d593
[oweals/gnunet.git] / src / pq / pq_prepare.c
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2017 GNUnet e.V.
4
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.
9
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.
14  
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/>.
17 */
18 /**
19  * @file pq/pq_prepare.c
20  * @brief functions to connect to libpq (PostGres)
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_pq_lib.h"
26
27
28 /**
29  * Create a `struct GNUNET_PQ_PreparedStatement`.
30  *
31  * @param name name of the statement
32  * @param sql actual SQL statement
33  * @param num_args number of arguments in the statement
34  * @return initialized struct
35  */
36 struct GNUNET_PQ_PreparedStatement
37 GNUNET_PQ_make_prepare (const char *name,
38                         const char *sql,
39                         unsigned int num_args)
40 {
41   struct GNUNET_PQ_PreparedStatement ps = {
42     .name = name,
43     .sql = sql,
44     .num_arguments = num_args
45   };
46
47   return ps;
48 }
49
50
51 /**
52  * Request creation of prepared statements @a ps from Postgres.
53  *
54  * @param connection connection to prepare the statements for
55  * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
56  *            statements.
57  * @return #GNUNET_OK on success,
58  *         #GNUNET_SYSERR on error
59  */
60 int
61 GNUNET_PQ_prepare_statements (PGconn *connection,
62                               const struct GNUNET_PQ_PreparedStatement *ps)
63 {
64   for (unsigned int i=0;NULL != ps[i].name;i++)
65   {
66     PGresult *ret;
67
68     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
69                      "pq",
70                      "Preparing SQL statement `%s' as `%s'\n",
71                      ps[i].sql,
72                      ps[i].name);
73     ret = PQprepare (connection,
74                      ps[i].name,
75                      ps[i].sql,
76                      ps[i].num_arguments,
77                      NULL);
78     if (PGRES_COMMAND_OK != PQresultStatus (ret))
79     {
80       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
81                        "pq",
82                        _("PQprepare (`%s' as `%s') failed with error: %s\n"),
83                        ps[i].sql,
84                        ps[i].name,
85                        PQerrorMessage (connection));
86       PQclear (ret);
87       return GNUNET_SYSERR;
88     }
89     PQclear (ret);
90   }
91   return GNUNET_OK;
92 }
93
94
95 /* end of pq/pq_prepare.c */