#include <mysql/mysql.h>
#include "stralloc.h"
#include "qsutil.h"

extern MYSQL dbh, *mysql;
extern MYSQL_RES *result;

/* run a given query and return 1 row only */
int do_query(stralloc *query, MYSQL_ROW *row) {
  int num;

  if (! connect_mysql()) return -1;
#ifndef O_NOT_LOG
  tcplog("query: ", query->s, ";\n");
#endif
  if (mysql_real_query(mysql, query->s, query->len) < 0) {
    /* bad => log it anyway */
    tcplog("error: ", mysql_error(&dbh), "\n");
    return -1;
  }
  stralloc_free(query);
  if (! (result = mysql_store_result(mysql))) return -1;

  num = mysql_num_rows(result);

  if (num != 1) {
    mysql_free_result(result);
    return 0;
  }

  *row = mysql_fetch_row(result);

  return 1;
}


syntax highlighted by Code2HTML, v. 0.9.1