2 This file is part of GNUnet
3 Copyright (C) 2016 Inria & GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @brief library to help with access to a MySQL database
23 * @author Christophe Genevey
24 * @author Christian Grothoff
27 #include <mysql/mysql.h>
28 #include "gnunet_my_lib.h"
33 * Run a prepared SELECT statement.
35 * @param mc mysql context
36 * @param sh handle to SELECT statment
37 * @param params parameters to the statement
39 #GNUNET_YES if we can prepare all statement
40 #GNUNET_SYSERR if we can't prepare all statement
44 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
45 struct GNUNET_MYSQL_StatementHandle *sh,
46 const struct GNUNET_MY_QueryParam *params)
48 const struct GNUNET_MY_QueryParam *p;
54 for (i=0;NULL != params[i].conv;i++)
55 num += params[i].num_params;
57 MYSQL_BIND qbind[num];
60 memset(qbind, 0, sizeof(qbind));
62 for (i=0;NULL != (p = ¶ms[i])->conv;i++)
73 stmt = GNUNET_MYSQL_statement_get_stmt (mc, sh);
74 if (mysql_stmt_bind_param (stmt,
77 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
78 _("`%s' failed at %s:%d with error: %s\n"),
79 "mysql_stmt_bind_param", __FILE__, __LINE__,
80 mysql_stmt_error (stmt));
81 GNUNET_MYSQL_statements_invalidate (mc);
85 if (mysql_stmt_execute (stmt))
87 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
88 _("`%s' failed at %s:%d with error: %s\n"),
89 "mysql_stmt_execute", __FILE__, __LINE__,
90 mysql_stmt_error (stmt));
91 GNUNET_MYSQL_statements_invalidate (mc);
99 * Extract results from a query result according
100 * to the given specification. If colums are NULL,
101 * the destination is not modified, and #GNUNET_NO is returned4
105 * @param row, the row from the result to extract
106 * @param result specificatio to extract for
108 #GNUNET_YES if all results could be extracted
109 #GNUNET_NO if at least one result was NULL
110 #GNUNET_SYSERR if a result was invalid
113 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
114 struct GNUNET_MY_QueryParam *qp,
115 struct GNUNET_MY_ResultSpec *rs,
120 int had_null = GNUNET_NO;
125 stmt = GNUNET_MYSQL_statement_get_stmt (NULL /* FIXME */, sh);
126 // result = mysql_get_result (stmt);
129 if (mysql_stmt_bind_result(stmt, result))
132 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
133 _("`%s' failed at %s:%d with error: %s\n"),
134 "mysql_stmt_bind_result", __FILE__, __LINE__,
135 mysql_stmt_error (stmt));
136 return GNUNET_SYSERR;
139 for (i = 0 ; NULL != rs[i].conv ; i++)
141 struct GNUNET_MY_ResultSpec *spec;
144 ret = spec->conv (spec->conv_cls,
148 if (GNUNET_SYSERR == ret)
150 return GNUNET_SYSERR;
153 if (NULL != spec->result_size)
154 *spec->result_size = spec->dst_size;
157 if (GNUNET_YES == had_null)