aboutsummaryrefslogtreecommitdiff
path: root/src/pq/pq_exec.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq/pq_exec.c')
-rw-r--r--src/pq/pq_exec.c117
1 files changed, 0 insertions, 117 deletions
diff --git a/src/pq/pq_exec.c b/src/pq/pq_exec.c
deleted file mode 100644
index dcde331b6..000000000
--- a/src/pq/pq_exec.c
+++ /dev/null
@@ -1,117 +0,0 @@
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 */
20/**
21 * @file pq/pq_exec.c
22 * @brief functions to execute plain SQL statements (PostGres)
23 * @author Christian Grothoff
24 */
25#include "platform.h"
26#include "pq.h"
27
28
29/**
30 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors are fatal.
31 *
32 * @param sql actual SQL statement
33 * @return initialized struct
34 */
35struct GNUNET_PQ_ExecuteStatement
36GNUNET_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
47/**
48 * Create a `struct GNUNET_PQ_ExecuteStatement` where errors should
49 * be tolerated.
50 *
51 * @param sql actual SQL statement
52 * @return initialized struct
53 */
54struct GNUNET_PQ_ExecuteStatement
55GNUNET_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
66/**
67 * Request execution of an array of statements @a es from Postgres.
68 *
69 * @param db database to execute the statements with
70 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
71 * statements.
72 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
73 * #GNUNET_SYSERR on error
74 */
75enum GNUNET_GenericReturnValue
76GNUNET_PQ_exec_statements (struct GNUNET_PQ_Context *db,
77 const struct GNUNET_PQ_ExecuteStatement *es)
78{
79 for (unsigned int i = 0; NULL != es[i].sql; i++)
80 {
81 PGresult *result;
82
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84 "Running statement `%s' on %p\n",
85 es[i].sql,
86 db);
87 result = PQexec (db->conn,
88 es[i].sql);
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90 "Running statement `%s' on %p finished (%d)\n",
91 es[i].sql,
92 db,
93 PGRES_COMMAND_OK == PQresultStatus (result));
94 if ((GNUNET_NO == es[i].ignore_errors) &&
95 (PGRES_COMMAND_OK != PQresultStatus (result)))
96 {
97 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
98 "pq",
99 "Failed to execute `%s': %s/%s/%s/%s/%s",
100 es[i].sql,
101 PQresultErrorField (result,
102 PG_DIAG_MESSAGE_PRIMARY),
103 PQresultErrorField (result,
104 PG_DIAG_MESSAGE_DETAIL),
105 PQresultErrorMessage (result),
106 PQresStatus (PQresultStatus (result)),
107 PQerrorMessage (db->conn));
108 PQclear (result);
109 return GNUNET_SYSERR;
110 }
111 PQclear (result);
112 }
113 return GNUNET_OK;
114}
115
116
117/* end of pq/pq_exec.c */