diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-06-01 21:48:19 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-06-01 21:48:19 +0200 |
commit | 1defd30dfeb1867c2756b3fe6a437f695951d0c9 (patch) | |
tree | b48c0fe6bb32469cfcb4284bfac3142e22417ae8 /src/pq/pq_exec.c | |
parent | bbbe0b2404d131cc0d9eda26725b65b47a7e073a (diff) | |
download | gnunet-1defd30dfeb1867c2756b3fe6a437f695951d0c9.tar.gz gnunet-1defd30dfeb1867c2756b3fe6a437f695951d0c9.zip |
adding more good helpers to libgnunetpq
Diffstat (limited to 'src/pq/pq_exec.c')
-rw-r--r-- | src/pq/pq_exec.c | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/src/pq/pq_exec.c b/src/pq/pq_exec.c new file mode 100644 index 000000000..1e5e4eb76 --- /dev/null +++ b/src/pq/pq_exec.c | |||
@@ -0,0 +1,106 @@ | |||
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 under the | ||
6 | terms of the GNU General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | /** | ||
17 | * @file pq/pq_exec.c | ||
18 | * @brief functions to execute plain SQL statements (PostGres) | ||
19 | * @author Christian Grothoff | ||
20 | */ | ||
21 | #include "platform.h" | ||
22 | #include "gnunet_util_lib.h" | ||
23 | #include "gnunet_pq_lib.h" | ||
24 | |||
25 | |||
26 | /** | ||
27 | * Create a `struct GNUNET_PQ_ExecuteStatement` where errors are fatal. | ||
28 | * | ||
29 | * @param sql actual SQL statement | ||
30 | * @return initialized struct | ||
31 | */ | ||
32 | struct GNUNET_PQ_ExecuteStatement | ||
33 | GNUNET_PQ_make_execute (const char *sql) | ||
34 | { | ||
35 | struct GNUNET_PQ_ExecuteStatement es = { | ||
36 | .sql = sql, | ||
37 | .ignore_errors = GNUNET_NO | ||
38 | }; | ||
39 | |||
40 | return es; | ||
41 | } | ||
42 | |||
43 | |||
44 | /** | ||
45 | * Create a `struct GNUNET_PQ_ExecuteStatement` where errors should | ||
46 | * be tolerated. | ||
47 | * | ||
48 | * @param sql actual SQL statement | ||
49 | * @return initialized struct | ||
50 | */ | ||
51 | struct GNUNET_PQ_ExecuteStatement | ||
52 | GNUNET_PQ_make_try_execute (const char *sql) | ||
53 | { | ||
54 | struct GNUNET_PQ_ExecuteStatement es = { | ||
55 | .sql = sql, | ||
56 | .ignore_errors = GNUNET_YES | ||
57 | }; | ||
58 | |||
59 | return es; | ||
60 | } | ||
61 | |||
62 | |||
63 | /** | ||
64 | * Request execution of an array of statements @a es from Postgres. | ||
65 | * | ||
66 | * @param connection connection to execute the statements over | ||
67 | * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared | ||
68 | * statements. | ||
69 | * @return #GNUNET_OK on success (modulo statements where errors can be ignored) | ||
70 | * #GNUNET_SYSERR on error | ||
71 | */ | ||
72 | int | ||
73 | GNUNET_PQ_exec_statements (PGconn *connection, | ||
74 | const struct GNUNET_PQ_ExecuteStatement *es) | ||
75 | { | ||
76 | for (unsigned int i=0; NULL != es[i].sql; i++) | ||
77 | { | ||
78 | PGresult *result; | ||
79 | |||
80 | result = PQexec (connection, | ||
81 | es[i].sql); | ||
82 | |||
83 | if ( (GNUNET_NO == es[i].ignore_errors) && | ||
84 | (PGRES_COMMAND_OK != PQresultStatus (result)) ) | ||
85 | { | ||
86 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
87 | "pq", | ||
88 | "Failed to execute `%s': %s/%s/%s/%s/%s", | ||
89 | es[i].sql, | ||
90 | PQresultErrorField (result, | ||
91 | PG_DIAG_MESSAGE_PRIMARY), | ||
92 | PQresultErrorField (result, | ||
93 | PG_DIAG_MESSAGE_DETAIL), | ||
94 | PQresultErrorMessage (result), | ||
95 | PQresStatus (PQresultStatus (result)), | ||
96 | PQerrorMessage (connection)); | ||
97 | PQclear (result); | ||
98 | return GNUNET_SYSERR; | ||
99 | } | ||
100 | PQclear (result); | ||
101 | } | ||
102 | return GNUNET_OK; | ||
103 | } | ||
104 | |||
105 | |||
106 | /* end of pq/pq_exec.c */ | ||