summaryrefslogtreecommitdiff
path: root/src/pq/pq.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq/pq.c')
-rw-r--r--src/pq/pq.c156
1 files changed, 78 insertions, 78 deletions
diff --git a/src/pq/pq.c b/src/pq/pq.c
index df5ef17ad..a581b4277 100644
--- a/src/pq/pq.c
+++ b/src/pq/pq.c
@@ -1,22 +1,22 @@
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 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
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file pq/pq.c 21 * @file pq/pq.c
22 * @brief helper functions for libpq (PostGres) interactions 22 * @brief helper functions for libpq (PostGres) interactions
@@ -38,20 +38,20 @@
38 * @return postgres result 38 * @return postgres result
39 */ 39 */
40PGresult * 40PGresult *
41GNUNET_PQ_exec_prepared (PGconn *db_conn, 41GNUNET_PQ_exec_prepared(PGconn *db_conn,
42 const char *name, 42 const char *name,
43 const struct GNUNET_PQ_QueryParam *params) 43 const struct GNUNET_PQ_QueryParam *params)
44{ 44{
45 unsigned int len; 45 unsigned int len;
46 unsigned int i; 46 unsigned int i;
47 47
48 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 48 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
49 "Running prepared statement `%s' on %p\n", 49 "Running prepared statement `%s' on %p\n",
50 name, 50 name,
51 db_conn); 51 db_conn);
52 /* count the number of parameters */ 52 /* count the number of parameters */
53 len = 0; 53 len = 0;
54 for (i=0;0 != params[i].num_params;i++) 54 for (i = 0; 0 != params[i].num_params; i++)
55 len += params[i].num_params; 55 len += params[i].num_params;
56 56
57 /* new scope to allow stack allocation without alloca */ 57 /* new scope to allow stack allocation without alloca */
@@ -70,42 +70,42 @@ GNUNET_PQ_exec_prepared (PGconn *db_conn,
70 70
71 off = 0; 71 off = 0;
72 soff = 0; 72 soff = 0;
73 for (i=0;0 != params[i].num_params;i++) 73 for (i = 0; 0 != params[i].num_params; i++)
74 {
75 const struct GNUNET_PQ_QueryParam *x = &params[i];
76
77 ret = x->conv (x->conv_cls,
78 x->data,
79 x->size,
80 &param_values[off],
81 &param_lengths[off],
82 &param_formats[off],
83 x->num_params,
84 &scratch[soff],
85 len - soff);
86 if (ret < 0)
87 { 74 {
88 for (off = 0; off < soff; off++) 75 const struct GNUNET_PQ_QueryParam *x = &params[i];
89 GNUNET_free (scratch[off]); 76
90 return NULL; 77 ret = x->conv(x->conv_cls,
78 x->data,
79 x->size,
80 &param_values[off],
81 &param_lengths[off],
82 &param_formats[off],
83 x->num_params,
84 &scratch[soff],
85 len - soff);
86 if (ret < 0)
87 {
88 for (off = 0; off < soff; off++)
89 GNUNET_free(scratch[off]);
90 return NULL;
91 }
92 soff += ret;
93 off += x->num_params;
91 } 94 }
92 soff += ret; 95 GNUNET_assert(off == len);
93 off += x->num_params; 96 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
94 } 97 "pq",
95 GNUNET_assert (off == len); 98 "Executing prepared SQL statement `%s'\n",
96 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 99 name);
97 "pq", 100 res = PQexecPrepared(db_conn,
98 "Executing prepared SQL statement `%s'\n", 101 name,
99 name); 102 len,
100 res = PQexecPrepared (db_conn, 103 (const char **)param_values,
101 name, 104 param_lengths,
102 len, 105 param_formats,
103 (const char **) param_values, 106 1);
104 param_lengths,
105 param_formats,
106 1);
107 for (off = 0; off < soff; off++) 107 for (off = 0; off < soff; off++)
108 GNUNET_free (scratch[off]); 108 GNUNET_free(scratch[off]);
109 return res; 109 return res;
110 } 110 }
111} 111}
@@ -118,14 +118,14 @@ GNUNET_PQ_exec_prepared (PGconn *db_conn,
118 * @param rs reult specification to clean up 118 * @param rs reult specification to clean up
119 */ 119 */
120void 120void
121GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs) 121GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs)
122{ 122{
123 unsigned int i; 123 unsigned int i;
124 124
125 for (i=0; NULL != rs[i].conv; i++) 125 for (i = 0; NULL != rs[i].conv; i++)
126 if (NULL != rs[i].cleaner) 126 if (NULL != rs[i].cleaner)
127 rs[i].cleaner (rs[i].cls, 127 rs[i].cleaner(rs[i].cls,
128 rs[i].dst); 128 rs[i].dst);
129} 129}
130 130
131 131
@@ -141,32 +141,32 @@ GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs)
141 * #GNUNET_SYSERR if a result was invalid (non-existing field) 141 * #GNUNET_SYSERR if a result was invalid (non-existing field)
142 */ 142 */
143int 143int
144GNUNET_PQ_extract_result (PGresult *result, 144GNUNET_PQ_extract_result(PGresult *result,
145 struct GNUNET_PQ_ResultSpec *rs, 145 struct GNUNET_PQ_ResultSpec *rs,
146 int row) 146 int row)
147{ 147{
148 unsigned int i; 148 unsigned int i;
149 int ret; 149 int ret;
150 150
151 for (i=0; NULL != rs[i].conv; i++) 151 for (i = 0; NULL != rs[i].conv; i++)
152 {
153 struct GNUNET_PQ_ResultSpec *spec;
154
155 spec = &rs[i];
156 ret = spec->conv (spec->cls,
157 result,
158 row,
159 spec->fname,
160 &spec->dst_size,
161 spec->dst);
162 if (GNUNET_OK != ret)
163 { 152 {
164 GNUNET_PQ_cleanup_result (rs); 153 struct GNUNET_PQ_ResultSpec *spec;
165 return GNUNET_SYSERR; 154
155 spec = &rs[i];
156 ret = spec->conv(spec->cls,
157 result,
158 row,
159 spec->fname,
160 &spec->dst_size,
161 spec->dst);
162 if (GNUNET_OK != ret)
163 {
164 GNUNET_PQ_cleanup_result(rs);
165 return GNUNET_SYSERR;
166 }
167 if (NULL != spec->result_size)
168 *spec->result_size = spec->dst_size;
166 } 169 }
167 if (NULL != spec->result_size)
168 *spec->result_size = spec->dst_size;
169 }
170 return GNUNET_OK; 170 return GNUNET_OK;
171} 171}
172 172