Files @ 2db3c59f604d
Branch filter:

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

ethanzonca@protofusion.org
Fixed returning multiple statements
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
a1fb4d1ec4fa
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
742325b16d40
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
d5f4334ff4e6
9b974d776960
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
5cc750f10d9d
5cc750f10d9d
5cc750f10d9d
5cc750f10d9d
5cc750f10d9d
5cc750f10d9d
5cc750f10d9d
5cc750f10d9d
5cc750f10d9d
5cc750f10d9d
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
d5f4334ff4e6
d5f4334ff4e6
9b974d776960
9b974d776960
c4234d261cdb
9b974d776960
9b974d776960
9b974d776960
f5a1573ebc08
9b974d776960
9b974d776960
742325b16d40
543a653e60b7
742325b16d40
14cd2541024e
742325b16d40
742325b16d40
9b974d776960
9b974d776960
742325b16d40
2db3c59f604d
9b974d776960
9b974d776960
9b974d776960
9b974d776960
742325b16d40
f5a1573ebc08
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
9b974d776960
9b974d776960
9b974d776960
9b974d776960
742325b16d40
742325b16d40
f5a1573ebc08
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
f5a1573ebc08
742325b16d40
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
d5f4334ff4e6
9b974d776960
9b974d776960
9b974d776960
742325b16d40
742325b16d40
17ee4de48309
f5a1573ebc08
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
f5a1573ebc08
c4234d261cdb
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
9b974d776960
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
c4234d261cdb
d5f4334ff4e6
d5f4334ff4e6
c4234d261cdb
9b974d776960
9b974d776960
9b974d776960
d5f4334ff4e6
d5f4334ff4e6
f5a1573ebc08
f5a1573ebc08
f5a1573ebc08
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
d5f4334ff4e6
a1fb4d1ec4fa
f5a1573ebc08
f5a1573ebc08
a1fb4d1ec4fa
a1fb4d1ec4fa
c4234d261cdb
a1fb4d1ec4fa
50915ba5beb7
50915ba5beb7
a1fb4d1ec4fa
c4234d261cdb
c4234d261cdb
f5a1573ebc08
f5a1573ebc08
c4234d261cdb
c4234d261cdb
c4234d261cdb
c4234d261cdb
c4234d261cdb
c4234d261cdb
c4234d261cdb
c4234d261cdb
c4234d261cdb
c4234d261cdb
c4234d261cdb
c4234d261cdb
c4234d261cdb
c4234d261cdb
f5a1573ebc08
50915ba5beb7
50915ba5beb7
50915ba5beb7
50915ba5beb7
50915ba5beb7
50915ba5beb7
a1fb4d1ec4fa
c4234d261cdb
50915ba5beb7
50915ba5beb7
50915ba5beb7
c4234d261cdb
50915ba5beb7
50915ba5beb7
a1fb4d1ec4fa
50915ba5beb7
50915ba5beb7
50915ba5beb7
50915ba5beb7
c4234d261cdb
a1fb4d1ec4fa
1143afdf4fdd
ffce970408ba
50915ba5beb7
50915ba5beb7
a1fb4d1ec4fa
50915ba5beb7
50915ba5beb7
50915ba5beb7
5cc750f10d9d
5cc750f10d9d
5cc750f10d9d
c4234d261cdb
4e9e4543f9d8
4e9e4543f9d8
4e9e4543f9d8
4e9e4543f9d8
5cc750f10d9d
50915ba5beb7
50915ba5beb7
a1fb4d1ec4fa
a1fb4d1ec4fa
/*
  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 "common/protocol.h"

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

/**
   local types
 */

#define SEVENTY_FIVE 75
#define FORTY_TWO 42

/**
   performs mysql query.
   errors will be logged to the user by this function.
   @return pointer to query handle on success, NULL on failure
 */
distrend_mysql_result_t mysqlQuery(distrend_mysql_conn_t conn, char *query);

/**
   frees mysql query result. Accepts a NULL pointer and ignores it to
   help deal with one-shot calls to mysqlQuery so that you don't have to
   check if it returned NULL or not.
   @return 0 on success
 */
int mysqlResultFree(distrend_mysql_result_t result);

/**
   reads an integer mysql field value
   @return 0 on success
*/
int distrend_mysql_getint(MYSQL_ROW row, MYSQL_FIELD_OFFSET column, int32_t *theint)
{
  *theint = atol(row[column]);

  return 0;
}


struct distrend_mysql_conn
{
  MYSQL *mysqlconn;
  short pointlesscheck;
};

struct distrend_mysql_result
{
  MYSQL_RES *mysqlresult;
  short pointlesscheck;
};

