*** VERSION 5.2.0 ***
[NeonServV5.git] / src / mysqlConn.c
1 /* mysqlConn.c - NeonServ v5.2
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "mysqlConn.h"
19 #define DATABASE_VERSION "5"
20
21 struct used_result {
22     MYSQL_RES *result;
23     struct used_result *next;
24 };
25
26 struct escaped_string {
27     char *string;
28     struct escaped_string *next;
29 };
30
31 MYSQL *mysql_conn = NULL;
32 static struct used_result *used_results;
33 static struct escaped_string *escaped_strings;
34 static int mysql_serverport;
35 static char *mysql_host, *mysql_user, *mysql_pass, *mysql_base;
36
37 void check_mysql() {
38     int errid;
39     if((errid = mysql_ping(mysql_conn))) {
40         if(mysql_errno(mysql_conn) == CR_SERVER_GONE_ERROR) {
41             if(!mysql_real_connect(mysql_conn, mysql_host, mysql_user, mysql_pass, mysql_base, mysql_serverport, NULL, 0)) {
42                 show_mysql_error();
43             }
44         } else {
45             //mysql error
46             show_mysql_error();
47         }
48     }
49 }
50
51 MYSQL_RES *mysql_use() {
52     MYSQL_RES *res = mysql_store_result(mysql_conn);
53     struct used_result *result = malloc(sizeof(*result));
54     if (!result) {
55         mysql_free_result(res);
56         return NULL;
57     }
58     result->result = res;
59     result->next = used_results;
60     used_results = result;
61     return res;
62 }
63
64 void mysql_free() {
65     struct used_result *result, *next_result;
66     for(result = used_results; result; result = next_result) {
67         next_result = result->next;
68         mysql_free_result(result->result);
69         free(result);
70     }
71     used_results = NULL;
72     struct escaped_string *escaped, *next_escaped;
73     for(escaped = escaped_strings; escaped; escaped = next_escaped) {
74         next_escaped = escaped->next;
75         free(escaped->string);
76         free(escaped);
77     }
78     escaped_strings = NULL;
79 }
80
81 void init_mysql(char *host, int port, char *user, char *pass, char *base) {
82     mysql_host = strdup(host);
83     mysql_serverport = port;
84     mysql_user = strdup(user);
85     mysql_pass = strdup(pass);
86     mysql_base = strdup(base);
87     mysql_conn = mysql_init(NULL);
88     if (!mysql_real_connect(mysql_conn, mysql_host, mysql_user, mysql_pass, mysql_base, mysql_serverport, NULL, 0)) {
89         //error
90         show_mysql_error();
91     }
92     //check database version...
93     int version = 0;
94     if(!mysql_query(mysql_conn, "SELECT `database_version` FROM `version`")) {
95         MYSQL_RES *res = mysql_use();
96         MYSQL_ROW row;
97         if((row = mysql_fetch_row(res))) {
98             version = atoi(row[0]);
99         }
100     }
101     printf("%d \n", version);
102     if(!version) {
103         //CREATE DATABASE
104         FILE *f = fopen("database.sql", "r");
105         mysql_set_server_option(mysql_conn, MYSQL_OPTION_MULTI_STATEMENTS_ON);
106         if (f) {
107             char line[512];
108             char query_buffer[8192];
109             int query_buffer_pos = 0;
110             while (fgets(line, sizeof(line), f)) {
111                 query_buffer_pos += sprintf(query_buffer + query_buffer_pos, " %s", line);
112                 if(line[(strlen(line) - 2)] == ';') {
113                     if(mysql_query(mysql_conn, query_buffer))
114                         show_mysql_error();
115                     query_buffer_pos = 0;
116                 }
117             }
118             fclose(f);
119         }
120         f = fopen("database.defaults.sql", "r");
121         if (f) {
122             char line[4096];
123             char query_buffer[131072];
124             int query_buffer_pos = 0;
125             while (fgets(line, sizeof(line), f)) {
126                 query_buffer_pos += sprintf(query_buffer + query_buffer_pos, " %s", line);
127                 if(line[(strlen(line) - 2)] == ';') {
128                     if(mysql_query(mysql_conn, query_buffer))
129                         show_mysql_error();
130                     query_buffer_pos = 0;
131                 }
132             }
133             fclose(f);
134         }
135         mysql_set_server_option(mysql_conn, MYSQL_OPTION_MULTI_STATEMENTS_OFF);
136         mysql_query(mysql_conn, "INSERT INTO `version` (`database_version`) VALUES ('" DATABASE_VERSION "')");
137     }
138     else if(version < atoi(DATABASE_VERSION)) {
139         //UPDATE DATABASE
140         FILE *f = fopen("database.upgrade.sql", "r");
141         mysql_set_server_option(mysql_conn, MYSQL_OPTION_MULTI_STATEMENTS_ON);
142         if (f) {
143             char line[512];
144             char query_buffer[8192];
145             int query_buffer_pos = 0, use_querys = 0;
146             sprintf(query_buffer, "-- version: %d", version);
147             while (fgets(line, sizeof(line), f)) {
148                 if(use_querys) {
149                     query_buffer_pos += sprintf(query_buffer + query_buffer_pos, " %s", line);
150                     if(line[strlen(line) - 1] == ';') {
151                         mysql_query(mysql_conn, query_buffer);
152                         query_buffer_pos = 0;
153                     }
154                 } else if(!stricmp(query_buffer, line)) {
155                     use_querys = 1;
156                 }
157             }
158             if(query_buffer_pos) {
159                 if(mysql_query(mysql_conn, query_buffer))
160                     show_mysql_error();
161             }
162             fclose(f);
163         } else
164             perror("database.sql missing!");
165         mysql_set_server_option(mysql_conn, MYSQL_OPTION_MULTI_STATEMENTS_OFF);
166         mysql_query(mysql_conn, "UPDATE `version` SET `database_version` = '" DATABASE_VERSION "'");
167     }
168 }
169
170 void free_mysql() {
171     mysql_close(mysql_conn);
172 }
173
174 void show_mysql_error() {
175     //show mysql_error()
176     printf("MySQL Error: %s\n", mysql_error(mysql_conn));
177 }
178
179 void printf_mysql_query(const char *text, ...) {
180     va_list arg_list;
181     char queryBuf[MYSQLMAXLEN];
182     int pos;
183     queryBuf[0] = '\0';
184     va_start(arg_list, text);
185     pos = vsnprintf(queryBuf, MYSQLMAXLEN - 2, text, arg_list);
186     va_end(arg_list);
187     if (pos < 0 || pos > (MYSQLMAXLEN - 2)) pos = MYSQLMAXLEN - 2;
188     queryBuf[pos] = '\0';
189     printf("MySQL: %s\n", queryBuf);
190     if(mysql_query(mysql_conn, queryBuf)) {
191         check_mysql();
192         if(mysql_query(mysql_conn, queryBuf)) {
193             show_mysql_error();
194         }
195     }
196 }
197
198 char* escape_string(const char *str) {
199     struct escaped_string *escapedstr = malloc(sizeof(*escapedstr));
200     if (!escapedstr) {
201         return NULL;
202     }
203     char escaped[strlen(str)*2+1];
204     mysql_real_escape_string(mysql_conn, escaped, str, strlen(str));
205     escapedstr->string = strdup(escaped);
206     escapedstr->next = escaped_strings;
207     escaped_strings = escapedstr;
208     return escapedstr->string;
209 }