66494e7f5055cf333db9d500c9bde2a605c7f2cd
[NeonServV5.git] / mysqlConn.c
1
2 #include "mysqlConn.h"
3
4 struct used_result {
5     MYSQL_RES *result;
6     struct used_result *next;
7 };
8
9 struct escaped_string {
10     char *string;
11     struct escaped_string *next;
12 };
13
14 MYSQL *mysql_conn = NULL;
15 static struct used_result *used_results;
16 static struct escaped_string *escaped_strings;
17
18 void check_mysql() {
19     int errid;
20     if((errid = mysql_ping(mysql_conn))) {
21         if(mysql_errno(mysql_conn) == CR_SERVER_GONE_ERROR) {
22             if(!mysql_real_connect(mysql_conn, MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_BASE, MYSQL_PORT, NULL, 0)) {
23                 show_mysql_error();
24             }
25         } else {
26             //mysql error
27             show_mysql_error();
28         }
29     }
30 }
31
32 MYSQL_RES *mysql_use() {
33     MYSQL_RES *res = mysql_store_result(mysql_conn);
34     struct used_result *result = malloc(sizeof(*result));
35     if (!result) {
36         mysql_free_result(res);
37         return NULL;
38     }
39     result->result = res;
40     result->next = used_results;
41     used_results = result;
42     return res;
43 }
44
45 void mysql_free() {
46     struct used_result *result, *next_result;
47     for(result = used_results; result; result = next_result) {
48         next_result = result->next;
49         mysql_free_result(result->result);
50         free(result);
51     }
52     used_results = NULL;
53     struct escaped_string *escaped, *next_escaped;
54     for(escaped = escaped_strings; escaped; escaped = next_escaped) {
55         next_escaped = escaped->next;
56         free(escaped->string);
57         free(escaped);
58     }
59     escaped_strings = NULL;
60 }
61
62 void init_mysql() {
63     mysql_conn = mysql_init(NULL);
64     if (!mysql_real_connect(mysql_conn, MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_BASE, MYSQL_PORT, NULL, 0)) {
65         //error
66         show_mysql_error();
67     }
68 }
69
70 void free_mysql() {
71     mysql_close(mysql_conn);
72 }
73
74 void show_mysql_error() {
75     //show mysql_error()
76     printf("MySQL Error: %s\n", mysql_error(mysql_conn));
77 }
78
79 void printf_mysql_query(const char *text, ...) {
80     va_list arg_list;
81     char queryBuf[MYSQLMAXLEN];
82     int pos;
83     queryBuf[0] = '\0';
84     va_start(arg_list, text);
85     pos = vsnprintf(queryBuf, MYSQLMAXLEN - 2, text, arg_list);
86     va_end(arg_list);
87     if (pos < 0 || pos > (MYSQLMAXLEN - 2)) pos = MYSQLMAXLEN - 2;
88     queryBuf[pos] = '\0';
89     printf("MySQL: %s\n", queryBuf);
90     if(mysql_query(mysql_conn, queryBuf)) {
91         check_mysql();
92         if(mysql_query(mysql_conn, queryBuf)) {
93             show_mysql_error();
94         }
95     }
96 }
97
98 char* escape_string(const char *str) {
99     struct escaped_string *escapedstr = malloc(sizeof(*escapedstr));
100     if (!escapedstr) {
101         return NULL;
102     }
103     char escaped[strlen(str)*2+1];
104     mysql_real_escape_string(mysql_conn, escaped, str, strlen(str));
105     escapedstr->string = strdup(escaped);
106     escapedstr->next = escaped_strings;
107     escaped_strings = escapedstr;
108     return escapedstr->string;
109 }