diff --git a/src/server/mysql.c b/src/server/mysql.c new file mode 100644 --- /dev/null +++ b/src/server/mysql.c @@ -0,0 +1,73 @@ +/* + 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 + +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; +}