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