Files @ afc21acbe5ef
Branch filter:

Location: DistRen/src/server/mysql.c - annotation

ethanzonca
Commenting, more cleanup
/*
  Copyright 2009 Nathan Phillip Brink, Ethan Zonca, Matthew Orlando

  This file is a part of DistRen.

  DistRen is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  DistRen is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.

*/

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

#include "common/asprintf.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>

MYSQL mysqlConnect(){
  MYSQL *conn;

  char *server = "zserver1";
  char *user = "distren";
  char *password = "secretpassword";
  char *database = "distren";

  conn = mysql_init(NULL);
  mysql_options(conn, MYSQL_OPT_RECONNECT,"true");

  if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    fprintf(stderr, "%s\n", mysql_error(conn));

  return *conn;
}

MYSQL_RES mysqlQuery(MYSQL *conn, char *query){
  if (mysql_query(conn, query))
     fprintf(stderr, "%s\n", mysql_error(conn));

  MYSQL_RES *result;
  result = mysql_use_result(conn);
  return *result;
}

// Check if connection is alive, reconnect if broken.
int mysqlPing(MYSQL *conn){
  if(mysql_ping(conn)){
    fprintf(stderr, "MySQL Connection broken, attempting reconnection...");
    return 1;
  }
  else
    return 0; // Connection is still alive
}

int mysqlFree(MYSQL_RES *res, MYSQL *conn){
  mysql_free_result(res);
  mysql_close(conn);
  return 0;
}