glitch in the license text detected by hyazinthe, thank you!
[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
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,
8   or (at your option) any later version.
9
10   GNUnet is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Affero General Public License for more details.
14 */
15 /**
16  * @file sq/sq.c
17  * @brief helper functions for Sqlite3 DB interactions
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include "gnunet_sq_lib.h"
22
23
24 /**
25  * Execute a prepared statement.
26  *
27  * @param db_conn database connection
28  * @param params parameters to the statement
29  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
30  */
31 int
32 GNUNET_SQ_bind (sqlite3_stmt *stmt,
33                 const struct GNUNET_SQ_QueryParam *params)
34 {
35   unsigned int j;
36
37   j = 1;
38   for (unsigned int i=0;NULL != params[i].conv; i++)
39   {
40     if (GNUNET_OK !=
41         params[i].conv (params[i].conv_cls,
42                         params[i].data,
43                         params[i].size,
44                         stmt,
45                         j))
46     {
47       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
48                        "sq",
49                        _("Failure to bind %u-th SQL parameter\n"),
50                        i);
51       if (SQLITE_OK !=
52           sqlite3_reset (stmt))
53       {
54         GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
55                          "sq",
56                          _("Failure in sqlite3_reset (!)\n"));
57         return GNUNET_SYSERR;
58       }
59     }
60     GNUNET_assert (0 != params[i].num_params);
61     j += params[i].num_params;
62   }
63   return GNUNET_OK;
64 }
65
66
67 /**
68  * Extract results from a query result according to the given specification.
69  *
70  * @param result result to process
71  * @param[in,out] rs result specification to extract for
72  * @return
73  *   #GNUNET_OK if all results could be extracted
74  *   #GNUNET_SYSERR if a result was invalid (non-existing field)
75  */
76 int
77 GNUNET_SQ_extract_result (sqlite3_stmt *result,
78                           struct GNUNET_SQ_ResultSpec *rs)
79 {
80   unsigned int j = 0;
81
82   for (unsigned int i=0;NULL != rs[i].conv; i++)
83   {
84     if (NULL == rs[i].result_size)
85       rs[i].result_size = &rs[i].dst_size;
86     if (GNUNET_OK !=
87         rs[i].conv (rs[i].cls,
88                     result,
89                     j,
90                     rs[i].result_size,
91                     rs[i].dst))
92     {
93       for (unsigned int k=0;k<i;k++)
94         if (NULL != rs[k].cleaner)
95           rs[k].cleaner (rs[k].cls);
96       return GNUNET_SYSERR;
97     }
98     GNUNET_assert (0 != rs[i].num_params);
99     j += rs[i].num_params;
100   }
101   return GNUNET_OK;
102 }
103
104
105 /**
106  * Free all memory that was allocated in @a rs during
107  * #GNUNET_SQ_extract_result().
108  *
109  * @param rs reult specification to clean up
110  */
111 void
112 GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs)
113 {
114   for (unsigned int i=0;NULL != rs[i].conv; i++)
115     if (NULL != rs[i].cleaner)
116       rs[i].cleaner (rs[i].cls);
117 }
118
119
120 /**
121  * Reset @a stmt and log error.
122  *
123  * @param dbh database handle
124  * @param stmt statement to reset
125  */
126 void
127 GNUNET_SQ_reset (sqlite3 *dbh,
128                  sqlite3_stmt *stmt)
129 {
130   if (SQLITE_OK !=
131       sqlite3_reset (stmt))
132     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
133                      "sqlite",
134                      _("Failed to reset sqlite statement with error: %s\n"),
135                      sqlite3_errmsg (dbh));
136 }
137
138
139 /* end of sq.c */