diff options
Diffstat (limited to 'src/pq/pq_prepare.c')
-rw-r--r-- | src/pq/pq_prepare.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/pq/pq_prepare.c b/src/pq/pq_prepare.c new file mode 100644 index 000000000..f533cb564 --- /dev/null +++ b/src/pq/pq_prepare.c | |||
@@ -0,0 +1,92 @@ | |||
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 | } | ||
88 | return GNUNET_OK; | ||
89 | } | ||
90 | |||
91 | |||
92 | /* end of pq/pq_prepare.c */ | ||