2 This file is part of GNUnet
3 Copyright (C) 2012 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @author Christian Grothoff
24 * Helper library to access a MySQL database
26 * @defgroup mysql MySQL library
27 * Helper library to access a MySQL database.
30 #ifndef GNUNET_MYSQL_LIB_H
31 #define GNUNET_MYSQL_LIB_H
33 #include "gnunet_util_lib.h"
34 #include <mysql/mysql.h>
39 #if 0 /* keep Emacsens' auto-indent happy */
48 struct GNUNET_MYSQL_Context;
52 * Handle for a prepared statement.
54 struct GNUNET_MYSQL_StatementHandle;
58 * Type of a callback that will be called for each
59 * data set returned from MySQL.
61 * @param cls user-defined argument
62 * @param num_values number of elements in values
63 * @param values values returned by MySQL
64 * @return GNUNET_OK to continue iterating, GNUNET_SYSERR to abort
66 typedef int (*GNUNET_MYSQL_DataProcessor) (void *cls, unsigned int num_values,
71 * Create a mysql context.
73 * @param cfg configuration
74 * @param section configuration section to use to get MySQL configuration options
75 * @return the mysql context
77 struct GNUNET_MYSQL_Context *
78 GNUNET_MYSQL_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
83 * Destroy a mysql context. Also frees all associated prepared statements.
85 * @param mc context to destroy
88 GNUNET_MYSQL_context_destroy (struct GNUNET_MYSQL_Context *mc);
92 * Close database connection and all prepared statements (we got a DB
93 * error). The connection will automatically be re-opened and
94 * statements will be re-prepared if they are needed again later.
96 * @param mc mysql context
99 GNUNET_MYSQL_statements_invalidate (struct GNUNET_MYSQL_Context *mc);
103 * Get internal handle for a prepared statement. This function should rarely
104 * be used, and if, with caution! On failures during the interaction with
105 * the handle, you must call 'GNUNET_MYSQL_statements_invalidate'!
107 * @param sh prepared statement to introspect
108 * @return MySQL statement handle, NULL on error
111 GNUNET_MYSQL_statement_get_stmt (struct GNUNET_MYSQL_StatementHandle *sh);
115 * Prepare a statement. Prepared statements are automatically discarded
116 * when the MySQL context is destroyed.
118 * @param mc mysql context
119 * @param query query text
120 * @return prepared statement, NULL on error
122 struct GNUNET_MYSQL_StatementHandle *
123 GNUNET_MYSQL_statement_prepare (struct GNUNET_MYSQL_Context *mc,
128 * Run a SQL statement.
130 * @param mc mysql context
131 * @param sql SQL statement to run
132 * @return GNUNET_OK on success
133 * GNUNET_SYSERR if there was a problem
136 GNUNET_MYSQL_statement_run (struct GNUNET_MYSQL_Context *mc,
141 * Run a prepared SELECT statement.
143 * @param sh handle to SELECT statment
144 * @param result_size number of elements in results array
145 * @param results pointer to already initialized MYSQL_BIND
146 * array (of sufficient size) for passing results
147 * @param processor function to call on each result
148 * @param processor_cls extra argument to processor
149 * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
150 * values (size + buffer-reference for pointers); terminated
152 * @return GNUNET_SYSERR on error, otherwise
153 * the number of successfully affected (or queried) rows
156 GNUNET_MYSQL_statement_run_prepared_select (struct GNUNET_MYSQL_StatementHandle *sh,
157 unsigned int result_size, MYSQL_BIND * results,
158 GNUNET_MYSQL_DataProcessor processor,
159 void *processor_cls, ...);
163 * Run a prepared SELECT statement.
165 * @param s statement to run
166 * @param result_size number of elements in results array
167 * @param results pointer to already initialized MYSQL_BIND
168 * array (of sufficient size) for passing results
169 * @param processor function to call on each result
170 * @param processor_cls extra argument to processor
171 * @param ap pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
172 * values (size + buffer-reference for pointers); terminated
174 * @return GNUNET_SYSERR on error, otherwise
175 * the number of successfully affected (or queried) rows
178 GNUNET_MYSQL_statement_run_prepared_select_va (struct GNUNET_MYSQL_StatementHandle *s,
179 unsigned int result_size,
180 MYSQL_BIND * results,
181 GNUNET_MYSQL_DataProcessor processor,
187 * Run a prepared statement that does NOT produce results.
189 * @param sh handle to statment
190 * @param insert_id NULL or address where to store the row ID of whatever
191 * was inserted (only for INSERT statements!)
192 * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
193 * values (size + buffer-reference for pointers); terminated
195 * @return GNUNET_SYSERR on error, otherwise
196 * the number of successfully affected rows
199 GNUNET_MYSQL_statement_run_prepared (struct GNUNET_MYSQL_StatementHandle *sh,
200 unsigned long long *insert_id, ...);
203 #if 0 /* keep Emacsens' auto-indent happy */
212 /** @} */ /* end of group */