From 717470a27cccefc7ecd609d8449059a1e7579d49 Mon Sep 17 00:00:00 2001 From: pk910 Date: Sun, 14 Aug 2011 02:52:32 +0200 Subject: [PATCH] added mysql connector --- Makefile | 25 +++++++++++++------------ config.h | 6 ++++++ main.c | 3 +++ main.h | 2 ++ mysqlConn.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ mysqlConn.h | 14 ++++++++++++++ 6 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 config.h create mode 100644 mysqlConn.c create mode 100644 mysqlConn.h diff --git a/Makefile b/Makefile index 78835ed..3f6a6eb 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,21 @@ CFLAGS=-Wall -Wshadow -Werror all: - gcc -g -O2 -I. -c IRCEvents.c -o IRCEvents.o ${CFLAGS} - gcc -g -O2 -I. -c main.c -o main.o ${CFLAGS} - gcc -g -O2 -I. -c ChanNode.c -o ChanNode.o ${CFLAGS} - gcc -g -O2 -I. -c IRCParser.c -o IRCParser.o ${CFLAGS} - gcc -g -O2 -I. -c ClientSocket.c -o ClientSocket.o ${CFLAGS} - gcc -g -O2 -I. -c UserNode.c -o UserNode.o ${CFLAGS} - gcc -g -O2 -I. -c ChanUser.c -o ChanUser.o ${CFLAGS} - gcc -g -O2 -I. -c WHOHandler.c -o WHOHandler.o ${CFLAGS} - gcc -g -O2 -I. -c modcmd.c -o modcmd.o ${CFLAGS} - gcc -g -O2 -I. -c bots.c -o bots.o ${CFLAGS} - gcc -g -O2 -I. -c bot_NeonServ.c -o bot_NeonServ.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c IRCEvents.c -o IRCEvents.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c main.c -o main.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c ChanNode.c -o ChanNode.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c IRCParser.c -o IRCParser.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c ClientSocket.c -o ClientSocket.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c UserNode.c -o UserNode.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c ChanUser.c -o ChanUser.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c WHOHandler.c -o WHOHandler.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c modcmd.c -o modcmd.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c mysqlConn.c -o mysqlConn.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c bots.c -o bots.o ${CFLAGS} + gcc -g -O2 -I. -I/usr/include/mysql -c bot_NeonServ.c -o bot_NeonServ.o ${CFLAGS} install: - gcc -g -O0 -I. -o neonserv *.o ${CFLAGS} + gcc -g -O0 -I. -I/usr/include/mysql -o neonserv *.o ${CFLAGS} -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto clean: rm *.o diff --git a/config.h b/config.h new file mode 100644 index 0000000..c2896e3 --- /dev/null +++ b/config.h @@ -0,0 +1,6 @@ + +#define MYSQL_HOST "" +#define MYSQL_PORT 3306 +#define MYSQL_USER "" +#define MYSQL_PASS "" +#define MYSQL_BASE "" diff --git a/main.c b/main.c index 1dd4950..0870d96 100644 --- a/main.c +++ b/main.c @@ -8,6 +8,7 @@ #include "modcmd.h" #include "WHOHandler.h" #include "bots.h" +#include "mysqlConn.h" void cleanup() { free_sockets(); @@ -18,10 +19,12 @@ void cleanup() { free_modcmd(); free_whoqueue(); free_bots(); + free_mysql(); } int main(void) { + init_mysql(); init_parser(); init_UserNode(); init_ChanNode(); diff --git a/main.h b/main.h index 7efb1cd..c154da3 100644 --- a/main.h +++ b/main.h @@ -1,6 +1,8 @@ #ifndef _main_h #define _main_h +#include "config.h" + #include #include #include diff --git a/mysqlConn.c b/mysqlConn.c new file mode 100644 index 0000000..2dbd608 --- /dev/null +++ b/mysqlConn.c @@ -0,0 +1,54 @@ + +#include "mysqlConn.h" + +struct used_result { + MYSQL_RES *result; + struct used_result *next; +}; + +static MYSQL *mysql_conn = NULL; +static struct used_result *used_results; + +MYSQL *getMySQL() { + return mysql_conn; +} + +void check_mysql() { + if(mysql_ping(mysql_conn)) { + //mysql error + } +} + +MYSQL_RES *mysql_use() { + MYSQL_RES *res = mysql_use_result(mysql_conn); + struct used_result *result = malloc(sizeof(*result)); + if (!result) { + mysql_free_result(res); + return NULL; + } + result->result = res; + result->next = used_results; + used_results = result; + return res; +} + +void mysql_free() { + struct used_result *result, *next; + for(result = used_results; result; result = next) { + next = result->next; + mysql_free_result(result->result); + free(result); + } +} + +void init_mysql() { + mysql_conn = mysql_init(NULL); + if (!mysql_real_connect(mysql_conn, MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_BASE, MYSQL_PORT, NULL, 0)) { + //error + } +} + +void free_mysql() { + mysql_close(mysql_conn); +} + diff --git a/mysqlConn.h b/mysqlConn.h new file mode 100644 index 0000000..33438b3 --- /dev/null +++ b/mysqlConn.h @@ -0,0 +1,14 @@ +#ifndef _MySQLConn_h +#define _MySQLConn_h + +#include "main.h" +#include + +MYSQL *getMySQL(); +void check_mysql(); +MYSQL_RES *mysql_use(); +void mysql_free(); +void init_mysql(); +void free_mysql(); + +#endif \ No newline at end of file -- 2.20.1