Newer
Older
tuvebridge / mysql.c
@reddawg reddawg on 6 Feb 2009 1 KB Sync
/*
 (C) 2007 Christopher Olsen

 $Id$
*/

#include <stdio.h>

#include "tuvebridge.h"

MYSQL *conn = 0x0;

pthread_mutex_t mysqlMutex = PTHREAD_MUTEX_INITIALIZER;


int dbInit() {

  conn = mysql_init(NULL);
  if (conn == NULL) {
    writeLog(0,"mysql_init() failed (probably out of memory)\n.");
    return(0x1);
    }

  if (!mysql_real_connect(conn,MYSQL_HOST_NAME,MYSQL_USERNAME,MYSQL_PASSWORD,MYSQL_DB_NAME,0,NULL,0)) {
    writeLog(0,"mysql_real_connect() failed: Error %u (%s)\n.", mysql_errno(conn), mysql_error(conn));
    return(0x1);
    }

  if (mysql_options(conn,MYSQL_OPT_RECONNECT,"true") != 0x0) {
    writeLog(0,"mysql_options() failed: To Set Reconnect\n");
    }

  pthread_mutex_init(&mysqlMutex, NULL);

  writeLog(0,"Sucessful connection to the MySQL Database.\n");
  
  return(0x0);
  }

MYSQL_RES *dbQuery(const char *query,short store) {
  MYSQL_RES *res_set = 0x0;

  pthread_mutex_lock(&mysqlMutex);

  writeLog(0,"Query: %s\n",query);

  if (mysql_query(conn,query) != 0x0) {
    writeLog(0,"mysql_query() failed: Error %u (%s)\n.", mysql_errno(conn), mysql_error(conn));
    pthread_mutex_unlock(&mysqlMutex);
    return(0x0);
    }

  if (store == 1) {
  res_set = mysql_store_result(conn);
    if (res_set == 0x0) {
      writeLog(0,"mysql_store_result() failed: Error %u (%s)\n.", mysql_errno(conn), mysql_error(conn));
      pthread_mutex_unlock(&mysqlMutex);
      return(0x0);
      }
    }

  pthread_mutex_unlock(&mysqlMutex);
  return(res_set);
  }