2 Copyright (c) 2003-4, WebThing Ltd
3 Author: Nick Kew <nick@webthing.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 The current GPL satisfies MySQL licensing terms without
23 invoking any exceptions.
25 This code is untested, and probably mostly won't work.
26 It's a demo of concept.
36 #include <mysql/mysql.h>
37 #include <mysql/errmsg.h>
39 #include "apr_strings.h"
41 #include "apr_dbd_internal.h"
44 struct apr_dbd_prepared_t {
48 struct apr_dbd_transaction_t {
55 apr_dbd_transaction_t* trans ;
58 struct apr_dbd_results_t {
61 MYSQL_STMT *statement;
64 struct apr_dbd_row_t {
66 apr_dbd_results_t *res;
69 static int dbd_mysql_select(apr_pool_t *pool, apr_dbd_t *sql,
70 apr_dbd_results_t **results,
71 const char *query, int seek)
75 if (sql->trans && sql->trans->errnum) {
76 return sql->trans->errnum;
78 ret = mysql_query(sql->conn, query);
80 if (sz = mysql_field_count(sql->conn), sz > 0) {
82 *results = apr_palloc(pool, sizeof(apr_dbd_results_t));
84 (*results)->random = seek;
85 (*results)->statement = NULL;
87 (*results)->res = mysql_store_result(sql->conn);
90 (*results)->res = mysql_use_result(sql->conn);
92 apr_pool_cleanup_register(pool, (*results)->res,
93 (void*)mysql_free_result,
94 apr_pool_cleanup_null);
98 sql->trans->errnum = ret;
102 static int dbd_mysql_get_row(apr_pool_t *pool, apr_dbd_results_t *res,
103 apr_dbd_row_t **row, int rownum)
108 if (res->statement) {
111 mysql_stmt_data_seek(res->statement, (my_ulonglong)rownum);
114 ret = mysql_stmt_fetch(res->statement);
119 mysql_data_seek(res->res, (my_ulonglong) rownum);
122 r = mysql_fetch_row(res->res);
129 *row = apr_palloc(pool, sizeof(apr_dbd_row_t));
135 mysql_free_result(res->res);
136 apr_pool_cleanup_kill(pool, res->res, (void*)mysql_free_result);
142 static int dbd_mysql_get_entry(const apr_dbd_row_t *row, int n,
143 apr_dbd_datum_t *val)
146 if (row->res->statement) {
147 bind = &row->res->bind[n];
148 if (mysql_stmt_fetch_column(row->res->statement, bind, n, 0) != 0) {
149 val->type = APR_DBD_VALUE_NULL;
152 if (*bind->is_null) {
153 val->type = APR_DBD_VALUE_NULL;
157 val->type = APR_DBD_VALUE_STRING;
158 val->value.stringval = bind->buffer;
162 val->type = APR_DBD_VALUE_STRING;
163 val->value.stringval = row->row[n];
168 static const char *dbd_mysql_get_entry(const apr_dbd_row_t *row, int n)
171 if (row->res->statement) {
172 bind = &row->res->bind[n];
173 if (mysql_stmt_fetch_column(row->res->statement, bind, n, 0) != 0) {
176 if (*bind->is_null) {
189 static const char *dbd_mysql_error(apr_dbd_t *sql, int n)
191 return mysql_error(sql->conn);
193 static int dbd_mysql_query(apr_dbd_t *sql, int *nrows, const char *query)
196 if (sql->trans && sql->trans->errnum) {
197 return sql->trans->errnum;
199 ret = mysql_query(sql->conn, query);
200 *nrows = mysql_affected_rows(sql->conn);
202 sql->trans->errnum = ret;
206 static const char *dbd_mysql_escape(apr_pool_t *pool, const char *arg,
209 unsigned long len = strlen(arg);
210 char *ret = apr_palloc(pool, 2*len + 1);
211 mysql_real_escape_string(sql->conn, ret, arg, len);
214 static int dbd_mysql_prepare(apr_pool_t *pool, apr_dbd_t *sql,
215 const char *query, const char *label,
216 apr_dbd_prepared_t **statement)
218 /* Translate from apr_dbd to native query format */
219 char *myquery = apr_pstrdup(pool, query);
222 for (q = query; *q; ++q) {
228 else if (q[1] == '%') {
242 *statement = apr_palloc(pool, sizeof(apr_dbd_prepared_t));
244 (*statement)->stmt = mysql_stmt_init(sql->conn);
245 apr_pool_cleanup_register(pool, *statement, (void*)mysql_stmt_close,
246 apr_pool_cleanup_null);
247 return mysql_stmt_prepare((*statement)->stmt, myquery, strlen(myquery));
249 static int dbd_mysql_pquery(apr_pool_t *pool, apr_dbd_t *sql,
250 int *nrows, apr_dbd_prepared_t *statement,
251 int nargs, const char **values)
257 my_bool is_null = FALSE;
259 if (sql->trans && sql->trans->errnum) {
260 return sql->trans->errnum;
262 nargs = mysql_stmt_param_count(statement->stmt);
264 bind = apr_palloc(pool, nargs*sizeof(MYSQL_BIND));
265 for (i=0; i < nargs; ++i) {
266 arg = (char*)values[i];
267 bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
268 bind[i].buffer = arg;
269 bind[i].buffer_length = strlen(arg);
270 bind[i].length = &bind[i].buffer_length;
271 bind[i].is_null = &is_null;
272 bind[i].is_unsigned = 0;
275 ret = mysql_stmt_bind_param(statement->stmt, bind);
280 ret = mysql_stmt_execute(statement->stmt);
281 *nrows = mysql_stmt_affected_rows(statement->stmt);
284 sql->trans->errnum = ret;
288 static int dbd_mysql_pvquery(apr_pool_t *pool, apr_dbd_t *sql, int *nrows,
289 apr_dbd_prepared_t *statement, va_list args)
296 my_bool is_null = FALSE;
298 if (sql->trans && sql->trans->errnum) {
299 return sql->trans->errnum;
301 nargs = mysql_stmt_param_count(statement->stmt);
303 bind = apr_palloc(pool, nargs*sizeof(MYSQL_BIND));
304 for (i=0; i < nargs; ++i) {
305 arg = va_arg(args, char*);
306 bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
307 bind[i].buffer = arg;
308 bind[i].buffer_length = strlen(arg);
309 bind[i].length = &bind[i].buffer_length;
310 bind[i].is_null = &is_null;
311 bind[i].is_unsigned = 0;
314 ret = mysql_stmt_bind_param(statement->stmt, bind);
319 ret = mysql_stmt_execute(statement->stmt);
320 *nrows = mysql_stmt_affected_rows(statement->stmt);
323 sql->trans->errnum = ret;
327 static int dbd_mysql_pselect(apr_pool_t *pool, apr_dbd_t *sql,
328 apr_dbd_results_t **res,
329 apr_dbd_prepared_t *statement, int random,
330 int nargs, const char **args)
335 my_bool is_null = FALSE;
338 const int FIELDSIZE = 255;
339 unsigned long *length;
343 if (sql->trans && sql->trans->errnum) {
344 return sql->trans->errnum;
347 nargs = mysql_stmt_param_count(statement->stmt);
348 bind = apr_palloc(pool, nargs*sizeof(MYSQL_BIND));
350 for (i=0; i < nargs; ++i) {
351 arg = (char*)args[i];
352 bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
353 bind[i].buffer = arg;
354 bind[i].buffer_length = strlen(arg);
355 bind[i].length = &bind[i].buffer_length;
356 bind[i].is_null = &is_null;
357 bind[i].is_unsigned = 0;
360 ret = mysql_stmt_bind_param(statement->stmt, bind);
362 ret = mysql_stmt_execute(statement->stmt);
365 *res = apr_pcalloc(pool, sizeof(apr_dbd_results_t));
367 while (!mysql_stmt_fetch(statement->stmt));
371 (*res)->random = random;
372 (*res)->statement = statement->stmt;
373 (*res)->res = mysql_stmt_result_metadata(statement->stmt);
374 apr_pool_cleanup_register(pool, (*res)->res,
375 (void*)mysql_free_result, apr_pool_cleanup_null);
376 nfields = mysql_num_fields((*res)->res);
378 (*res)->bind = apr_palloc(pool, nfields*sizeof(MYSQL_BIND));
379 length = apr_pcalloc(pool, nfields*sizeof(unsigned long));
380 data = apr_palloc(pool, nfields*sizeof(char*));
381 is_nullr = apr_pcalloc(pool, nfields*sizeof(my_bool));
382 length = apr_pcalloc(pool, nfields);
383 for ( i = 0; i < nfields; ++i ) {
384 (*res)->bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
385 (*res)->bind[i].buffer_length = FIELDSIZE;
386 (*res)->bind[i].length = &length[i];
387 data[i] = apr_palloc(pool, FIELDSIZE*sizeof(char));
388 (*res)->bind[i].buffer = data[i];
389 (*res)->bind[i].is_null = is_nullr+i;
392 ret = mysql_stmt_bind_result(statement->stmt, (*res)->bind);
394 ret = mysql_stmt_store_result(statement->stmt);
399 sql->trans->errnum = ret;
403 static int dbd_mysql_pvselect(apr_pool_t *pool, apr_dbd_t *sql,
404 apr_dbd_results_t **res,
405 apr_dbd_prepared_t *statement, int random,
411 my_bool is_null = FALSE;
414 const int FIELDSIZE = 255;
415 unsigned long *length;
420 if (sql->trans && sql->trans->errnum) {
421 return sql->trans->errnum;
424 nargs = mysql_stmt_param_count(statement->stmt);
425 bind = apr_palloc(pool, nargs*sizeof(MYSQL_BIND));
427 for (i=0; i < nargs; ++i) {
428 arg = va_arg(args, char*);
429 bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
430 bind[i].buffer = arg;
431 bind[i].buffer_length = strlen(arg);
432 bind[i].length = &bind[i].buffer_length;
433 bind[i].is_null = &is_null;
434 bind[i].is_unsigned = 0;
437 ret = mysql_stmt_bind_param(statement->stmt, bind);
439 ret = mysql_stmt_execute(statement->stmt);
442 *res = apr_pcalloc(pool, sizeof(apr_dbd_results_t));
444 while (!mysql_stmt_fetch(statement->stmt));
448 (*res)->random = random;
449 (*res)->statement = statement->stmt;
450 (*res)->res = mysql_stmt_result_metadata(statement->stmt);
451 apr_pool_cleanup_register(pool, (*res)->res,
452 (void*)mysql_free_result, apr_pool_cleanup_null);
453 nfields = mysql_num_fields((*res)->res);
455 (*res)->bind = apr_palloc(pool, nfields*sizeof(MYSQL_BIND));
456 length = apr_pcalloc(pool, nfields*sizeof(unsigned long));
457 data = apr_palloc(pool, nfields*sizeof(char*));
458 is_nullr = apr_pcalloc(pool, nfields*sizeof(my_bool));
459 length = apr_pcalloc(pool, nfields);
460 for ( i = 0; i < nfields; ++i ) {
461 (*res)->bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
462 (*res)->bind[i].buffer_length = FIELDSIZE;
463 (*res)->bind[i].length = &length[i];
464 data[i] = apr_palloc(pool, FIELDSIZE*sizeof(char));
465 (*res)->bind[i].buffer = data[i];
466 (*res)->bind[i].is_null = is_nullr+i;
469 ret = mysql_stmt_bind_result(statement->stmt, (*res)->bind);
471 ret = mysql_stmt_store_result(statement->stmt);
476 sql->trans->errnum = ret;
480 static int dbd_mysql_end_transaction(apr_dbd_transaction_t *trans)
486 ret = mysql_rollback(trans->handle->conn);
489 ret = mysql_commit(trans->handle->conn);
492 ret |= mysql_autocommit(trans->handle->conn, 1);
495 /* Whether or not transactions work depends on whether the
496 * underlying DB supports them within MySQL. Unfortunately
497 * it fails silently with the default InnoDB.
499 static int dbd_mysql_transaction(apr_pool_t *pool, apr_dbd_t *handle,
500 apr_dbd_transaction_t **trans)
502 /* Don't try recursive transactions here */
504 dbd_mysql_end_transaction(handle->trans) ;
507 *trans = apr_pcalloc(pool, sizeof(apr_dbd_transaction_t));
509 (*trans)->errnum = mysql_autocommit(handle->conn, 0);
510 (*trans)->handle = handle;
511 handle->trans = *trans;
512 return (*trans)->errnum;
514 static apr_dbd_t *dbd_mysql_open(apr_pool_t *pool, const char *params)
516 static const char *const delims = " \r\n\t;|,";
535 unsigned int port = 0;
536 apr_dbd_t *sql = apr_pcalloc(pool, sizeof(apr_dbd_t));
537 sql->conn = mysql_init(sql->conn);
538 if ( sql->conn == NULL ) {
541 for (ptr = strchr(params, '='); ptr; ptr = strchr(ptr, '=')) {
542 for (key = ptr-1; isspace(*key); --key);
544 while (isalpha(*key)) {
549 for (value = ptr+1; isspace(*value); ++value);
550 vlen = strcspn(value, delims);
551 for (i=0; fields[i].field != NULL; ++i) {
552 if (!strncasecmp(fields[i].field, key, klen)) {
553 fields[i].value = apr_pstrndup(pool, value, vlen);
559 if (fields[4].value != NULL) {
560 port = atoi(fields[4].value);
562 sql->conn = mysql_real_connect(sql->conn, fields[0].value,
563 fields[1].value, fields[2].value,
564 fields[3].value, port,
566 return sql->conn ? sql : NULL;
568 static apr_status_t dbd_mysql_close(apr_dbd_t *handle)
570 mysql_close(handle->conn);
573 static apr_status_t dbd_mysql_check_conn(apr_pool_t *pool,
576 return mysql_ping(handle->conn) ? APR_EGENERAL : APR_SUCCESS;
578 static int dbd_mysql_select_db(apr_pool_t *pool, apr_dbd_t* handle,
581 return mysql_select_db(handle->conn, name);
583 static void *dbd_mysql_native(apr_dbd_t *handle)
587 static int dbd_mysql_num_cols(apr_dbd_results_t *res)
589 if (res->statement) {
590 return mysql_stmt_field_count(res->statement);
593 return mysql_num_fields(res->res);
596 static int dbd_mysql_num_tuples(apr_dbd_results_t *res)
599 if (res->statement) {
600 return (int) mysql_stmt_num_rows(res->statement);
603 return (int) mysql_num_rows(res->res);
610 static void dbd_mysql_init(apr_pool_t *pool)
613 /* FIXME: this is a guess; find out what it really does */
614 apr_pool_cleanup_register(pool, NULL, apr_pool_cleanup_null,
615 (void*)mysql_thread_end);
617 APU_DECLARE_DATA const apr_dbd_driver_t apr_dbd_mysql_driver = {
622 dbd_mysql_check_conn,
625 dbd_mysql_transaction,
626 dbd_mysql_end_transaction,
630 dbd_mysql_num_tuples,