Fix for #4553
[oweals/gnunet.git] / src / my / my.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2016 Inria & GNUnet e.V.
4
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.
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      General Public License for more details.
14
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.
19 */
20 /**
21  * @file my/my.c
22  * @brief library to help with access to a MySQL database
23  * @author Christophe Genevey
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include <mysql/mysql.h>
28 #include "gnunet_my_lib.h"
29
30
31
32 /**
33  * Run a prepared SELECT statement.
34  *
35  * @param mc mysql context
36  * @param sh handle to SELECT statment
37  * @param params parameters to the statement
38  * @return 
39       #GNUNET_YES if we can prepare all statement
40       #GNUNET_SYSERR if we can't prepare all statement
41  */
42
43 int
44 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
45                          struct GNUNET_MYSQL_StatementHandle *sh,
46                          const struct GNUNET_MY_QueryParam *params)
47 {
48   const struct GNUNET_MY_QueryParam *p;
49   unsigned int num;
50   unsigned int i;
51   MYSQL_STMT *stmt;
52
53   num = 0;
54   for (i=0;NULL != params[i].conv;i++)
55     num += params[i].num_params;
56   {
57     MYSQL_BIND qbind[num];
58     unsigned int off;
59
60     memset(qbind, 0, sizeof(qbind));
61     off = 0;
62     for (i=0;NULL != (p = &params[i])->conv;i++)
63     {
64       if (GNUNET_OK !=
65           p->conv (p->conv_cls,
66                    p,
67                    &qbind[off]))
68       {
69         return GNUNET_SYSERR;
70       }
71       off += p->num_params;
72     }
73     stmt = GNUNET_MYSQL_statement_get_stmt (mc, sh);
74     if (mysql_stmt_bind_param (stmt,
75                                qbind))
76     {
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);
82       return GNUNET_SYSERR;
83     }
84
85   }
86   if (mysql_stmt_execute (stmt))
87   {
88     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
89                      _("`%s' failed at %s:%d with error: %s\n"),
90                      "mysql_stmt_execute", __FILE__, __LINE__,
91                      mysql_stmt_error (stmt));
92     GNUNET_MYSQL_statements_invalidate (mc);
93     return GNUNET_SYSERR;
94   }
95
96   return GNUNET_OK;
97 }
98
99
100 /**
101  * Extract results from a query result according
102  * to the given specification. If colums are NULL,
103  * the destination is not modified, and #GNUNET_NO is returned4
104  *
105  *
106  * @param result
107  * @param row, the row from the result to extract
108  * @param result specificatio to extract for
109  * @return
110     #GNUNET_YES if all results could be extracted
111     #GNUNET_NO if at least one result was NULL
112     #GNUNET_SYSERR if a result was invalid
113 */
114 int
115 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
116                           struct GNUNET_MY_QueryParam *qp,
117                           struct GNUNET_MY_ResultSpec *rs,
118                           int row)
119 {
120   MYSQL_BIND *result;
121   unsigned int i;
122   int had_null = GNUNET_NO;
123   int ret;
124   
125   MYSQL_STMT *stmt;
126
127   stmt = GNUNET_MYSQL_statement_get_stmt (NULL /* FIXME */, sh);
128   // result = mysql_get_result (stmt);
129   result = NULL;
130
131   if (mysql_stmt_bind_result(stmt, result))
132   {
133
134       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
135                        _("`%s' failed at %s:%d with error: %s\n"),
136                        "mysql_stmt_bind_result", __FILE__, __LINE__,
137                        mysql_stmt_error (stmt));
138       return GNUNET_SYSERR;
139   }
140
141   for (i = 0 ; NULL != rs[i].conv ; i++)
142   {
143     struct GNUNET_MY_ResultSpec *spec;
144
145     spec = &rs[i];
146     ret = spec->conv (spec->conv_cls,
147                       spec,
148                       result);
149
150     if (GNUNET_SYSERR == ret)
151     {
152       return GNUNET_SYSERR;
153     }
154
155     if (NULL != spec->result_size)
156       *spec->result_size = spec->dst_size;
157   }
158
159   if (GNUNET_YES == had_null)
160     return GNUNET_NO;
161
162   return GNUNET_OK;
163 }
164
165 /* end of my.c */