aboutsummaryrefslogtreecommitdiff
path: root/src/lib/pq/pq_prepare.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/pq/pq_prepare.c')
-rw-r--r--src/lib/pq/pq_prepare.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/src/lib/pq/pq_prepare.c b/src/lib/pq/pq_prepare.c
new file mode 100644
index 000000000..b4292dea3
--- /dev/null
+++ b/src/lib/pq/pq_prepare.c
@@ -0,0 +1,125 @@
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_prepare.c
22 * @brief functions to connect to libpq (PostGres)
23 * @author Christian Grothoff
24 */
25#include "platform.h"
26#include "pq.h"
27
28
29struct GNUNET_PQ_PreparedStatement
30GNUNET_PQ_make_prepare (const char *name,
31 const char *sql)
32{
33 struct GNUNET_PQ_PreparedStatement ps = {
34 .name = name,
35 .sql = sql
36 };
37
38 return ps;
39}
40
41
42enum GNUNET_GenericReturnValue
43GNUNET_PQ_prepare_once (struct GNUNET_PQ_Context *db,
44 const struct GNUNET_PQ_PreparedStatement *ps)
45{
46 for (unsigned int i = 0; NULL != ps[i].name; i++)
47 {
48 PGresult *ret;
49
50 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
51 "pq",
52 "Preparing SQL statement `%s' as `%s'\n",
53 ps[i].sql,
54 ps[i].name);
55 ret = PQprepare (db->conn,
56 ps[i].name,
57 ps[i].sql,
58 0,
59 NULL);
60 if (PGRES_COMMAND_OK != PQresultStatus (ret))
61 {
62 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
63 "pq",
64 "PQprepare (`%s' as `%s') failed with error: %s\n",
65 ps[i].sql,
66 ps[i].name,
67 PQerrorMessage (db->conn));
68 PQclear (ret);
69 ret = PQdescribePrepared (db->conn,
70 ps[i].name);
71 if (PGRES_COMMAND_OK != PQresultStatus (ret))
72 {
73 PQclear (ret);
74 return GNUNET_SYSERR;
75 }
76 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
77 "pq",
78 "Statement `%s' already known. Ignoring the issue in the hope that you are using connection pooling...\n",
79 ps[i].name);
80 }
81 PQclear (ret);
82 }
83 return GNUNET_OK;
84}
85
86
87enum GNUNET_GenericReturnValue
88GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
89 const struct GNUNET_PQ_PreparedStatement *ps)
90{
91 if (db->ps != ps)
92 {
93 /* add 'ps' to list db->ps of prepared statements to run on reconnect! */
94 unsigned int nlen = 0; /* length of 'ps' array */
95 unsigned int xlen;
96 struct GNUNET_PQ_PreparedStatement *rps; /* combined array */
97
98 while (NULL != ps[nlen].name)
99 nlen++;
100 xlen = nlen + db->ps_off;
101 if (xlen > db->ps_len)
102 {
103 xlen = 2 * xlen + 1;
104 rps = GNUNET_new_array (xlen,
105 struct GNUNET_PQ_PreparedStatement);
106 if (NULL != db->ps)
107 memcpy (rps,
108 db->ps,
109 db->ps_off * sizeof (struct GNUNET_PQ_PreparedStatement));
110 GNUNET_free (db->ps);
111 db->ps_len = xlen;
112 db->ps = rps;
113 }
114 memcpy (&db->ps[db->ps_off],
115 ps,
116 nlen * sizeof (struct GNUNET_PQ_PreparedStatement));
117 db->ps_off += nlen;
118 }
119
120 return GNUNET_PQ_prepare_once (db,
121 ps);
122}
123
124
125/* end of pq/pq_prepare.c */