2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @brief library to help with access to a MySQL database
23 * @author Christian Grothoff
26 #include <mysql/mysql.h>
27 #include "gnunet_mysql_lib.h"
30 * Maximum number of supported parameters for a prepared
31 * statement. Increase if needed.
37 * Die with an error message that indicates
38 * a failure of the command 'cmd' with the message given
41 #define DIE_MYSQL(cmd, dbh) do { GNUNET_log_from (GNUNET_ERROR_TYPE__ERROR, "mysql", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, mysql_error((dbh)->dbf)); GNUNET_abort(); } while(0);
44 * Log an error message at log-level 'level' that indicates
45 * a failure of the command 'cmd' on file 'filename'
46 * with the message given by strerror(errno).
48 #define LOG_MYSQL(level, cmd, dbh) do { GNUNET_log_from (level, "mysql", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, mysql_error((dbh)->dbf)); } while(0);
54 struct GNUNET_MYSQL_Context
60 const struct GNUNET_CONFIGURATION_Handle *cfg;
68 * Handle to the mysql database.
73 * Head of list of our prepared statements.
75 struct GNUNET_MYSQL_StatementHandle *shead;
78 * Tail of list of our prepared statements.
80 struct GNUNET_MYSQL_StatementHandle *stail;
83 * Filename of "my.cnf" (msyql configuration).
91 * Handle for a prepared statement.
93 struct GNUNET_MYSQL_StatementHandle
99 struct GNUNET_MYSQL_StatementHandle *next;
104 struct GNUNET_MYSQL_StatementHandle *prev;
107 * Original query string.
112 * Handle to MySQL prepared statement.
114 MYSQL_STMT *statement;
117 * Is the MySQL prepared statement valid, or do we need to re-initialize it?
125 * Obtain the location of ".my.cnf".
127 * @param cfg our configuration
128 * @return NULL on error
131 get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
144 pw = getpwuid (getuid ());
147 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR, "mysql", "getpwuid");
150 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg, section, "CONFIG"))
152 GNUNET_assert (GNUNET_OK ==
153 GNUNET_CONFIGURATION_get_value_filename (cfg, section,
156 configured = GNUNET_YES;
160 home_dir = GNUNET_strdup (pw->pw_dir);
161 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
162 GNUNET_free (home_dir);
163 configured = GNUNET_NO;
166 home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1);
167 plibc_conv_to_win_path ("~/", home_dir);
168 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
169 GNUNET_free (home_dir);
170 configured = GNUNET_NO;
172 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "mysql",
173 _("Trying to use file `%s' for MySQL configuration.\n"),
175 if ((0 != STAT (cnffile, &st)) || (0 != ACCESS (cnffile, R_OK)) ||
176 (!S_ISREG (st.st_mode)))
178 if (configured == GNUNET_YES)
179 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
180 _("Could not access file `%s': %s\n"), cnffile,
182 GNUNET_free (cnffile);
190 * Open the connection with the database (and initialize
191 * our default options).
193 * @param mc database context to initialze
194 * @return GNUNET_OK on success
197 iopen (struct GNUNET_MYSQL_Context *mc)
202 char *mysql_password;
203 unsigned long long mysql_port;
205 unsigned int timeout;
207 mc->dbf = mysql_init (NULL);
209 return GNUNET_SYSERR;
210 if (mc->cnffile != NULL)
211 mysql_options (mc->dbf, MYSQL_READ_DEFAULT_FILE, mc->cnffile);
212 mysql_options (mc->dbf, MYSQL_READ_DEFAULT_GROUP, "client");
214 mysql_options (mc->dbf, MYSQL_OPT_RECONNECT, &reconnect);
215 mysql_options (mc->dbf, MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout);
216 mysql_options (mc->dbf, MYSQL_SET_CHARSET_NAME, "UTF8");
217 timeout = 60; /* in seconds */
218 mysql_options (mc->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
219 mysql_options (mc->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
222 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "DATABASE"))
223 GNUNET_assert (GNUNET_OK ==
224 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
228 mysql_dbname = GNUNET_strdup ("gnunet");
231 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "USER"))
233 GNUNET_assert (GNUNET_OK ==
234 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
235 "USER", &mysql_user));
237 mysql_password = NULL;
239 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PASSWORD"))
241 GNUNET_assert (GNUNET_OK ==
242 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
248 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "HOST"))
250 GNUNET_assert (GNUNET_OK ==
251 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
257 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PORT"))
259 GNUNET_assert (GNUNET_OK ==
260 GNUNET_CONFIGURATION_get_value_number (mc->cfg, mc->section,
261 "PORT", &mysql_port));
264 GNUNET_assert (mysql_dbname != NULL);
265 mysql_real_connect (mc->dbf, mysql_server, mysql_user, mysql_password,
266 mysql_dbname, (unsigned int) mysql_port, NULL,
267 CLIENT_IGNORE_SIGPIPE);
268 GNUNET_free_non_null (mysql_server);
269 GNUNET_free_non_null (mysql_user);
270 GNUNET_free_non_null (mysql_password);
271 GNUNET_free (mysql_dbname);
272 if (mysql_error (mc->dbf)[0])
274 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_real_connect", mc);
275 return GNUNET_SYSERR;
282 * Create a mysql context.
284 * @param cfg configuration
285 * @param section configuration section to use to get MySQL configuration options
286 * @return the mysql context
288 struct GNUNET_MYSQL_Context *
289 GNUNET_MYSQL_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
292 struct GNUNET_MYSQL_Context *mc;
294 mc = GNUNET_malloc (sizeof (struct GNUNET_MYSQL_Context));
296 mc->section = section;
297 mc->cnffile = get_my_cnf_path (cfg, section);
304 * Close database connection and all prepared statements (we got a DB
307 * @param mc mysql context
310 GNUNET_MYSQL_statements_invalidate (struct GNUNET_MYSQL_Context *mc)
312 struct GNUNET_MYSQL_StatementHandle *sh;
314 for (sh = mc->shead; NULL != sh; sh = sh->next)
316 if (GNUNET_YES == sh->valid)
318 mysql_stmt_close (sh->statement);
319 sh->valid = GNUNET_NO;
321 sh->statement = NULL;
325 mysql_close (mc->dbf);
332 * Destroy a mysql context. Also frees all associated prepared statements.
334 * @param mc context to destroy
337 GNUNET_MYSQL_context_destroy (struct GNUNET_MYSQL_Context *mc)
339 struct GNUNET_MYSQL_StatementHandle *sh;
341 GNUNET_MYSQL_statements_invalidate (mc);
342 while (NULL != (sh = mc->shead))
344 GNUNET_CONTAINER_DLL_remove (mc->shead, mc->stail, sh);
345 GNUNET_free (sh->query);
349 mysql_library_end ();
354 * Prepare a statement. Prepared statements are automatically discarded
355 * when the MySQL context is destroyed.
357 * @param mc mysql context
358 * @param query query text
359 * @return prepared statement, NULL on error
361 struct GNUNET_MYSQL_StatementHandle *
362 GNUNET_MYSQL_statement_prepare (struct GNUNET_MYSQL_Context *mc,
365 struct GNUNET_MYSQL_StatementHandle *sh;
367 sh = GNUNET_malloc (sizeof (struct GNUNET_MYSQL_StatementHandle));
368 sh->query = GNUNET_strdup (query);
369 GNUNET_CONTAINER_DLL_insert (mc->shead, mc->stail, sh);
375 * Run a SQL statement.
377 * @param mc mysql context
378 * @param sql SQL statement to run
379 * @return GNUNET_OK on success
380 * GNUNET_SYSERR if there was a problem
383 GNUNET_MYSQL_statement_run (struct GNUNET_MYSQL_Context *mc, const char *sql)
385 if ((NULL == mc->dbf) && (GNUNET_OK != iopen (mc)))
386 return GNUNET_SYSERR;
387 mysql_query (mc->dbf, sql);
388 if (mysql_error (mc->dbf)[0])
390 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_query", mc);
391 GNUNET_MYSQL_statements_invalidate (mc);
392 return GNUNET_SYSERR;
399 * Prepare a statement for running.
401 * @param mc mysql context
402 * @param sh statement handle to prepare
403 * @return GNUNET_OK on success
406 prepare_statement (struct GNUNET_MYSQL_Context *mc,
407 struct GNUNET_MYSQL_StatementHandle *sh)
409 if (GNUNET_YES == sh->valid)
411 if ((NULL == mc->dbf) && (GNUNET_OK != iopen (mc)))
412 return GNUNET_SYSERR;
413 sh->statement = mysql_stmt_init (mc->dbf);
414 if (NULL == sh->statement)
416 GNUNET_MYSQL_statements_invalidate (mc);
417 return GNUNET_SYSERR;
419 if (0 != mysql_stmt_prepare (sh->statement, sh->query, strlen (sh->query)))
421 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", mc);
422 mysql_stmt_close (sh->statement);
423 sh->statement = NULL;
424 GNUNET_MYSQL_statements_invalidate (mc);
425 return GNUNET_SYSERR;
427 sh->valid = GNUNET_YES;
433 * Get internal handle for a prepared statement. This function should rarely
434 * be used, and if, with caution! On failures during the interaction with
435 * the handle, you must call 'GNUNET_MYSQL_statements_invalidate'!
437 * @param mc mysql context
438 * @param sh prepared statement to introspect
439 * @return MySQL statement handle, NULL on error
442 GNUNET_MYSQL_statement_get_stmt (struct GNUNET_MYSQL_Context * mc,
443 struct GNUNET_MYSQL_StatementHandle * sh)
445 (void) prepare_statement (mc, sh);
446 return sh->statement;
451 * Bind the parameters for the given MySQL statement
454 * @param mc mysql context
455 * @param sh statement to bind and run
456 * @param ap arguments for the binding
457 * @return GNUNET_SYSERR on error, GNUNET_OK on success
460 init_params (struct GNUNET_MYSQL_Context *mc,
461 struct GNUNET_MYSQL_StatementHandle *sh, va_list ap)
463 MYSQL_BIND qbind[MAX_PARAM];
466 enum enum_field_types ft;
468 pc = mysql_stmt_param_count (sh->statement);
471 /* increase internal constant! */
473 return GNUNET_SYSERR;
475 memset (qbind, 0, sizeof (qbind));
478 while ((pc > 0) && (-1 != (int) (ft = va_arg (ap, enum enum_field_types))))
480 qbind[off].buffer_type = ft;
483 case MYSQL_TYPE_FLOAT:
484 qbind[off].buffer = va_arg (ap, float *);
487 case MYSQL_TYPE_LONGLONG:
488 qbind[off].buffer = va_arg (ap, unsigned long long *);
489 qbind[off].is_unsigned = va_arg (ap, int);
492 case MYSQL_TYPE_LONG:
493 qbind[off].buffer = va_arg (ap, unsigned int *);
494 qbind[off].is_unsigned = va_arg (ap, int);
497 case MYSQL_TYPE_VAR_STRING:
498 case MYSQL_TYPE_STRING:
499 case MYSQL_TYPE_BLOB:
500 qbind[off].buffer = va_arg (ap, void *);
501 qbind[off].buffer_length = va_arg (ap, unsigned long);
502 qbind[off].length = va_arg (ap, unsigned long *);
506 /* unsupported type */
508 return GNUNET_SYSERR;
513 if (!((pc == 0) && (-1 != (int) ft) && (va_arg (ap, int) == -1)))
516 return GNUNET_SYSERR;
518 if (mysql_stmt_bind_param (sh->statement, qbind))
520 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
521 _("`%s' failed at %s:%d with error: %s\n"),
522 "mysql_stmt_bind_param", __FILE__, __LINE__,
523 mysql_stmt_error (sh->statement));
524 GNUNET_MYSQL_statements_invalidate (mc);
525 return GNUNET_SYSERR;
527 if (mysql_stmt_execute (sh->statement))
529 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
530 _("`%s' failed at %s:%d with error: %s\n"),
531 "mysql_stmt_execute", __FILE__, __LINE__,
532 mysql_stmt_error (sh->statement));
533 GNUNET_MYSQL_statements_invalidate (mc);
534 return GNUNET_SYSERR;
542 * Run a prepared SELECT statement.
544 * @param mc mysql context
545 * @param s statement to run
546 * @param result_size number of elements in results array
547 * @param results pointer to already initialized MYSQL_BIND
548 * array (of sufficient size) for passing results
549 * @param processor function to call on each result
550 * @param processor_cls extra argument to processor
551 * @param ap pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
552 * values (size + buffer-reference for pointers); terminated
554 * @return GNUNET_SYSERR on error, otherwise
555 * the number of successfully affected (or queried) rows
558 GNUNET_MYSQL_statement_run_prepared_select_va (struct GNUNET_MYSQL_Context *mc,
560 GNUNET_MYSQL_StatementHandle *s,
561 unsigned int result_size,
562 MYSQL_BIND * results,
563 GNUNET_MYSQL_DataProcessor
564 processor, void *processor_cls,
571 if (GNUNET_OK != prepare_statement (mc, s))
574 return GNUNET_SYSERR;
576 if (GNUNET_OK != init_params (mc, s, ap))
579 return GNUNET_SYSERR;
581 rsize = mysql_stmt_field_count (s->statement);
582 if (rsize > result_size)
585 return GNUNET_SYSERR;
587 if (mysql_stmt_bind_result (s->statement, results))
589 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
590 _("`%s' failed at %s:%d with error: %s\n"),
591 "mysql_stmt_bind_result", __FILE__, __LINE__,
592 mysql_stmt_error (s->statement));
593 GNUNET_MYSQL_statements_invalidate (mc);
594 return GNUNET_SYSERR;
600 ret = mysql_stmt_fetch (s->statement);
601 if (ret == MYSQL_NO_DATA)
605 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
606 _("`%s' failed at %s:%d with error: %s\n"),
607 "mysql_stmt_fetch", __FILE__, __LINE__,
608 mysql_stmt_error (s->statement));
609 GNUNET_MYSQL_statements_invalidate (mc);
610 return GNUNET_SYSERR;
613 if ((NULL == processor) ||
614 (GNUNET_OK != processor (processor_cls, rsize, results)))
617 mysql_stmt_reset (s->statement);
623 * Run a prepared SELECT statement.
625 * @param mc mysql context
626 * @param sh handle to SELECT statment
627 * @param result_size number of elements in results array
628 * @param results pointer to already initialized MYSQL_BIND
629 * array (of sufficient size) for passing results
630 * @param processor function to call on each result
631 * @param processor_cls extra argument to processor
632 * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
633 * values (size + buffer-reference for pointers); terminated
635 * @return GNUNET_SYSERR on error, otherwise
636 * the number of successfully affected (or queried) rows
639 GNUNET_MYSQL_statement_run_prepared_select (struct GNUNET_MYSQL_Context *mc,
640 struct GNUNET_MYSQL_StatementHandle
641 *sh, unsigned int result_size,
642 MYSQL_BIND * results,
643 GNUNET_MYSQL_DataProcessor
644 processor, void *processor_cls, ...)
649 va_start (ap, processor_cls);
651 GNUNET_MYSQL_statement_run_prepared_select_va (mc, sh, result_size,
660 * Run a prepared statement that does NOT produce results.
662 * @param mc mysql context
663 * @param sh handle to statment
664 * @param insert_id NULL or address where to store the row ID of whatever
665 * was inserted (only for INSERT statements!)
666 * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
667 * values (size + buffer-reference for pointers); terminated
669 * @return GNUNET_SYSERR on error, otherwise
670 * the number of successfully affected rows
673 GNUNET_MYSQL_statement_run_prepared (struct GNUNET_MYSQL_Context *mc,
674 struct GNUNET_MYSQL_StatementHandle *sh,
675 unsigned long long *insert_id, ...)
680 if (GNUNET_OK != prepare_statement (mc, sh))
681 return GNUNET_SYSERR;
682 va_start (ap, insert_id);
683 if (GNUNET_OK != init_params (mc, sh, ap))
686 return GNUNET_SYSERR;
689 affected = mysql_stmt_affected_rows (sh->statement);
690 if (NULL != insert_id)
691 *insert_id = (unsigned long long) mysql_stmt_insert_id (sh->statement);
692 mysql_stmt_reset (sh->statement);