libgnunetmy query helper
[oweals/gnunet.git] / src / my / my.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2016 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
38  */
39 int
40 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
41                          struct GNUNET_MYSQL_StatementHandle *sh,
42                          const struct GNUNET_MY_QueryParam *params)
43 {
44   const struct GNUNET_MY_QueryParam *p;
45   unsigned int num;
46   unsigned int i;
47   MYSQL_STMT *stmt;
48
49   num = 0;
50   for (i=0;NULL != params[i].conv;i++)
51     num += params[i].num_params;
52   {
53     MYSQL_BIND qbind[num];
54     unsigned int off;
55
56     memset(qbind, 0, sizeof(qbind));
57     off = 0;
58     for (i=0;NULL != (p = &params[i])->conv;i++)
59     {
60       if (GNUNET_OK !=
61           p->conv (p->conv_cls,
62                    p,
63                    &qbind[off]))
64       {
65         return GNUNET_SYSERR;
66       }
67       off += p->num_params;
68     }
69     stmt = GNUNET_MYSQL_statement_get_stmt (mc, sh);
70     if (mysql_stmt_bind_param (stmt,
71                                qbind))
72     {
73       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
74                        _("`%s' failed at %s:%d with error: %s\n"),
75                        "mysql_stmt_bind_param", __FILE__, __LINE__,
76                        mysql_stmt_error (stmt));
77       GNUNET_MYSQL_statements_invalidate (mc);
78       return GNUNET_SYSERR;
79     }
80   }
81   if (mysql_stmt_execute (stmt))
82   {
83     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
84                      _("`%s' failed at %s:%d with error: %s\n"),
85                      "mysql_stmt_execute", __FILE__, __LINE__,
86                      mysql_stmt_error (stmt));
87     GNUNET_MYSQL_statements_invalidate (mc);
88     return GNUNET_SYSERR;
89   }
90   return GNUNET_OK;
91 }
92
93 /**
94   * Extract results from a query result according 
95   * to the given specification. If colums are NULL,
96   * the destination is not modified, and #GNUNET_NO is returned
97   * 
98   *
99   * @param result 
100   * @param row, the row from the result to extract
101   * @param result specificatio to extract for
102   * @return 
103     #GNUNET_YES if all results could be extracted
104     #GNUNET_NO if at least one result was NULL
105     #GNUNET_SYSERR if a result was invalid
106 */
107 int
108 GNUNET_MY_extract_result (MYSQL_BIND * result,
109                           int row,
110                           struct GNUNET_MY_ResultSpec *specs)
111 {
112   unsigned int i;
113   int had_null = GNUNET_NO;
114   int ret;
115
116   for(i = 0 ; NULL != specs[i].conv ; i++) 
117   {
118     struct GNUNET_MY_ResultSpec *spec;
119
120     spec = &specs[i];
121     ret = spec->conv(spec->conv_cls,
122                     NULL, //wait GNUNET_MY_QueryParam
123                     result);
124     
125     if(ret == GNUNET_SYSERR)
126       return GNUNET_SYSERR;
127
128     if(spec->result_size != NULL)
129       *spec->result_size = spec->dst_size;
130   }
131   
132   if(GNUNET_YES == had_null)
133     return GNUNET_NO;
134
135   return GNUNET_OK;
136 }
137
138 /* end of my.c */