aboutsummaryrefslogtreecommitdiff
path: root/src/pq/pq.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq/pq.c')
-rw-r--r--src/pq/pq.c43
1 files changed, 25 insertions, 18 deletions
diff --git a/src/pq/pq.c b/src/pq/pq.c
index 7e97c8f72..d2b9a6174 100644
--- a/src/pq/pq.c
+++ b/src/pq/pq.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V. 3 Copyright (C) 2014, 2015, 2016, 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
@@ -25,33 +25,30 @@
25 * @author Christian Grothoff 25 * @author Christian Grothoff
26 */ 26 */
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_util_lib.h" 28#include "pq.h"
29#include "gnunet_pq_lib.h"
30
31 29
32/** 30/**
33 * Execute a prepared statement. 31 * Execute a prepared statement.
34 * 32 *
35 * @param db_conn database connection 33 * @param db database handle
36 * @param name name of the prepared statement 34 * @param name name of the prepared statement
37 * @param params parameters to the statement 35 * @param params parameters to the statement
38 * @return postgres result 36 * @return postgres result
39 */ 37 */
40PGresult * 38PGresult *
41GNUNET_PQ_exec_prepared (PGconn *db_conn, 39GNUNET_PQ_exec_prepared (struct GNUNET_PQ_Context *db,
42 const char *name, 40 const char *name,
43 const struct GNUNET_PQ_QueryParam *params) 41 const struct GNUNET_PQ_QueryParam *params)
44{ 42{
45 unsigned int len; 43 unsigned int len;
46 unsigned int i;
47 44
48 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 45 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
49 "Running prepared statement `%s' on %p\n", 46 "Running prepared statement `%s' on %p\n",
50 name, 47 name,
51 db_conn); 48 db);
52 /* count the number of parameters */ 49 /* count the number of parameters */
53 len = 0; 50 len = 0;
54 for (i = 0; 0 != params[i].num_params; i++) 51 for (unsigned int i = 0; 0 != params[i].num_params; i++)
55 len += params[i].num_params; 52 len += params[i].num_params;
56 53
57 /* new scope to allow stack allocation without alloca */ 54 /* new scope to allow stack allocation without alloca */
@@ -67,10 +64,11 @@ GNUNET_PQ_exec_prepared (PGconn *db_conn,
67 unsigned int soff; 64 unsigned int soff;
68 PGresult *res; 65 PGresult *res;
69 int ret; 66 int ret;
67 ConnStatusType status;
70 68
71 off = 0; 69 off = 0;
72 soff = 0; 70 soff = 0;
73 for (i = 0; 0 != params[i].num_params; i++) 71 for (unsigned int i = 0; 0 != params[i].num_params; i++)
74 { 72 {
75 const struct GNUNET_PQ_QueryParam *x = &params[i]; 73 const struct GNUNET_PQ_QueryParam *x = &params[i];
76 74
@@ -97,13 +95,24 @@ GNUNET_PQ_exec_prepared (PGconn *db_conn,
97 "pq", 95 "pq",
98 "Executing prepared SQL statement `%s'\n", 96 "Executing prepared SQL statement `%s'\n",
99 name); 97 name);
100 res = PQexecPrepared (db_conn, 98 res = PQexecPrepared (db->conn,
101 name, 99 name,
102 len, 100 len,
103 (const char **) param_values, 101 (const char **) param_values,
104 param_lengths, 102 param_lengths,
105 param_formats, 103 param_formats,
106 1); 104 1);
105 if ( (PGRES_COMMAND_OK != PQresultStatus (res)) &&
106 (CONNECTION_OK != (status = PQstatus (db->conn))) )
107 {
108 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
109 "pq",
110 "Database disconnected on SQL statement `%s' (reconnecting)\n",
111 name);
112 GNUNET_PQ_reconnect (db);
113 res = NULL;
114 }
115
107 for (off = 0; off < soff; off++) 116 for (off = 0; off < soff; off++)
108 GNUNET_free (scratch[off]); 117 GNUNET_free (scratch[off]);
109 return res; 118 return res;
@@ -120,9 +129,7 @@ GNUNET_PQ_exec_prepared (PGconn *db_conn,
120void 129void
121GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs) 130GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs)
122{ 131{
123 unsigned int i; 132 for (unsigned int i = 0; NULL != rs[i].conv; i++)
124
125 for (i = 0; NULL != rs[i].conv; i++)
126 if (NULL != rs[i].cleaner) 133 if (NULL != rs[i].cleaner)
127 rs[i].cleaner (rs[i].cls, 134 rs[i].cleaner (rs[i].cls,
128 rs[i].dst); 135 rs[i].dst);
@@ -145,12 +152,12 @@ GNUNET_PQ_extract_result (PGresult *result,
145 struct GNUNET_PQ_ResultSpec *rs, 152 struct GNUNET_PQ_ResultSpec *rs,
146 int row) 153 int row)
147{ 154{
148 unsigned int i; 155 if (NULL == result)
149 int ret; 156 return GNUNET_SYSERR;
150 157 for (unsigned int i = 0; NULL != rs[i].conv; i++)
151 for (i = 0; NULL != rs[i].conv; i++)
152 { 158 {
153 struct GNUNET_PQ_ResultSpec *spec; 159 struct GNUNET_PQ_ResultSpec *spec;
160 int ret;
154 161
155 spec = &rs[i]; 162 spec = &rs[i];
156 ret = spec->conv (spec->cls, 163 ret = spec->conv (spec->cls,