aboutsummaryrefslogtreecommitdiff
path: root/src/my/my.c
blob: 54b2a49b017fba06360808f4ee286b3b0fea694b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
     This file is part of GNUnet
     Copyright (C) 2016 Inria & GNUnet e.V.

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/
/**
 * @file my/my.c
 * @brief library to help with access to a MySQL database
 * @author Christophe Genevey
 * @author Christian Grothoff
 */
#include "platform.h"
#include <mysql/mysql.h>
#include "gnunet_my_lib.h"

#define STRING_SIZE 50

/**
 * Run a prepared SELECT statement.
 *
 * @param mc mysql context
 * @param sh handle to SELECT statment
 * @param params parameters to the statement
 * @return 
      #GNUNET_YES if we can prepare all statement
      #GNUNET_SYSERR if we can't prepare all statement
 */

int
GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
                         struct GNUNET_MYSQL_StatementHandle *sh,
                         const struct GNUNET_MY_QueryParam *params)
{
  const struct GNUNET_MY_QueryParam *p;
  unsigned int num;
  unsigned int i;
  MYSQL_STMT *stmt;

  num = 0;
  for (i=0;NULL != params[i].conv;i++)
    num += params[i].num_params;
  {
    MYSQL_BIND qbind[num];
    unsigned int off;

    memset(qbind, 0, sizeof(qbind));
    off = 0;
    for (i=0;NULL != (p = &params[i])->conv;i++)
    {
      if (GNUNET_OK !=
          p->conv (p->conv_cls,
                   p,
                   &qbind[off]))
      {
        return GNUNET_SYSERR;
      }
      off += p->num_params;
    }
    stmt = GNUNET_MYSQL_statement_get_stmt (mc, sh);
    if (mysql_stmt_bind_param (stmt,
                               qbind))
    {
      GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
                       _("`%s' failed at %s:%d with error: %s\n"),
                       "mysql_stmt_bind_param", __FILE__, __LINE__,
                       mysql_stmt_error (stmt));
      GNUNET_MYSQL_statements_invalidate (mc);
      return GNUNET_SYSERR;
    }

  }
  if (mysql_stmt_execute (stmt))
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
                     _("`%s' failed at %s:%d with error: %s\n"),
                     "mysql_stmt_execute", __FILE__, __LINE__,
                     mysql_stmt_error (stmt));
    GNUNET_MYSQL_statements_invalidate (mc);
    return GNUNET_SYSERR;
  }

  return GNUNET_OK;
}


/**
 * Extract results from a query result according
 * to the given specification. If colums are NULL,
 * the destination is not modified, and #GNUNET_NO is returned4
 *
 *
 * @param result
 * @param row, the row from the result to extract
 * @param result specificatio to extract for
 * @return
    #GNUNET_YES if all results could be extracted
    #GNUNET_NO if at least one result was NULL
    #GNUNET_SYSERR if a result was invalid
*/
int
GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
                          struct GNUNET_MY_QueryParam *qp,
                          struct GNUNET_MY_ResultSpec *rs,
                          int row)
{
  MYSQL_BIND *result;

  int num_fields;  
  MYSQL_FIELD *fields;
  MYSQL_RES *res;

  unsigned int i;
  unsigned int j;
  int had_null = GNUNET_NO;
  int ret;
  
  result = NULL;
  MYSQL_STMT *stmt;

  stmt = GNUNET_MYSQL_statement_get_stmt (NULL /* FIXME */, sh);
  if (NULL == stmt)
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
                    ("`%s' failed at %s:%d with error: %s\n"),
                       "mysql_stmt_bind_result", __FILE__, __LINE__,
                       mysql_stmt_error (stmt));
    return GNUNET_SYSERR;
  }


  num_fields = mysql_stmt_field_count (stmt);
  res = mysql_stmt_result_metadata (stmt);
  fields = mysql_fetch_fields (res);

  int int_data[num_fields];
  long int long_data[num_fields];
  short short_data[num_fields];
  char str_data[STRING_SIZE];
  int error[num_fields];

  result = (MYSQL_BIND *)malloc (sizeof (MYSQL_BIND)*num_fields);
  if(!result)
  {
    fprintf(stderr, "Error to allocate output buffers\n");
    return GNUNET_SYSERR;
  }

  memset(result, 0, sizeof (MYSQL_BIND) * num_fields);

/** INITIALISER LE MYSQL_BIND ****/

  for(i = 0 ; i< num_fields ;i++)
  {
    result[i].buffer_type = fields[i].type; 
    result[i].is_null = 0;  
    result[i].error = &error[i];

    switch (fields[i].type)
    {
      case MYSQL_TYPE_LONG:
        result[i].buffer = &(int_data[i]);
        result[i].buffer_length = sizeof (int_data);
        break;

      case MYSQL_TYPE_LONGLONG:
        result[i].buffer = &(long_data[i]);
        result[i].buffer_length = sizeof (long_data);
        break;

      case MYSQL_TYPE_STRING:
        result[i].buffer = (char *)str_data;
        result[i].buffer_length = sizeof (str_data);
        break;

      case MYSQL_TYPE_SHORT:
        result[i].buffer = &(short_data[i]);
        result[i].buffer_length = sizeof (short_data);
        break;

      default:
        fprintf(stderr, "Failed : wrong type : %d!\n", fields[i].type);
    } 
  }

  if (mysql_stmt_bind_result(stmt, result))
  {
      GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
                       _("`%s' failed at %s:%d with error: %s\n"),
                       "mysql_stmt_bind_result", __FILE__, __LINE__,
                       mysql_stmt_error (stmt));
      return GNUNET_SYSERR;
  }

  /*** FAILED HERE ***/
  if (mysql_stmt_fetch (stmt))
  {
    for(j = 0 ; j < num_fields ;j++)
    {
      fprintf(stderr, "Error Bind [%d] : %d\n", j, error[j]);
    }

    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
                       _("`%s' failed at %s:%d with error: %s\n"),
                       "mysql_stmt_fetch", __FILE__, __LINE__,
                       mysql_stmt_error (stmt));
    return GNUNET_SYSERR;
  }

/*
  while (1)
  {
    mysql_stmt_fetch (stmt);

    for (i = 0 ; NULL != rs[i].conv ; i++)
    {
      struct GNUNET_MY_ResultSpec *spec;

      spec = &rs[i];
      ret = spec->conv (spec->conv_cls,
                        spec,
                        result);

      if (GNUNET_SYSERR == ret)
      {
        return GNUNET_SYSERR;
      }

      if (NULL != spec->result_size)
        *spec->result_size = spec->dst_size;
    }
  }

  if (GNUNET_YES == had_null)
    return GNUNET_NO;
*/

  free (result);
  return GNUNET_OK;
}

/* end of my.c */