# HG changeset patch # User ethanzonca # Date 2010-02-09 18:24:32 # Node ID 742325b16d40547ac98c365dde3369cb019e32e8 # Parent 142eaf297d7d52bbcec246e31f2187be8f6d3067 Added mysql wrapper functions diff --git a/Makefile.am b/Makefile.am --- a/Makefile.am +++ b/Makefile.am @@ -60,7 +60,9 @@ distrend_SOURCES = $(SERVER_SOURCES) \ src/server/user_mgr.c \ src/server/user_mgr.h \ src/server/listen.h \ - src/server/listen.c + src/server/listen.c \ + src/server/mysql.h \ + src/server/mysql.c distrend_LDADD = libdistrencommon.la # distrenslave: distrenslave_SOURCES = $(SERVER_SOURCES) \ diff --git a/src/server/distrend.c b/src/server/distrend.c --- a/src/server/distrend.c +++ b/src/server/distrend.c @@ -23,6 +23,7 @@ #include "distrenjob.h" #include "listen.h" #include "slavefuncs.h" +#include "mysql.h" #include "common/asprintf.h" #include "common/execio.h" 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; +} diff --git a/src/server/mysql.h b/src/server/mysql.h new file mode 100644 --- /dev/null +++ b/src/server/mysql.h @@ -0,0 +1,32 @@ +/* + 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 . + +*/ + + +#ifndef MYSQL_H_ +#define MYSQL_H_ + +#include + +MYSQL mysqlConnect(); +MYSQL_RES mysqlQuery(MYSQL *conn, char *query); +MYSQL_RES mysqlQuery(MYSQL *conn, char *query); +int mysqlFree(MYSQL_RES *res, MYSQL *conn); + +#endif /* MYSQL_H_ */