/* 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 . */ #include "mysql.h" #include #include "common/asprintf.h" #include #include #include #include #include #include /** local types */ #define SEVENTY_FIVE 75 struct distrend_mysql_con { MYSQL *mysqlconn; short pointlesscheck; }; struct distrend_mysql_result { MYSQL_RES *result; }; /** funcs */ int mysqlConnect(struct distrend_mysql_con **conn) { MYSQL *mysqlconn; char *server = "zserver1"; char *user = "distren"; char *password = "secretpassword"; 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, 0)) { fprintf(stderr, "%s\n", mysql_error(mysqlconn)); return 1; } *conn = malloc(sizeof(struct distrend_mysql_con)); (*conn)->mysqlconn = mysqlconn; (*conn)->pointlesscheck = SEVENTY_FIVE; return 0; } int mysqlDisconnect(distrend_mysql_con_t conn) { /** check if this handle is valid */ if(conn->pointlesscheck != SEVENTY_FIVE) fprintf(stderr, "warning, I was passed a bad struct distrend_mysql_con...\n"); /** invalidate handle :-D */ conn->pointlesscheck ++; mysql_close(conn->mysqlconn); free(conn); return 0; } distrend_mysql_result_t mysqlQuery(distrend_mysql_con_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_con...\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)); return distrenresult; }