/**
    funcs
 */

int mysqlConnect(distrend_mysql_conn_t *conn)
{
  MYSQL *mysqlconn;

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

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

  if(!mysql_real_connect(mysqlconn, server, user, password, database, 0, NULL, CLIENT_MULTI_STATEMENTS))
    {
      fprintf(stderr, "%s\n", mysql_error(mysqlconn));
      return 1;
    }

  *conn = malloc(sizeof(struct distrend_mysql_conn));
  if(!*conn)
    {
      mysql_close(mysqlconn);
      mysql_server_end();
      return 2;
    }
  (*conn)->mysqlconn = mysqlconn;
  (*conn)->pointlesscheck = SEVENTY_FIVE;

  return 0;
}

int mysqlDisconnect(distrend_mysql_conn_t conn)
{
  /**
     check if this handle is valid
   */
  if(conn->pointlesscheck != SEVENTY_FIVE)
    fprintf(stderr, "warning, I was passed a bad struct distrend_mysql_conn...\n");

  /**
     invalidate handle :-D
   */
  conn->pointlesscheck ++;

  mysql_close(conn->mysqlconn);
  mysql_server_end();
  free(conn);

  return 0;
}


distrend_mysql_result_t mysqlQuery(distrend_mysql_conn_t conn, char *query)
{
  MYSQL_RES *result;
  distrend_mysql_result_t distrenresult;

  /**
     pointless sanity check
  */
  if(conn->pointlesscheck != SEVENTY_FIVE)
    fprintf(stderr, "warning, I was passed a bad struct distrend_mysql_conn...\n");

  /** make sure that connection is still alive
   */
  if(mysql_ping(conn->mysqlconn))
    fprintf(stderr, "MySQL Connection _was_ broken or may be broken, I'm not sure exactly what this return value means\n");

  if (mysql_query(conn->mysqlconn, query))
     fprintf(stderr, "%s\n", mysql_error(conn->mysqlconn));

  result = mysql_use_result(conn->mysqlconn);

  distrenresult = malloc(sizeof(struct distrend_mysql_result));
  if(!distrenresult)
    {
      mysql_free_result(result);
      return NULL;
    }

  distrenresult->mysqlresult = result;
  distrenresult->pointlesscheck = FORTY_TWO;

  return distrenresult;
}

int mysqlResultFree(distrend_mysql_result_t result)
{
  if(!result)
    return 0;

  if(result->pointlesscheck != FORTY_TWO)
    fprintf(stderr, "%s:%d: I didn't get the type of handle I wanted\n", __FILE__, __LINE__);

  mysql_free_result(result->mysqlresult);
  free(result);

  return 0;
}

/*
   Individual query functions:
 */

void finish_frame(distrend_mysql_conn_t conn, int32_t slavekey, int32_t jobkey, int32_t framenum)
{
  char *query;
  distrend_mysql_result_t result;

  _distren_asprintf(&query, "CALL `distren`.`Frame_Complete`(%d,%d,%d);", slavekey, jobkey, framenum);

  result = mysqlQuery(conn, query);
  free(query);

  mysqlResultFree(result);
}

void start_frame(distrend_mysql_conn_t conn, int32_t slavekey, int32_t jobkey, int32_t framenum)
{
  char *query;
  distrend_mysql_result_t result;

  _distren_asprintf(&query, "CALL `distren`.`Frame_Start`(%d,%d,%d);", slavekey, jobkey, framenum);

  result = mysqlQuery(conn, query);
  free(query);

  mysqlResultFree(result);
}

int change_job_priority(distrend_mysql_conn_t conn, int32_t jobkey, int32_t newpriority)
{
  char *query;
  distrend_mysql_result_t result;

  _distren_asprintf(&query, "UPDATE `distren`.`Job` SET `Priority`=%d WHERE `Job_Key`=%d",
		    newpriority, jobkey);
  result = mysqlQuery(conn, query);
  mysqlResultFree(result);

  return 0;
}

int find_jobframe(distrend_mysql_conn_t conn, int32_t slavekey, int32_t jobkey, int32_t framenum)
{
  distrend_mysql_result_t result;
  char *query;
  MYSQL_ROW row;

  _distren_asprintf(&query, "CALL `distren`.`Frame_Get`(%d);", slavekey);

  result = mysqlQuery(conn, query);
  free(query);

  if(!result)
    return 1;

  row = mysql_fetch_row(result->mysqlresult);
  if(!row)
    return 1;

  distrend_mysql_getint(row, 1, &jobkey);
  distrend_mysql_getint(row, 2, &framenum);


  
  mysqlResultFree(result);

  return 0;
}