1ab4a64dbd09a49e336693b2bfc9e8af22f5a73a
[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 Christian Grothoff
24  */
25 #include "platform.h"
26 #include <mysql/mysql.h>
27 #include "gnunet_my_lib.h"
28
29
30
31 /**
32  * Run a prepared SELECT statement.
33  *
34  * @param mc mysql context
35  * @param sh handle to SELECT statment
36  * @param params parameters to the statement
37  * @return mysql result
38  */
39
40  /***** FIXE THIS FUNCTION *****/
41 int
42 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
43                          struct GNUNET_MYSQL_StatementHandle *sh,
44                          const struct GNUNET_MY_QueryParam *params)
45 {
46   const struct GNUNET_MY_QueryParam *p;
47   unsigned int num;
48   unsigned int i;
49   MYSQL_STMT *stmt;
50
51   num = 0;
52   for (i=0;NULL != params[i].conv;i++)
53     num += params[i].num_params;
54   {
55     MYSQL_BIND qbind[num];
56     unsigned int off;
57
58     memset(qbind, 0, sizeof(qbind));
59     off = 0;
60     for (i=0;NULL != (p = &params[i])->conv;i++)
61     {
62       if (GNUNET_OK !=
63           p->conv (p->conv_cls,
64                    p,
65                    &qbind[off]))
66       {
67         return GNUNET_SYSERR;
68       }
69       off += p->num_params;
70     }
71     stmt = GNUNET_MYSQL_statement_get_stmt (mc, sh);
72     if (mysql_stmt_bind_param (stmt,
73                                qbind))
74     {
75       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
76                        _("`%s' failed at %s:%d with error: %s\n"),
77                        "mysql_stmt_bind_param", __FILE__, __LINE__,
78                        mysql_stmt_error (stmt));
79       GNUNET_MYSQL_statements_invalidate (mc);
80       return GNUNET_SYSERR;
81     }
82   }
83   if (mysql_stmt_execute (stmt))
84   {
85     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
86                      _("`%s' failed at %s:%d with error: %s\n"),
87                      "mysql_stmt_execute", __FILE__, __LINE__,
88                      mysql_stmt_error (stmt));
89     GNUNET_MYSQL_statements_invalidate (mc);
90     return GNUNET_SYSERR;
91   }
92   return GNUNET_OK;
93 }
94
95
96 /**
97  * Extract results from a query result according
98  * to the given specification. If colums are NULL,
99  * the destination is not modified, and #GNUNET_NO is returned4
100  *
101  *
102  * @param result
103  * @param row, the row from the result to extract
104  * @param result specificatio to extract for
105  * @return
106     #GNUNET_YES if all results could be extracted
107     #GNUNET_NO if at least one result was NULL
108     #GNUNET_SYSERR if a result was invalid
109 */
110 int
111 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
112                           struct GNUNET_MY_QueryParam *qp,
113                           struct GNUNET_MY_ResultSpec *rs,
114                           int row)
115 {
116   MYSQL_BIND * result;
117   unsigned int i;
118   int had_null = GNUNET_NO;
119   int ret;
120   MYSQL_STMT *stmt;
121
122   stmt = GNUNET_MYSQL_statement_get_stmt (NULL /* FIXME */, sh);
123   // result = mysql_get_result (stmt);
124   result = NULL;
125   for (i = 0 ; NULL != rs[i].conv ; i++)
126   {
127     struct GNUNET_MY_ResultSpec *spec;
128
129     spec = &rs[i];
130     ret = spec->conv (spec->conv_cls,
131                       qp,
132                       result);
133
134     if (GNUNET_SYSERR == ret)
135     {
136      //GNUNET_MY_cleanup_result(rs);
137       return GNUNET_SYSERR;
138     }
139
140     if (NULL != spec->result_size)
141       *spec->result_size = spec->dst_size;
142   }
143
144   if (GNUNET_YES == had_null)
145     return GNUNET_NO;
146
147   return GNUNET_OK;
148 }
149
150 /* end of my.c */