reduce loop counters to more practical levels
[oweals/gnunet.git] / src / pq / pq.c
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4
5   GNUnet is free software; you can redistribute it and/or modify it under the
6   terms of the GNU General Public License as published by the Free Software
7   Foundation; either version 3, or (at your option) any later version.
8
9   GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
12
13   You should have received a copy of the GNU General Public License along with
14   GNUnet; see the file COPYING.  If not, If not, see <http://www.gnu.org/licenses/>
15 */
16 /**
17  * @file pq/pq.c
18  * @brief helper functions for libpq (PostGres) interactions
19  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20  * @author Florian Dold
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_pq_lib.h"
26
27
28 /**
29  * Execute a prepared statement.
30  *
31  * @param db_conn database connection
32  * @param name name of the prepared statement
33  * @param params parameters to the statement
34  * @return postgres result
35  */
36 PGresult *
37 GNUNET_PQ_exec_prepared (PGconn *db_conn,
38                          const char *name,
39                          const struct GNUNET_PQ_QueryParam *params)
40 {
41   unsigned int len;
42   unsigned int i;
43
44   /* count the number of parameters */
45   len = 0;
46   for (i=0;0 != params[i].num_params;i++)
47     len += params[i].num_params;
48
49   /* new scope to allow stack allocation without alloca */
50   {
51     /* Scratch buffer for temporary storage */
52     void *scratch[len];
53     /* Parameter array we are building for the query */
54     void *param_values[len];
55     int param_lengths[len];
56     int param_formats[len];
57     unsigned int off;
58     /* How many entries in the scratch buffer are in use? */
59     unsigned int soff;
60     PGresult *res;
61     int ret;
62
63     off = 0;
64     soff = 0;
65     for (i=0;0 != params[i].num_params;i++)
66     {
67       const struct GNUNET_PQ_QueryParam *x = &params[i];
68
69       ret = x->conv (x->conv_cls,
70                      x->data,
71                      x->size,
72                      &param_values[off],
73                      &param_lengths[off],
74                      &param_formats[off],
75                      x->num_params,
76                      &scratch[soff],
77                      len - soff);
78       if (ret < 0)
79       {
80         for (off = 0; off < soff; off++)
81           GNUNET_free (scratch[off]);
82         return NULL;
83       }
84       soff += ret;
85       off += x->num_params;
86     }
87     GNUNET_assert (off == len);
88     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
89                      "pq",
90                      "Executing prepared SQL statement `%s'\n",
91                      name);
92     res = PQexecPrepared (db_conn,
93                           name,
94                           len,
95                           (const char **) param_values,
96                           param_lengths,
97                           param_formats,
98                           1);
99     for (off = 0; off < soff; off++)
100       GNUNET_free (scratch[off]);
101     return res;
102   }
103 }
104
105
106 /**
107  * Free all memory that was allocated in @a rs during
108  * #GNUNET_PQ_extract_result().
109  *
110  * @param rs reult specification to clean up
111  */
112 void
113 GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs)
114 {
115   unsigned int i;
116
117   for (i=0; NULL != rs[i].conv; i++)
118     if (NULL != rs[i].cleaner)
119       rs[i].cleaner (rs[i].cls,
120                      rs[i].dst);
121 }
122
123
124 /**
125  * Extract results from a query result according to the given
126  * specification.
127  *
128  * @param result result to process
129  * @param[in,out] rs result specification to extract for
130  * @param row row from the result to extract
131  * @return
132  *   #GNUNET_YES if all results could be extracted
133  *   #GNUNET_SYSERR if a result was invalid (non-existing field)
134  */
135 int
136 GNUNET_PQ_extract_result (PGresult *result,
137                           struct GNUNET_PQ_ResultSpec *rs,
138                           int row)
139 {
140   unsigned int i;
141   int ret;
142
143   for (i=0; NULL != rs[i].conv; i++)
144   {
145     struct GNUNET_PQ_ResultSpec *spec;
146
147     spec = &rs[i];
148     ret = spec->conv (spec->cls,
149                       result,
150                       row,
151                       spec->fname,
152                       &spec->dst_size,
153                       spec->dst);
154     if (GNUNET_OK != ret)
155     {
156       GNUNET_PQ_cleanup_result (rs);
157       return GNUNET_SYSERR;
158     }
159     if (NULL != spec->result_size)
160       *spec->result_size = spec->dst_size;
161   }
162   return GNUNET_OK;
163 }
164
165
166 /* end of pq/pq.c */