GNUnet 0.28.1-dev.4-2-gb857fff5c
 
Loading...
Searching...
No Matches
pq_prepare.c
Go to the documentation of this file.
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 */
25#include "platform.h"
26#include "pq.h"
27
28
31 const char *sql)
32{
34 .name = name,
35 .sql = sql
36 };
37
38 /* PostgreSQL truncates prepared statements at 63 characters,
39 assert this one is short enough or at least fail hard if
40 it is not. */
41 GNUNET_assert (strlen (name) < 64);
42 return ps;
43}
44
45
48 const char *sql)
49{
50 PGresult *ret;
51
53 "pq",
54 "Preparing SQL: %s\n",
55 sql);
56 ret = PQprepare (pg->conn,
57 "",
58 sql,
59 0,
60 NULL);
61 if (PGRES_COMMAND_OK != PQresultStatus (ret))
62 {
64 "pq",
65 "PQprepare (`%s') failed with error: %s\n",
66 sql,
67 PQerrorMessage (pg->conn));
68 PQclear (ret);
69 return GNUNET_SYSERR;
70 }
71 PQclear (ret);
72 return GNUNET_OK;
73}
74
75
78 const struct GNUNET_PQ_PreparedStatement *ps)
79{
80 for (unsigned int i = 0;
81 NULL != ps[i].name;
82 i++)
83 {
84 PGresult *ret;
85
87 "pq",
88 "Preparing SQL statement `%s' as `%s'\n",
89 ps[i].sql,
90 ps[i].name);
91 ret = PQprepare (db->conn,
92 ps[i].name,
93 ps[i].sql,
94 0,
95 NULL);
96 if (PGRES_COMMAND_OK != PQresultStatus (ret))
97 {
99 "pq",
100 "PQprepare (`%s' as `%s') failed with error: %s\n",
101 ps[i].sql,
102 ps[i].name,
103 PQerrorMessage (db->conn));
104 PQclear (ret);
105 ret = PQdescribePrepared (db->conn,
106 ps[i].name);
107 if (PGRES_COMMAND_OK != PQresultStatus (ret))
108 {
109 PQclear (ret);
110 return GNUNET_SYSERR;
111 }
113 "pq",
114 "Statement `%s' already known. Ignoring the issue in the hope that you are using connection pooling...\n",
115 ps[i].name);
116 }
117 PQclear (ret);
118 }
119 return GNUNET_OK;
120}
121
122
123/* end of pq/pq_prepare.c */
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
static char * name
Name (label) of the records to list.
static struct GNUNET_FS_DirectoryBuilder * db
#define GNUNET_log_from(kind, comp,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_INFO
shared internal data structures of libgnunetpq
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_anon(struct GNUNET_PQ_Context *pg, const char *sql)
Prepares SQL statement sql under no name ("") for connection pg.
Definition pq_prepare.c:47
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres.
Definition pq_prepare.c:77
struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare(const char *name, const char *sql)
Create a struct GNUNET_PQ_PreparedStatement.
Definition pq_prepare.c:30
Handle to Postgres database.
Definition pq.h:36
PGconn * conn
Actual connection.
Definition pq.h:40
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
const char * sql
Actual SQL statement.