GNUnet  0.19.2
pq_exec.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2017, 2019 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 "pq.h"
27 
28 
36 GNUNET_PQ_make_execute (const char *sql)
37 {
38  struct GNUNET_PQ_ExecuteStatement es = {
39  .sql = sql,
40  .ignore_errors = GNUNET_NO
41  };
42 
43  return es;
44 }
45 
46 
55 GNUNET_PQ_make_try_execute (const char *sql)
56 {
57  struct GNUNET_PQ_ExecuteStatement es = {
58  .sql = sql,
59  .ignore_errors = GNUNET_YES
60  };
61 
62  return es;
63 }
64 
65 
68  const struct GNUNET_PQ_ExecuteStatement *es)
69 {
70  for (unsigned int i = 0; NULL != es[i].sql; i++)
71  {
72  PGresult *result;
73 
75  "Running statement `%s' on %p\n",
76  es[i].sql,
77  db);
78  result = PQexec (db->conn,
79  es[i].sql);
81  "Running statement `%s' on %p finished (%s)\n",
82  es[i].sql,
83  db,
84  PQresStatus (PQresultStatus (result)));
85  if ((GNUNET_NO == es[i].ignore_errors) &&
86  (PGRES_COMMAND_OK != PQresultStatus (result)))
87  {
89  "pq",
90  "Failed to execute `%s': %s/%s/%s/%s/%s",
91  es[i].sql,
92  PQresultErrorField (result,
93  PG_DIAG_MESSAGE_PRIMARY),
94  PQresultErrorField (result,
95  PG_DIAG_MESSAGE_DETAIL),
96  PQresultErrorMessage (result),
97  PQresStatus (PQresultStatus (result)),
98  PQerrorMessage (db->conn));
99  PQclear (result);
100  return GNUNET_SYSERR;
101  }
102  PQclear (result);
103  }
104  return GNUNET_OK;
105 }
106 
107 
108 /* end of pq/pq_exec.c */
static int result
Global testing status.
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:97
#define GNUNET_log(kind,...)
#define GNUNET_log_from(kind, comp,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
shared internal data structures of libgnunetpq
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_execute(const char *sql)
Create a struct GNUNET_PQ_ExecuteStatement where errors are fatal.
Definition: pq_exec.c:36
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_ExecuteStatement *es)
Request execution of an array of statements es from Postgres.
Definition: pq_exec.c:67
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_try_execute(const char *sql)
Create a struct GNUNET_PQ_ExecuteStatement where errors should be tolerated.
Definition: pq_exec.c:55
Handle to Postgres database.
Definition: pq.h:36
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().
const char * sql
Actual SQL statement.
int ignore_errors
Should we ignore errors?