26 #include <mysql/mysql.h> 41 #define DIE_MYSQL(cmd, dbh) \ 44 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, \ 46 _ ("`%s' failed at %s:%d with error: %s\n"), \ 50 mysql_error ((dbh)->dbf)); \ 59 #define LOG_MYSQL(level, cmd, dbh) \ 62 GNUNET_log_from (level, \ 64 _ ("`%s' failed at %s:%d with error: %s\n"), \ 68 mysql_error ((dbh)->dbf)); \ 165 pw = getpwuid (getuid ());
190 _ (
"Trying to use file `%s' for MySQL configuration.\n"),
192 if ((0 != stat (cnffile, &st)) || (0 != access (cnffile, R_OK)) ||
193 (! S_ISREG (st.st_mode)))
198 _ (
"Could not access file `%s': %s\n"),
221 char *mysql_password;
222 unsigned long long mysql_port;
226 mc->
dbf = mysql_init (NULL);
230 mysql_options (mc->
dbf, MYSQL_READ_DEFAULT_FILE, mc->
cnffile);
231 mysql_options (mc->
dbf, MYSQL_READ_DEFAULT_GROUP,
"client");
233 mysql_options (mc->
dbf, MYSQL_OPT_RECONNECT, &reconnect);
234 mysql_options (mc->
dbf, MYSQL_OPT_CONNECT_TIMEOUT, (
const void *) &timeout);
235 mysql_options (mc->
dbf, MYSQL_SET_CHARSET_NAME,
"UTF8");
237 mysql_options (mc->
dbf, MYSQL_OPT_READ_TIMEOUT, (
const void *) &timeout);
238 mysql_options (mc->
dbf, MYSQL_OPT_WRITE_TIMEOUT, (
const void *) &timeout);
259 mysql_password = NULL;
291 mysql_real_connect (mc->
dbf,
296 (
unsigned int) mysql_port,
298 CLIENT_IGNORE_SIGPIPE);
303 if (mysql_error (mc->
dbf)[0])
345 for (sh = mc->
shead; NULL != sh; sh = sh->
next)
356 mysql_close (mc->
dbf);
373 while (NULL != (sh = mc->
shead))
380 mysql_library_end ();
419 mysql_query (mc->
dbf, sql);
420 if (mysql_error (mc->
dbf)[0])
456 "prepare_statement: %s\n",
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct GNUNET_MYSQL_Context * GNUNET_MYSQL_context_create(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Create a mysql context.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
MYSQL_STMT * statement
Handle to MySQL prepared statement.
#define GNUNET_log_from_strerror(level, component, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
struct GNUNET_MYSQL_StatementHandle * prev
Kept in a DLL.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
char * query
Original query string.
char * cnffile
Filename of "my.cnf" (msyql configuration).
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
Handle for a prepared statement.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static int prepare_statement(struct GNUNET_MYSQL_StatementHandle *sh)
Prepare a statement for running.
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
void GNUNET_MYSQL_context_destroy(struct GNUNET_MYSQL_Context *mc)
Destroy a mysql context.
static struct SolverHandle * sh
MYSQL_STMT * GNUNET_MYSQL_statement_get_stmt(struct GNUNET_MYSQL_StatementHandle *sh)
Get internal handle for a prepared statement.
void GNUNET_MYSQL_statements_invalidate(struct GNUNET_MYSQL_Context *mc)
Close database connection and all prepared statements (we got a DB error).
int valid
Is the MySQL prepared statement valid, or do we need to re-initialize it?
static struct GNUNET_TESTBED_Controller * mc
Handle to the master controller.
static void reconnect(struct GNUNET_ABD_Handle *handle)
Reconnect to ABD service.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
MYSQL * dbf
Handle to the mysql database.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_have_value(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Test if we have a value for a particular option.
struct GNUNET_MYSQL_StatementHandle * GNUNET_MYSQL_statement_prepare(struct GNUNET_MYSQL_Context *mc, const char *query)
Prepare a statement.
static char * get_my_cnf_path(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Obtain the location of ".my.cnf".
struct GNUNET_MYSQL_Context * mc
Mysql Context the statement handle belongs to.
int GNUNET_MYSQL_statement_run(struct GNUNET_MYSQL_Context *mc, const char *sql)
Run a SQL statement.
struct GNUNET_MYSQL_StatementHandle * stail
Tail of list of our prepared statements.
const char * section
Our section.
#define LOG_MYSQL(level, cmd, dbh)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'file...
struct GNUNET_MYSQL_StatementHandle * next
Kept in a DLL.
static int iopen(struct GNUNET_MYSQL_Context *mc)
Open the connection with the database (and initialize our default options).
#define GNUNET_log_from(kind, comp,...)
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_MYSQL_StatementHandle * shead
Head of list of our prepared statements.