Link libgnunetblockgroup to libgnunetblock
[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 (GNUNET_ERROR_TYPE_DEBUG,
89                 "Executing prepared SQL statement `%s'\n",
90                 name);
91     res = PQexecPrepared (db_conn,
92                           name,
93                           len,
94                           (const char **) param_values,
95                           param_lengths,
96                           param_formats,
97                           1);
98     for (off = 0; off < soff; off++)
99       GNUNET_free (scratch[off]);
100     return res;
101   }
102 }
103
104
105 /**
106  * Free all memory that was allocated in @a rs during
107  * #GNUNET_PQ_extract_result().
108  *
109  * @param rs reult specification to clean up
110  */
111 void
112 GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs)
113 {
114   unsigned int i;
115
116   for (i=0; NULL != rs[i].conv; i++)
117     if (NULL != rs[i].cleaner)
118       rs[i].cleaner (rs[i].cls,
119                      rs[i].dst);
120 }
121
122
123 /**
124  * Extract results from a query result according to the given
125  * specification.
126  *
127  * @param result result to process
128  * @param[in,out] rs result specification to extract for
129  * @param row row from the result to extract
130  * @return
131  *   #GNUNET_YES if all results could be extracted
132  *   #GNUNET_SYSERR if a result was invalid (non-existing field)
133  */
134 int
135 GNUNET_PQ_extract_result (PGresult *result,
136                           struct GNUNET_PQ_ResultSpec *rs,
137                           int row)
138 {
139   unsigned int i;
140   int ret;
141
142   for (i=0; NULL != rs[i].conv; i++)
143   {
144     struct GNUNET_PQ_ResultSpec *spec;
145
146     spec = &rs[i];
147     ret = spec->conv (spec->cls,
148                       result,
149                       row,
150                       spec->fname,
151                       &spec->dst_size,
152                       spec->dst);
153     if (GNUNET_OK != ret)
154     {
155       GNUNET_PQ_cleanup_result (rs);
156       return GNUNET_SYSERR;
157     }
158     if (NULL != spec->result_size)
159       *spec->result_size = spec->dst_size;
160   }
161   return GNUNET_OK;
162 }
163
164
165 /* end of pq/pq.c */