GNUnet  0.10.x
pq_prepare.c
Go to the documentation of this file.
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  SPDX-License-Identifier: AGPL3.0-or-later
19  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_pq_lib.h"
28 
29 
40  const char *sql,
41  unsigned int num_args)
42 {
43  struct GNUNET_PQ_PreparedStatement ps = {
44  .name = name,
45  .sql = sql,
46  .num_arguments = num_args
47  };
48 
49  return ps;
50 }
51 
52 
62 int
63 GNUNET_PQ_prepare_statements(PGconn *connection,
64  const struct GNUNET_PQ_PreparedStatement *ps)
65 {
66  for (unsigned int i = 0; NULL != ps[i].name; i++)
67  {
68  PGresult *ret;
69 
71  "pq",
72  "Preparing SQL statement `%s' as `%s'\n",
73  ps[i].sql,
74  ps[i].name);
75  ret = PQprepare(connection,
76  ps[i].name,
77  ps[i].sql,
78  ps[i].num_arguments,
79  NULL);
80  if (PGRES_COMMAND_OK != PQresultStatus(ret))
81  {
83  "pq",
84  _("PQprepare (`%s' as `%s') failed with error: %s\n"),
85  ps[i].sql,
86  ps[i].name,
87  PQerrorMessage(connection));
88  PQclear(ret);
89  return GNUNET_SYSERR;
90  }
91  PQclear(ret);
92  }
93  return GNUNET_OK;
94 }
95 
96 
97 /* end of pq/pq_prepare.c */
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
const char * name
Name of the statement.
static int ret
Final status code.
Definition: gnunet-arm.c:89
int GNUNET_PQ_prepare_statements(PGconn *connection, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres.
Definition: pq_prepare.c:63
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
const char * sql
Actual SQL statement.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
helper functions for Postgres DB interactions
const char * name
struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare(const char *name, const char *sql, unsigned int num_args)
Create a struct GNUNET_PQ_PreparedStatement.
Definition: pq_prepare.c:39
unsigned int num_arguments
Number of arguments included in sql.
#define GNUNET_log_from(kind, comp,...)