added mysql connector
authorpk910 <philipp@zoelle1.de>
Sun, 14 Aug 2011 00:52:32 +0000 (02:52 +0200)
committerpk910 <philipp@zoelle1.de>
Sun, 14 Aug 2011 01:17:00 +0000 (03:17 +0200)
Makefile
config.h [new file with mode: 0644]
main.c
main.h
mysqlConn.c [new file with mode: 0644]
mysqlConn.h [new file with mode: 0644]

index 78835ed02a33e18ff1459afe4d75efbb8fb76186..3f6a6eb6bfdc3fbb172100943d0218792f8be5a9 100644 (file)
--- 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 (file)
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 1dd49500f38975fd51173d1dd2224977a6f56c53..0870d968c321b81ec688dfecd13adb37d4a49903 100644 (file)
--- 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 7efb1cd0d8819eaed3755b65d60ba4abf21da4cf..c154da3014f4f9661a9ea4419c7d8101c803f3ce 100644 (file)
--- a/main.h
+++ b/main.h
@@ -1,6 +1,8 @@
 #ifndef _main_h
 #define _main_h
 
+#include "config.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
diff --git a/mysqlConn.c b/mysqlConn.c
new file mode 100644 (file)
index 0000000..2dbd608
--- /dev/null
@@ -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 (file)
index 0000000..33438b3
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef _MySQLConn_h
+#define _MySQLConn_h
+
+#include "main.h"
+#include <mysql.h>
+
+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