3 This file is part of GNUnet
4 Copyright (C) 2012 GNUnet e.V.
6 GNUnet is free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published
8 by the Free Software Foundation, either version 3 of the License,
9 or (at your option) any later version.
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Affero General Public License for more details.
18 * @brief library to help with access to a MySQL database
19 * @author Christian Grothoff
22 #include <mysql/mysql.h>
23 #include "gnunet_mysql_lib.h"
26 * Maximum number of supported parameters for a prepared
27 * statement. Increase if needed.
33 * Die with an error message that indicates
34 * a failure of the command 'cmd' with the message given
37 #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_assert (0); } while(0);
40 * Log an error message at log-level 'level' that indicates
41 * a failure of the command 'cmd' on file 'filename'
42 * with the message given by strerror(errno).
44 #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);
50 struct GNUNET_MYSQL_Context
56 const struct GNUNET_CONFIGURATION_Handle *cfg;
64 * Handle to the mysql database.
69 * Head of list of our prepared statements.
71 struct GNUNET_MYSQL_StatementHandle *shead;
74 * Tail of list of our prepared statements.
76 struct GNUNET_MYSQL_StatementHandle *stail;
79 * Filename of "my.cnf" (msyql configuration).
87 * Handle for a prepared statement.
89 struct GNUNET_MYSQL_StatementHandle
95 struct GNUNET_MYSQL_StatementHandle *next;
100 struct GNUNET_MYSQL_StatementHandle *prev;
103 * Mysql Context the statement handle belongs to.
105 struct GNUNET_MYSQL_Context *mc;
108 * Original query string.
113 * Handle to MySQL prepared statement.
115 MYSQL_STMT *statement;
118 * Is the MySQL prepared statement valid, or do we need to re-initialize it?
126 * Obtain the location of ".my.cnf".
128 * @param cfg our configuration
129 * @param section the section
130 * @return NULL on error
133 get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
146 pw = getpwuid (getuid ());
149 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR, "mysql", "getpwuid");
152 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg, section, "CONFIG"))
154 GNUNET_assert (GNUNET_OK ==
155 GNUNET_CONFIGURATION_get_value_filename (cfg, section,
158 configured = GNUNET_YES;
162 home_dir = GNUNET_strdup (pw->pw_dir);
163 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
164 GNUNET_free (home_dir);
165 configured = GNUNET_NO;
168 home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1);
169 plibc_conv_to_win_path ("~/", home_dir);
170 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
171 GNUNET_free (home_dir);
172 configured = GNUNET_NO;
174 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "mysql",
175 _("Trying to use file `%s' for MySQL configuration.\n"),
177 if ((0 != STAT (cnffile, &st)) || (0 != ACCESS (cnffile, R_OK)) ||
178 (!S_ISREG (st.st_mode)))
180 if (configured == GNUNET_YES)
181 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
182 _("Could not access file `%s': %s\n"), cnffile,
184 GNUNET_free (cnffile);
192 * Open the connection with the database (and initialize
193 * our default options).
195 * @param mc database context to initialze
196 * @return #GNUNET_OK on success
199 iopen (struct GNUNET_MYSQL_Context *mc)
204 char *mysql_password;
205 unsigned long long mysql_port;
207 unsigned int timeout;
209 mc->dbf = mysql_init (NULL);
211 return GNUNET_SYSERR;
212 if (mc->cnffile != NULL)
213 mysql_options (mc->dbf, MYSQL_READ_DEFAULT_FILE, mc->cnffile);
214 mysql_options (mc->dbf, MYSQL_READ_DEFAULT_GROUP, "client");
216 mysql_options (mc->dbf, MYSQL_OPT_RECONNECT, &reconnect);
217 mysql_options (mc->dbf, MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout);
218 mysql_options (mc->dbf, MYSQL_SET_CHARSET_NAME, "UTF8");
219 timeout = 60; /* in seconds */
220 mysql_options (mc->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
221 mysql_options (mc->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
224 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "DATABASE"))
225 GNUNET_assert (GNUNET_OK ==
226 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
230 mysql_dbname = GNUNET_strdup ("gnunet");
233 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "USER"))
235 GNUNET_assert (GNUNET_OK ==
236 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
237 "USER", &mysql_user));
239 mysql_password = NULL;
241 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PASSWORD"))
243 GNUNET_assert (GNUNET_OK ==
244 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
250 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "HOST"))
252 GNUNET_assert (GNUNET_OK ==
253 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section,
259 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PORT"))
261 GNUNET_assert (GNUNET_OK ==
262 GNUNET_CONFIGURATION_get_value_number (mc->cfg, mc->section,
263 "PORT", &mysql_port));
266 GNUNET_assert (mysql_dbname != NULL);
267 mysql_real_connect (mc->dbf, mysql_server, mysql_user, mysql_password,
268 mysql_dbname, (unsigned int) mysql_port, NULL,
269 CLIENT_IGNORE_SIGPIPE);
270 GNUNET_free_non_null (mysql_server);
271 GNUNET_free_non_null (mysql_user);
272 GNUNET_free_non_null (mysql_password);
273 GNUNET_free (mysql_dbname);
274 if (mysql_error (mc->dbf)[0])
276 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_real_connect", mc);
277 return GNUNET_SYSERR;
284 * Create a mysql context.
286 * @param cfg configuration
287 * @param section configuration section to use to get MySQL configuration options
288 * @return the mysql context
290 struct GNUNET_MYSQL_Context *
291 GNUNET_MYSQL_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
294 struct GNUNET_MYSQL_Context *mc;
296 mc = GNUNET_new (struct GNUNET_MYSQL_Context);
298 mc->section = section;
299 mc->cnffile = get_my_cnf_path (cfg,
307 * Close database connection and all prepared statements (we got a DB
310 * @param mc mysql context
313 GNUNET_MYSQL_statements_invalidate (struct GNUNET_MYSQL_Context *mc)
315 struct GNUNET_MYSQL_StatementHandle *sh;
317 for (sh = mc->shead; NULL != sh; sh = sh->next)
319 if (GNUNET_YES == sh->valid)
321 mysql_stmt_close (sh->statement);
322 sh->valid = GNUNET_NO;
324 sh->statement = NULL;
328 mysql_close (mc->dbf);
335 * Destroy a mysql context. Also frees all associated prepared statements.
337 * @param mc context to destroy
340 GNUNET_MYSQL_context_destroy (struct GNUNET_MYSQL_Context *mc)
342 struct GNUNET_MYSQL_StatementHandle *sh;
344 GNUNET_MYSQL_statements_invalidate (mc);
345 while (NULL != (sh = mc->shead))
347 GNUNET_CONTAINER_DLL_remove (mc->shead, mc->stail, sh);
348 GNUNET_free (sh->query);
352 mysql_library_end ();
357 * Prepare a statement. Prepared statements are automatically discarded
358 * when the MySQL context is destroyed.
360 * @param mc mysql context
361 * @param query query text
362 * @return prepared statement, NULL on error
364 struct GNUNET_MYSQL_StatementHandle *
365 GNUNET_MYSQL_statement_prepare (struct GNUNET_MYSQL_Context *mc,
368 struct GNUNET_MYSQL_StatementHandle *sh;
370 sh = GNUNET_new (struct GNUNET_MYSQL_StatementHandle);
372 sh->query = GNUNET_strdup (query);
373 GNUNET_CONTAINER_DLL_insert (mc->shead, mc->stail, sh);
379 * Run a SQL statement.
381 * @param mc mysql context
382 * @param sql SQL statement to run
383 * @return #GNUNET_OK on success
384 * #GNUNET_SYSERR if there was a problem
387 GNUNET_MYSQL_statement_run (struct GNUNET_MYSQL_Context *mc,
390 if ( (NULL == mc->dbf) &&
391 (GNUNET_OK != iopen (mc)) )
392 return GNUNET_SYSERR;
393 mysql_query (mc->dbf, sql);
394 if (mysql_error (mc->dbf)[0])
396 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR,
399 GNUNET_MYSQL_statements_invalidate (mc);
400 return GNUNET_SYSERR;
407 * Prepare a statement for running.
409 * @param mc mysql context
410 * @param sh statement handle to prepare
411 * @return #GNUNET_OK on success
414 prepare_statement (struct GNUNET_MYSQL_StatementHandle *sh)
416 struct GNUNET_MYSQL_Context *mc = sh->mc;
418 if (GNUNET_YES == sh->valid)
420 if ((NULL == mc->dbf) && (GNUNET_OK != iopen (mc)))
421 return GNUNET_SYSERR;
422 sh->statement = mysql_stmt_init (mc->dbf);
423 if (NULL == sh->statement)
425 GNUNET_MYSQL_statements_invalidate (mc);
426 return GNUNET_SYSERR;
428 if (0 != mysql_stmt_prepare (sh->statement, sh->query, strlen (sh->query)))
430 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
431 "prepare_statement: %s\n", sh->query);
432 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", mc);
433 mysql_stmt_close (sh->statement);
434 sh->statement = NULL;
435 GNUNET_MYSQL_statements_invalidate (mc);
436 return GNUNET_SYSERR;
438 sh->valid = GNUNET_YES;
444 * Get internal handle for a prepared statement. This function should rarely
445 * be used, and if, with caution! On failures during the interaction with
446 * the handle, you must call 'GNUNET_MYSQL_statements_invalidate'!
448 * @param sh prepared statement to introspect
449 * @return MySQL statement handle, NULL on error
452 GNUNET_MYSQL_statement_get_stmt (struct GNUNET_MYSQL_StatementHandle *sh)
454 (void) prepare_statement (sh);
455 return sh->statement;