aboutsummaryrefslogtreecommitdiff
path: root/src/lib/pq/pq_exec.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/pq/pq_exec.c')
-rw-r--r--src/lib/pq/pq_exec.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/lib/pq/pq_exec.c b/src/lib/pq/pq_exec.c
new file mode 100644
index 000000000..1fd8c5068
--- /dev/null
+++ b/src/lib/pq/pq_exec.c
@@ -0,0 +1,95 @@
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
29struct GNUNET_PQ_ExecuteStatement
30GNUNET_PQ_make_execute (const char *sql)
31{
32 struct GNUNET_PQ_ExecuteStatement es = {
33 .sql = sql,
34 .ignore_errors = GNUNET_NO
35 };
36
37 return es;
38}
39
40
41struct GNUNET_PQ_ExecuteStatement
42GNUNET_PQ_make_try_execute (const char *sql)
43{
44 struct GNUNET_PQ_ExecuteStatement es = {
45 .sql = sql,
46 .ignore_errors = GNUNET_YES
47 };
48
49 return es;
50}
51
52
53enum GNUNET_GenericReturnValue
54GNUNET_PQ_exec_statements (struct GNUNET_PQ_Context *db,
55 const struct GNUNET_PQ_ExecuteStatement *es)
56{
57 for (unsigned int i = 0; NULL != es[i].sql; i++)
58 {
59 PGresult *result;
60
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62 "Running statement `%s' on %p\n",
63 es[i].sql,
64 db);
65 result = PQexec (db->conn,
66 es[i].sql);
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
68 "Running statement `%s' on %p finished (%s)\n",
69 es[i].sql,
70 db,
71 PQresStatus (PQresultStatus (result)));
72 if ((GNUNET_NO == es[i].ignore_errors) &&
73 (PGRES_COMMAND_OK != PQresultStatus (result)))
74 {
75 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
76 "pq",
77 "Failed to execute `%s': %s/%s/%s/%s/%s",
78 es[i].sql,
79 PQresultErrorField (result,
80 PG_DIAG_MESSAGE_PRIMARY),
81 PQresultErrorField (result,
82 PG_DIAG_MESSAGE_DETAIL),
83 PQresultErrorMessage (result),
84 PQresStatus (PQresultStatus (result)),
85 PQerrorMessage (db->conn));
86 PQclear (result);
87 return GNUNET_SYSERR;
88 }
89 PQclear (result);
90 }
91 return GNUNET_OK;
92}
93
94
95/* end of pq/pq_exec.c */