diff options
author | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2017-06-23 20:39:27 +0200 |
---|---|---|
committer | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2017-06-23 20:39:27 +0200 |
commit | 2d81d6d8217b9aa08139bd66d14c597705568920 (patch) | |
tree | 197017d2b8498a79700ef6e00160b80ae749222e /src/pq | |
parent | ad5b64d8ef992d7281501297b547095569d8f0fb (diff) | |
parent | d7547ab18052726a69ee088876389a18798e03a8 (diff) | |
download | gnunet-2d81d6d8217b9aa08139bd66d14c597705568920.tar.gz gnunet-2d81d6d8217b9aa08139bd66d14c597705568920.zip |
Merge remote-tracking branch 'origin/master' into credentials
Diffstat (limited to 'src/pq')
-rw-r--r-- | src/pq/Makefile.am | 4 | ||||
-rw-r--r-- | src/pq/pq_connect.c | 127 | ||||
-rw-r--r-- | src/pq/pq_eval.c | 281 | ||||
-rw-r--r-- | src/pq/pq_exec.c | 106 | ||||
-rw-r--r-- | src/pq/pq_prepare.c | 93 |
5 files changed, 611 insertions, 0 deletions
diff --git a/src/pq/Makefile.am b/src/pq/Makefile.am index 8bb0a0132..d0c71703b 100644 --- a/src/pq/Makefile.am +++ b/src/pq/Makefile.am | |||
@@ -15,6 +15,10 @@ endif | |||
15 | 15 | ||
16 | libgnunetpq_la_SOURCES = \ | 16 | libgnunetpq_la_SOURCES = \ |
17 | pq.c \ | 17 | pq.c \ |
18 | pq_connect.c \ | ||
19 | pq_eval.c \ | ||
20 | pq_exec.c \ | ||
21 | pq_prepare.c \ | ||
18 | pq_query_helper.c \ | 22 | pq_query_helper.c \ |
19 | pq_result_helper.c | 23 | pq_result_helper.c |
20 | libgnunetpq_la_LIBADD = -lpq \ | 24 | libgnunetpq_la_LIBADD = -lpq \ |
diff --git a/src/pq/pq_connect.c b/src/pq/pq_connect.c new file mode 100644 index 000000000..99ad06485 --- /dev/null +++ b/src/pq/pq_connect.c | |||
@@ -0,0 +1,127 @@ | |||
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_connect.c | ||
18 | * @brief functions to connect to libpq (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 | * Function called by libpq whenever it wants to log something. | ||
28 | * We already log whenever we care, so this function does nothing | ||
29 | * and merely exists to silence the libpq logging. | ||
30 | * | ||
31 | * @param arg the SQL connection that was used | ||
32 | * @param res information about some libpq event | ||
33 | */ | ||
34 | static void | ||
35 | pq_notice_receiver_cb (void *arg, | ||
36 | const PGresult *res) | ||
37 | { | ||
38 | /* do nothing, intentionally */ | ||
39 | } | ||
40 | |||
41 | |||
42 | /** | ||
43 | * Function called by libpq whenever it wants to log something. | ||
44 | * We log those using the Taler logger. | ||
45 | * | ||
46 | * @param arg the SQL connection that was used | ||
47 | * @param message information about some libpq event | ||
48 | */ | ||
49 | static void | ||
50 | pq_notice_processor_cb (void *arg, | ||
51 | const char *message) | ||
52 | { | ||
53 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, | ||
54 | "pq", | ||
55 | "%s", | ||
56 | message); | ||
57 | } | ||
58 | |||
59 | |||
60 | /** | ||
61 | * Create a connection to the Postgres database using @a config_str | ||
62 | * for the configuration. Initialize logging via GNUnet's log | ||
63 | * routines and disable Postgres's logger. | ||
64 | * | ||
65 | * @param config_str configuration to use | ||
66 | * @return NULL on error | ||
67 | */ | ||
68 | PGconn * | ||
69 | GNUNET_PQ_connect (const char *config_str) | ||
70 | { | ||
71 | PGconn *conn; | ||
72 | |||
73 | conn = PQconnectdb (config_str); | ||
74 | if ( (NULL == conn) || | ||
75 | (CONNECTION_OK != | ||
76 | PQstatus (conn)) ) | ||
77 | { | ||
78 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
79 | "pq", | ||
80 | "Database connection to '%s' failed: %s\n", | ||
81 | config_str, | ||
82 | (NULL != conn) ? | ||
83 | PQerrorMessage (conn) | ||
84 | : "PQconnectdb returned NULL"); | ||
85 | if (NULL != conn) | ||
86 | PQfinish (conn); | ||
87 | return NULL; | ||
88 | } | ||
89 | PQsetNoticeReceiver (conn, | ||
90 | &pq_notice_receiver_cb, | ||
91 | conn); | ||
92 | PQsetNoticeProcessor (conn, | ||
93 | &pq_notice_processor_cb, | ||
94 | conn); | ||
95 | return conn; | ||
96 | } | ||
97 | |||
98 | |||
99 | /** | ||
100 | * Connect to a postgres database using the configuration | ||
101 | * option "CONFIG" in @a section. | ||
102 | * | ||
103 | * @param cfg configuration | ||
104 | * @param section configuration section to use to get Postgres configuration options | ||
105 | * @return the postgres handle, NULL on error | ||
106 | */ | ||
107 | PGconn * | ||
108 | GNUNET_PQ_connect_with_cfg (const struct GNUNET_CONFIGURATION_Handle * cfg, | ||
109 | const char *section) | ||
110 | { | ||
111 | PGconn *dbh; | ||
112 | char *conninfo; | ||
113 | |||
114 | /* Open database and precompile statements */ | ||
115 | if (GNUNET_OK != | ||
116 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
117 | section, | ||
118 | "CONFIG", | ||
119 | &conninfo)) | ||
120 | conninfo = NULL; | ||
121 | dbh = GNUNET_PQ_connect (conninfo == NULL ? "" : conninfo); | ||
122 | GNUNET_free_non_null (conninfo); | ||
123 | return dbh; | ||
124 | } | ||
125 | |||
126 | |||
127 | /* end of pq/pq_connect.c */ | ||
diff --git a/src/pq/pq_eval.c b/src/pq/pq_eval.c new file mode 100644 index 000000000..0f28aec7e --- /dev/null +++ b/src/pq/pq_eval.c | |||
@@ -0,0 +1,281 @@ | |||
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_eval.c | ||
18 | * @brief functions to execute SQL statements with arguments and/or results (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 | * Error code returned by Postgres for deadlock. | ||
28 | */ | ||
29 | #define PQ_DIAG_SQLSTATE_DEADLOCK "40P01" | ||
30 | |||
31 | /** | ||
32 | * Error code returned by Postgres for uniqueness violation. | ||
33 | */ | ||
34 | #define PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION "23505" | ||
35 | |||
36 | /** | ||
37 | * Error code returned by Postgres on serialization failure. | ||
38 | */ | ||
39 | #define PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE "40001" | ||
40 | |||
41 | |||
42 | /** | ||
43 | * Check the @a result's error code to see what happened. | ||
44 | * Also logs errors. | ||
45 | * | ||
46 | * @param connection connection to execute the statement in | ||
47 | * @param statement_name name of the statement that created @a result | ||
48 | * @param result result to check | ||
49 | * @return status code from the result, mapping PQ status | ||
50 | * codes to `enum GNUNET_DB_QueryStatus`. Never | ||
51 | * returns positive values as this function does | ||
52 | * not look at the result set. | ||
53 | * @deprecated (low level, let's see if we can do with just the high-level functions) | ||
54 | */ | ||
55 | enum GNUNET_DB_QueryStatus | ||
56 | GNUNET_PQ_eval_result (PGconn *connection, | ||
57 | const char *statement_name, | ||
58 | PGresult *result) | ||
59 | { | ||
60 | ExecStatusType est; | ||
61 | |||
62 | est = PQresultStatus (result); | ||
63 | if ( (PGRES_COMMAND_OK != est) && | ||
64 | (PGRES_TUPLES_OK != est) ) | ||
65 | { | ||
66 | const char *sqlstate; | ||
67 | |||
68 | sqlstate = PQresultErrorField (result, | ||
69 | PG_DIAG_SQLSTATE); | ||
70 | if (NULL == sqlstate) | ||
71 | { | ||
72 | /* very unexpected... */ | ||
73 | GNUNET_break (0); | ||
74 | return GNUNET_DB_STATUS_HARD_ERROR; | ||
75 | } | ||
76 | if ( (0 == strcmp (sqlstate, | ||
77 | PQ_DIAG_SQLSTATE_DEADLOCK)) || | ||
78 | (0 == strcmp (sqlstate, | ||
79 | PQ_DIAG_SQLSTATE_SERIALIZATION_FAILURE)) ) | ||
80 | { | ||
81 | /* These two can be retried and have a fair chance of working | ||
82 | the next time */ | ||
83 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, | ||
84 | "pq", | ||
85 | "Query `%s' failed with result: %s/%s/%s/%s/%s\n", | ||
86 | statement_name, | ||
87 | PQresultErrorField (result, | ||
88 | PG_DIAG_MESSAGE_PRIMARY), | ||
89 | PQresultErrorField (result, | ||
90 | PG_DIAG_MESSAGE_DETAIL), | ||
91 | PQresultErrorMessage (result), | ||
92 | PQresStatus (PQresultStatus (result)), | ||
93 | PQerrorMessage (connection)); | ||
94 | return GNUNET_DB_STATUS_SOFT_ERROR; | ||
95 | } | ||
96 | if (0 == strcmp (sqlstate, | ||
97 | PQ_DIAG_SQLSTATE_UNIQUE_VIOLATION)) | ||
98 | { | ||
99 | /* Likely no need to retry, INSERT of "same" data. */ | ||
100 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
101 | "pq", | ||
102 | "Query `%s' failed with unique violation: %s/%s/%s/%s/%s\n", | ||
103 | statement_name, | ||
104 | PQresultErrorField (result, | ||
105 | PG_DIAG_MESSAGE_PRIMARY), | ||
106 | PQresultErrorField (result, | ||
107 | PG_DIAG_MESSAGE_DETAIL), | ||
108 | PQresultErrorMessage (result), | ||
109 | PQresStatus (PQresultStatus (result)), | ||
110 | PQerrorMessage (connection)); | ||
111 | return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; | ||
112 | } | ||
113 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
114 | "pq", | ||
115 | "Query `%s' failed with result: %s/%s/%s/%s/%s\n", | ||
116 | statement_name, | ||
117 | PQresultErrorField (result, | ||
118 | PG_DIAG_MESSAGE_PRIMARY), | ||
119 | PQresultErrorField (result, | ||
120 | PG_DIAG_MESSAGE_DETAIL), | ||
121 | PQresultErrorMessage (result), | ||
122 | PQresStatus (PQresultStatus (result)), | ||
123 | PQerrorMessage (connection)); | ||
124 | return GNUNET_DB_STATUS_HARD_ERROR; | ||
125 | } | ||
126 | return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; | ||
127 | } | ||
128 | |||
129 | |||
130 | /** | ||
131 | * Execute a named prepared @a statement that is NOT a SELECT | ||
132 | * statement in @a connnection using the given @a params. Returns the | ||
133 | * resulting session state. | ||
134 | * | ||
135 | * @param connection connection to execute the statement in | ||
136 | * @param statement_name name of the statement | ||
137 | * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated) | ||
138 | * @return status code from the result, mapping PQ status | ||
139 | * codes to `enum GNUNET_DB_QueryStatus`. If the | ||
140 | * statement was a DELETE or UPDATE statement, the | ||
141 | * number of affected rows is returned.; if the | ||
142 | * statment was an INSERT statement, and no row | ||
143 | * was added due to a UNIQUE violation, we return | ||
144 | * zero; if INSERT was successful, we return one. | ||
145 | */ | ||
146 | enum GNUNET_DB_QueryStatus | ||
147 | GNUNET_PQ_eval_prepared_non_select (PGconn *connection, | ||
148 | const char *statement_name, | ||
149 | const struct GNUNET_PQ_QueryParam *params) | ||
150 | { | ||
151 | PGresult *result; | ||
152 | enum GNUNET_DB_QueryStatus qs; | ||
153 | |||
154 | result = GNUNET_PQ_exec_prepared (connection, | ||
155 | statement_name, | ||
156 | params); | ||
157 | qs = GNUNET_PQ_eval_result (connection, | ||
158 | statement_name, | ||
159 | result); | ||
160 | if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) | ||
161 | { | ||
162 | const char *tuples; | ||
163 | |||
164 | /* What an awful API, this function really does return a string */ | ||
165 | tuples = PQcmdTuples (result); | ||
166 | if (NULL != tuples) | ||
167 | qs = strtol (tuples, NULL, 10); | ||
168 | } | ||
169 | PQclear (result); | ||
170 | return qs; | ||
171 | } | ||
172 | |||
173 | |||
174 | /** | ||
175 | * Execute a named prepared @a statement that is a SELECT statement | ||
176 | * which may return multiple results in @a connection using the given | ||
177 | * @a params. Call @a rh with the results. Returns the query | ||
178 | * status including the number of results given to @a rh (possibly zero). | ||
179 | * @a rh will not have been called if the return value is negative. | ||
180 | * | ||
181 | * @param connection connection to execute the statement in | ||
182 | * @param statement_name name of the statement | ||
183 | * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated) | ||
184 | * @param rh function to call with the result set, NULL to ignore | ||
185 | * @param rh_cls closure to pass to @a rh | ||
186 | * @return status code from the result, mapping PQ status | ||
187 | * codes to `enum GNUNET_DB_QueryStatus`. | ||
188 | */ | ||
189 | enum GNUNET_DB_QueryStatus | ||
190 | GNUNET_PQ_eval_prepared_multi_select (PGconn *connection, | ||
191 | const char *statement_name, | ||
192 | const struct GNUNET_PQ_QueryParam *params, | ||
193 | GNUNET_PQ_PostgresResultHandler rh, | ||
194 | void *rh_cls) | ||
195 | { | ||
196 | PGresult *result; | ||
197 | enum GNUNET_DB_QueryStatus qs; | ||
198 | unsigned int ret; | ||
199 | |||
200 | result = GNUNET_PQ_exec_prepared (connection, | ||
201 | statement_name, | ||
202 | params); | ||
203 | qs = GNUNET_PQ_eval_result (connection, | ||
204 | statement_name, | ||
205 | result); | ||
206 | if (qs < 0) | ||
207 | { | ||
208 | PQclear (result); | ||
209 | return qs; | ||
210 | } | ||
211 | ret = PQntuples (result); | ||
212 | if (NULL != rh) | ||
213 | rh (rh_cls, | ||
214 | result, | ||
215 | ret); | ||
216 | PQclear (result); | ||
217 | return ret; | ||
218 | } | ||
219 | |||
220 | |||
221 | /** | ||
222 | * Execute a named prepared @a statement that is a SELECT statement | ||
223 | * which must return a single result in @a connection using the given | ||
224 | * @a params. Stores the result (if any) in @a rs, which the caller | ||
225 | * must then clean up using #GNUNET_PQ_cleanup_result() if the return | ||
226 | * value was #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT. Returns the | ||
227 | * resulting session status. | ||
228 | * | ||
229 | * @param connection connection to execute the statement in | ||
230 | * @param statement_name name of the statement | ||
231 | * @param params parameters to give to the statement (#GNUNET_PQ_query_param_end-terminated) | ||
232 | * @param[in,out] rs result specification to use for storing the result of the query | ||
233 | * @return status code from the result, mapping PQ status | ||
234 | * codes to `enum GNUNET_DB_QueryStatus`. | ||
235 | */ | ||
236 | enum GNUNET_DB_QueryStatus | ||
237 | GNUNET_PQ_eval_prepared_singleton_select (PGconn *connection, | ||
238 | const char *statement_name, | ||
239 | const struct GNUNET_PQ_QueryParam *params, | ||
240 | struct GNUNET_PQ_ResultSpec *rs) | ||
241 | { | ||
242 | PGresult *result; | ||
243 | enum GNUNET_DB_QueryStatus qs; | ||
244 | |||
245 | result = GNUNET_PQ_exec_prepared (connection, | ||
246 | statement_name, | ||
247 | params); | ||
248 | qs = GNUNET_PQ_eval_result (connection, | ||
249 | statement_name, | ||
250 | result); | ||
251 | if (qs < 0) | ||
252 | { | ||
253 | PQclear (result); | ||
254 | return qs; | ||
255 | } | ||
256 | if (0 == PQntuples (result)) | ||
257 | { | ||
258 | PQclear (result); | ||
259 | return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; | ||
260 | } | ||
261 | if (1 != PQntuples (result)) | ||
262 | { | ||
263 | /* more than one result, but there must be at most one */ | ||
264 | GNUNET_break (0); | ||
265 | PQclear (result); | ||
266 | return GNUNET_DB_STATUS_HARD_ERROR; | ||
267 | } | ||
268 | if (GNUNET_OK != | ||
269 | GNUNET_PQ_extract_result (result, | ||
270 | rs, | ||
271 | 0)) | ||
272 | { | ||
273 | PQclear (result); | ||
274 | return GNUNET_DB_STATUS_HARD_ERROR; | ||
275 | } | ||
276 | PQclear (result); | ||
277 | return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; | ||
278 | } | ||
279 | |||
280 | |||
281 | /* end of pq/pq_eval.c */ | ||
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 */ | ||
diff --git a/src/pq/pq_prepare.c b/src/pq/pq_prepare.c new file mode 100644 index 000000000..612d6df7c --- /dev/null +++ b/src/pq/pq_prepare.c | |||
@@ -0,0 +1,93 @@ | |||
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_prepare.c | ||
18 | * @brief functions to connect to libpq (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_PreparedStatement`. | ||
28 | * | ||
29 | * @param name name of the statement | ||
30 | * @param sql actual SQL statement | ||
31 | * @param num_args number of arguments in the statement | ||
32 | * @return initialized struct | ||
33 | */ | ||
34 | struct GNUNET_PQ_PreparedStatement | ||
35 | GNUNET_PQ_make_prepare (const char *name, | ||
36 | const char *sql, | ||
37 | unsigned int num_args) | ||
38 | { | ||
39 | struct GNUNET_PQ_PreparedStatement ps = { | ||
40 | .name = name, | ||
41 | .sql = sql, | ||
42 | .num_arguments = num_args | ||
43 | }; | ||
44 | |||
45 | return ps; | ||
46 | } | ||
47 | |||
48 | |||
49 | /** | ||
50 | * Request creation of prepared statements @a ps from Postgres. | ||
51 | * | ||
52 | * @param connection connection to prepare the statements for | ||
53 | * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared | ||
54 | * statements. | ||
55 | * @return #GNUNET_OK on success, | ||
56 | * #GNUNET_SYSERR on error | ||
57 | */ | ||
58 | int | ||
59 | GNUNET_PQ_prepare_statements (PGconn *connection, | ||
60 | const struct GNUNET_PQ_PreparedStatement *ps) | ||
61 | { | ||
62 | for (unsigned int i=0;NULL != ps[i].name;i++) | ||
63 | { | ||
64 | PGresult *ret; | ||
65 | |||
66 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
67 | "pq", | ||
68 | "Preparing SQL statement `%s' as `%s'\n", | ||
69 | ps[i].sql, | ||
70 | ps[i].name); | ||
71 | ret = PQprepare (connection, | ||
72 | ps[i].name, | ||
73 | ps[i].sql, | ||
74 | ps[i].num_arguments, | ||
75 | NULL); | ||
76 | if (PGRES_COMMAND_OK != PQresultStatus (ret)) | ||
77 | { | ||
78 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
79 | "pq", | ||
80 | _("PQprepare (`%s' as `%s') failed with error: %s\n"), | ||
81 | ps[i].sql, | ||
82 | ps[i].name, | ||
83 | PQerrorMessage (connection)); | ||
84 | PQclear (ret); | ||
85 | return GNUNET_SYSERR; | ||
86 | } | ||
87 | PQclear (ret); | ||
88 | } | ||
89 | return GNUNET_OK; | ||
90 | } | ||
91 | |||
92 | |||
93 | /* end of pq/pq_prepare.c */ | ||