fix memory leak
[oweals/gnunet.git] / src / sq / sq.c
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2017 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 sq/sq.c
18  * @brief helper functions for Sqlite3 DB interactions
19  * @author Christian Grothoff
20  */
21 #include "platform.h"
22 #include "gnunet_sq_lib.h"
23
24
25 /**
26  * Execute a prepared statement.
27  *
28  * @param db_conn database connection
29  * @param params parameters to the statement
30  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
31  */
32 int
33 GNUNET_SQ_bind (sqlite3_stmt *stmt,
34                 const struct GNUNET_SQ_QueryParam *params)
35 {
36   unsigned int j;
37
38   j = 1;
39   for (unsigned int i=0;NULL != params[i].conv; i++)
40   {
41     if (GNUNET_OK !=
42         params[i].conv (params[i].conv_cls,
43                         params[i].data,
44                         params[i].size,
45                         stmt,
46                         j))
47     {
48       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
49                        "sq",
50                        _("Failure to bind %u-th SQL parameter\n"),
51                        i);
52       if (SQLITE_OK !=
53           sqlite3_reset (stmt))
54       {
55         GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
56                          "sq",
57                          _("Failure in sqlite3_reset (!)\n"));
58         return GNUNET_SYSERR;
59       }
60     }
61     GNUNET_assert (0 != params[i].num_params);
62     j += params[i].num_params;
63   }
64   return GNUNET_OK;
65 }
66
67
68 /**
69  * Extract results from a query result according to the given specification.
70  *
71  * @param result result to process
72  * @param[in,out] rs result specification to extract for
73  * @return
74  *   #GNUNET_OK if all results could be extracted
75  *   #GNUNET_SYSERR if a result was invalid (non-existing field)
76  */
77 int
78 GNUNET_SQ_extract_result (sqlite3_stmt *result,
79                           struct GNUNET_SQ_ResultSpec *rs)
80 {
81   unsigned int j = 0;
82
83   for (unsigned int i=0;NULL != rs[i].conv; i++)
84   {
85     if (NULL == rs[i].result_size)
86       rs[i].result_size = &rs[i].dst_size;
87     if (GNUNET_OK !=
88         rs[i].conv (rs[i].cls,
89                     result,
90                     j,
91                     rs[i].result_size,
92                     rs[i].dst))
93       return GNUNET_SYSERR;
94     GNUNET_assert (0 != rs[i].num_params);
95     j += rs[i].num_params;
96   }
97   return GNUNET_OK;
98 }
99
100
101 /**
102  * Free all memory that was allocated in @a rs during
103  * #GNUNET_SQ_extract_result().
104  *
105  * @param rs reult specification to clean up
106  */
107 void
108 GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs)
109 {
110   for (unsigned int i=0;NULL != rs[i].conv; i++)
111     if (NULL != rs[i].cleaner)
112       rs[i].cleaner (rs[i].cls);
113 }
114
115 /* end of sq.c */