GNUnet  last
sq.c
Go to the documentation of this file.
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  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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
25 #include "platform.h"
26 #include "gnunet_sq_lib.h"
27 
28 
30 GNUNET_SQ_bind (sqlite3_stmt *stmt,
31  const struct GNUNET_SQ_QueryParam *params)
32 {
33  unsigned int j;
34 
35  j = 1;
36  for (unsigned int i = 0; NULL != params[i].conv; i++)
37  {
38  if (GNUNET_OK !=
39  params[i].conv (params[i].conv_cls,
40  params[i].data,
41  params[i].size,
42  stmt,
43  j))
44  {
46  "sq",
47  _ ("Failure to bind %u-th SQL parameter\n"),
48  i);
49  if (SQLITE_OK !=
50  sqlite3_reset (stmt))
51  {
53  "sq",
54  _ ("Failure in sqlite3_reset (!)\n"));
55  return GNUNET_SYSERR;
56  }
57  }
58  GNUNET_assert (0 != params[i].num_params);
59  j += params[i].num_params;
60  }
61  return GNUNET_OK;
62 }
63 
64 
75 GNUNET_SQ_extract_result (sqlite3_stmt *result,
76  struct GNUNET_SQ_ResultSpec *rs)
77 {
78  unsigned int j = 0;
79 
80  for (unsigned int i = 0; NULL != rs[i].conv; i++)
81  {
82  if (NULL == rs[i].result_size)
83  rs[i].result_size = &rs[i].dst_size;
84  if (GNUNET_OK !=
85  rs[i].conv (rs[i].cls,
86  result,
87  j,
88  rs[i].result_size,
89  rs[i].dst))
90  {
91  for (unsigned int k = 0; k < i; k++)
92  if (NULL != rs[k].cleaner)
93  rs[k].cleaner (rs[k].cls);
94  return GNUNET_SYSERR;
95  }
96  GNUNET_assert (0 != rs[i].num_params);
97  j += rs[i].num_params;
98  }
99  return GNUNET_OK;
100 }
101 
102 
103 void
105 {
106  for (unsigned int i = 0; NULL != rs[i].conv; i++)
107  if (NULL != rs[i].cleaner)
108  rs[i].cleaner (rs[i].cls);
109 }
110 
111 
118 void
119 GNUNET_SQ_reset (sqlite3 *dbh,
120  sqlite3_stmt *stmt)
121 {
122  if (SQLITE_OK !=
123  sqlite3_reset (stmt))
125  "sqlite",
126  _ ("Failed to reset sqlite statement with error: %s\n"),
127  sqlite3_errmsg (dbh));
128 }
129 
130 
131 /* end of sq.c */
static char * data
The data to insert into the dht.
static GstElement * conv
static int result
Global testing status.
helper functions for Sqlite3 DB interactions
#define GNUNET_log_from(kind, comp,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_BULK
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
enum GNUNET_GenericReturnValue GNUNET_SQ_extract_result(sqlite3_stmt *result, struct GNUNET_SQ_ResultSpec *rs)
Extract results from a query result according to the given specification.
Definition: sq.c:75
void GNUNET_SQ_cleanup_result(struct GNUNET_SQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_SQ_extract_result().
Definition: sq.c:104
enum GNUNET_GenericReturnValue GNUNET_SQ_bind(sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
Execute binding operations for a prepared statement.
Definition: sq.c:30
void GNUNET_SQ_reset(sqlite3 *dbh, sqlite3_stmt *stmt)
Reset stmt and log error.
Definition: sq.c:119
Description of a DB query parameter.
Definition: gnunet_sq_lib.h:56
GNUNET_SQ_QueryConverter conv
Function for how to handle this type of entry.
Definition: gnunet_sq_lib.h:60
unsigned int num_params
Number of parameters eaten by this operation.
Definition: gnunet_sq_lib.h:80
Description of a DB result cell.
size_t * result_size
Where to store actual size of the result.
GNUNET_SQ_ResultConverter conv
What is the format of the result?
GNUNET_SQ_ResultCleanup cleaner
Function to clean up result data, NULL if cleanup is not necessary.
unsigned int num_params
Number of parameters (columns) eaten by this operation.
size_t dst_size
Allowed size for the data, 0 for variable-size (in this case, the type of dst is a void ** and we nee...