aboutsummaryrefslogtreecommitdiff
path: root/src/pq/pq_prepare.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq/pq_prepare.c')
-rw-r--r--src/pq/pq_prepare.c39
1 files changed, 32 insertions, 7 deletions
diff --git a/src/pq/pq_prepare.c b/src/pq/pq_prepare.c
index 0facf100f..b7003fb69 100644
--- a/src/pq/pq_prepare.c
+++ b/src/pq/pq_prepare.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V. 3 Copyright (C) 2017, 2019 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 6 under the terms of the GNU Affero General Public License as published
@@ -23,8 +23,7 @@
23 * @author Christian Grothoff 23 * @author Christian Grothoff
24 */ 24 */
25#include "platform.h" 25#include "platform.h"
26#include "gnunet_util_lib.h" 26#include "pq.h"
27#include "gnunet_pq_lib.h"
28 27
29 28
30/** 29/**
@@ -53,16 +52,42 @@ GNUNET_PQ_make_prepare (const char *name,
53/** 52/**
54 * Request creation of prepared statements @a ps from Postgres. 53 * Request creation of prepared statements @a ps from Postgres.
55 * 54 *
56 * @param connection connection to prepare the statements for 55 * @param db database to prepare the statements for
57 * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared 56 * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
58 * statements. 57 * statements.
59 * @return #GNUNET_OK on success, 58 * @return #GNUNET_OK on success,
60 * #GNUNET_SYSERR on error 59 * #GNUNET_SYSERR on error
61 */ 60 */
62int 61int
63GNUNET_PQ_prepare_statements (PGconn *connection, 62GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
64 const struct GNUNET_PQ_PreparedStatement *ps) 63 const struct GNUNET_PQ_PreparedStatement *ps)
65{ 64{
65 if (db->ps != ps)
66 {
67 /* add 'ps' to list db->ps of prepared statements to run on reconnect! */
68 unsigned int olen = 0; /* length of existing 'db->ps' array */
69 unsigned int nlen = 0; /* length of 'ps' array */
70 struct GNUNET_PQ_PreparedStatement *rps; /* combined array */
71
72 if (NULL != db->ps)
73 while (NULL != db->ps[olen].name)
74 olen++;
75 while (NULL != ps[nlen].name)
76 nlen++;
77 rps = GNUNET_new_array (olen + nlen + 1,
78 struct GNUNET_PQ_PreparedStatement);
79 if (NULL != db->ps)
80 memcpy (rps,
81 db->ps,
82 olen * sizeof (struct GNUNET_PQ_PreparedStatement));
83 memcpy (&rps[olen],
84 ps,
85 nlen * sizeof (struct GNUNET_PQ_PreparedStatement));
86 GNUNET_free_non_null (db->ps);
87 db->ps = rps;
88 }
89
90 /* actually prepare statements */
66 for (unsigned int i = 0; NULL != ps[i].name; i++) 91 for (unsigned int i = 0; NULL != ps[i].name; i++)
67 { 92 {
68 PGresult *ret; 93 PGresult *ret;
@@ -72,7 +97,7 @@ GNUNET_PQ_prepare_statements (PGconn *connection,
72 "Preparing SQL statement `%s' as `%s'\n", 97 "Preparing SQL statement `%s' as `%s'\n",
73 ps[i].sql, 98 ps[i].sql,
74 ps[i].name); 99 ps[i].name);
75 ret = PQprepare (connection, 100 ret = PQprepare (db->conn,
76 ps[i].name, 101 ps[i].name,
77 ps[i].sql, 102 ps[i].sql,
78 ps[i].num_arguments, 103 ps[i].num_arguments,
@@ -84,7 +109,7 @@ GNUNET_PQ_prepare_statements (PGconn *connection,
84 _ ("PQprepare (`%s' as `%s') failed with error: %s\n"), 109 _ ("PQprepare (`%s' as `%s') failed with error: %s\n"),
85 ps[i].sql, 110 ps[i].sql,
86 ps[i].name, 111 ps[i].name,
87 PQerrorMessage (connection)); 112 PQerrorMessage (db->conn));
88 PQclear (ret); 113 PQclear (ret);
89 return GNUNET_SYSERR; 114 return GNUNET_SYSERR;
90 } 115 }