Debian Codegraph @

src/bnetd/sql_sqlite3.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2005 Dizzy
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #ifdef WITH_SQL_SQLITE3
20 #include "common/setup_before.h"
21 # include <sqlite3.h>
22 #include <cstdlib>
23 #include "common/eventlog.h"
24 #include "common/xalloc.h"
25 #include "storage_sql.h"
26 #include "sql_sqlite3.h"
27 #include "common/setup_after.h"
28
29 namespace pvpgn
30 {
31
32 namespace bnetd
33 {
34
35 typedef struct {
36 char** results;
37 int rows;
38 int columns;
39 int crow;
40 } t_sqlite3_res;
41
42 static int sql_sqlite3_init(const char *, const char *, const char *, const char *, const char *, const char *);
43 static int sql_sqlite3_close(void);
44 static t_sql_res * sql_sqlite3_query_res(const char *);
45 static int sql_sqlite3_query(const char *);
46 static t_sql_row * sql_sqlite3_fetch_row(t_sql_res *);
47 static void sql_sqlite3_free_result(t_sql_res *);
48 static unsigned int sql_sqlite3_num_rows(t_sql_res *);
49 static unsigned int sql_sqlite3_num_fields(t_sql_res *);
50 static unsigned int sql_sqlite3_affected_rows(void);
51 static t_sql_field * sql_sqlite3_fetch_fields(t_sql_res *);
52 static int sql_sqlite3_free_fields(t_sql_field *);
53 static void sql_sqlite3_escape_string(char *, const char *, int);
54
55 t_sql_engine sql_sqlite3 = {
56 sql_sqlite3_init,
57 sql_sqlite3_close,
58 sql_sqlite3_query_res,
59 sql_sqlite3_query,
60 sql_sqlite3_fetch_row,
61 sql_sqlite3_free_result,
62 sql_sqlite3_num_rows,
63 sql_sqlite3_num_fields,
64 sql_sqlite3_affected_rows,
65 sql_sqlite3_fetch_fields,
66 sql_sqlite3_free_fields,
67 sql_sqlite3_escape_string
68 };
69
70 static sqlite3 *db = NULL;
71
72 #ifndef RUNTIME_LIBS
73 # define p_sqlite3_changes sqlite3_changes
74 # define p_sqlite3_close sqlite3_close
75 # define p_sqlite3_errmsg sqlite3_errmsg
76 # define p_sqlite3_exec sqlite3_exec
77 # define p_sqlite3_free_table sqlite3_free_table
78 # define p_sqlite3_get_table sqlite3_get_table
79 # define p_sqlite3_open sqlite3_open
80 # define p_sqlite3_snprintf sqlite3_snprintf
81 #else
82 /* RUNTIME_LIBS */
83 static int sqlite_load_library(void);
84
85 typedef int(*f_sqlite3_changes)(sqlite3*);
86 typedef int(*f_sqlite3_close)(sqlite3*);
87 typedef const char* (*f_sqlite3_errmsg)(sqlite3*);
88 typedef int(*f_sqlite3_exec)(sqlite3*, const char*, sqlite3_callback, void*, char**);
89 typedef void(*f_sqlite3_free_table)(char **);
90 typedef int(*f_sqlite3_get_table)(sqlite3*, const char*, char***, int*, int*, char**);
91 typedef int(*f_sqlite3_open)(const char*, sqlite3**);
92 typedef char* (*f_sqlite3_snprintf)(int, char*, const char*, ...);
93
94 static f_sqlite3_changes p_sqlite3_changes = NULL;
95 static f_sqlite3_close p_sqlite3_close = NULL;
96 static f_sqlite3_errmsg p_sqlite3_errmsg = NULL;
97 static f_sqlite3_exec p_sqlite3_exec = NULL;
98 static f_sqlite3_free_table p_sqlite3_free_table = NULL;
99 static f_sqlite3_get_table p_sqlite3_get_table = NULL;
100 static f_sqlite3_open p_sqlite3_open = NULL;
101 static f_sqlite3_snprintf p_sqlite3_snprintf = NULL;
102
103 #include "compat/runtime_libs.h" /* defines OpenLibrary(), GetFunction(), CloseLibrary() & SQLITE3_LIB */
104
105 static void * handle = NULL;
106
107 static int sqlite_load_library(void)
108 {
109 if ((handle = OpenLibrary(SQLITE3_LIB)) == NULL) return -1;
110
111 if (((p_sqlite3_changes = (f_sqlite3_changes)GetFunction(handle, "sqlite3_changes")) == NULL) ||
112 ((p_sqlite3_close = (f_sqlite3_close)GetFunction(handle, "sqlite3_close")) == NULL) ||
113 ((p_sqlite3_errmsg = (f_sqlite3_errmsg)GetFunction(handle, "sqlite3_errmsg")) == NULL) ||
114 ((p_sqlite3_exec = (f_sqlite3_exec)GetFunction(handle, "sqlite3_exec")) == NULL) ||
115 ((p_sqlite3_free_table = (f_sqlite3_free_table)GetFunction(handle, "sqlite3_free_table")) == NULL) ||
116 ((p_sqlite3_get_table = (f_sqlite3_get_table)GetFunction(handle, "sqlite3_get_table")) == NULL) ||
117 ((p_sqlite3_open = (f_sqlite3_open)GetFunction(handle, "sqlite3_open")) == NULL) ||
118 ((p_sqlite3_snprintf = (f_sqlite3_snprintf)GetFunction(handle, "sqlite3_snprintf")) == NULL))
119 {
120 CloseLibrary(handle);
121 handle = NULL;
122 return -1;
123 }
124
125 return 0;
126 }
127 #endif /* RUNTIME_LIBS */
128
129 static int sql_sqlite3_init(const char *host, const char *port, const char *socket, const char *name, const char *user, const char *pass)
130 {
131 #ifdef RUNTIME_LIBS
132 if (sqlite_load_library()) {
133 eventlog(eventlog_level_error, __FUNCTION__, "error loading library file \"{}\"", SQLITE3_LIB);
134 return -1;
135 }
136 #endif
137 /* SQLite3 has no host, port, socket, user or password and the database name is the path to the db file */
138 if (p_sqlite3_open(name, &db) != SQLITE_OK) {
139 eventlog(eventlog_level_error, __FUNCTION__, "got error from sqlite3_open ({})", p_sqlite3_errmsg(db));
140 p_sqlite3_close(db);
141 return -1;
142 }
143
144 return 0;
145 }
146
147 static int sql_sqlite3_close(void)
148 {
149 if (db) {
150 if (p_sqlite3_close(db) != SQLITE_OK) {
151 eventlog(eventlog_level_error, __FUNCTION__, "got error from sqlite3_close ({})", p_sqlite3_errmsg(db));
152 return -1;
153 }
154 db = NULL;
155 }
156 #ifdef RUNTIME_LIBS
157 if (handle) {
158 CloseLibrary(handle);
159 handle = NULL;
160 }
161 #endif
162 return 0;
163 }
164
165 static t_sql_res * sql_sqlite3_query_res(const char* query)
166 {
167 t_sqlite3_res *res;
168
169 if (db == NULL) {
170 eventlog(eventlog_level_error, __FUNCTION__, "sqlite3 driver not initilized");
171 return NULL;
172 }
173
174 if (query == NULL) {
175 eventlog(eventlog_level_error, __FUNCTION__, "got NULL query");
176 return NULL;
177 }
178
179 res = (t_sqlite3_res *)xmalloc(sizeof(t_sqlite3_res));
180
181 if (p_sqlite3_get_table(db, query, &res->results, &res->rows, &res->columns, NULL) != SQLITE_OK) {
182 /* eventlog(eventlog_level_debug, __FUNCTION__, "got error ({}) from query ({})", p_sqlite3_errmsg(db), query); */
183 xfree((void*)res);
184 return NULL;
185 }
186
187 res->crow = 0;
188
189 return res;
190 }
191
192 static int sql_sqlite3_query(const char* query)
193 {
194 if (db == NULL) {
195 eventlog(eventlog_level_error, __FUNCTION__, "sqlite3 driver not initilized");
196 return -1;
197 }
198
199 if (query == NULL) {
200 eventlog(eventlog_level_error, __FUNCTION__, "got NULL query");
201 return -1;
202 }
203
204 return p_sqlite3_exec(db, query, NULL, NULL, NULL) == SQLITE_OK ? 0 : -1;
205 }
206
207 static t_sql_row * sql_sqlite3_fetch_row(t_sql_res *result)
208 {
209 t_sqlite3_res *res = (t_sqlite3_res*)result;
210
211 if (result == NULL) {
212 eventlog(eventlog_level_error, __FUNCTION__, "got NULL result");
213 return NULL;
214 }
215
216 return res->crow < res->rows ? (res->results + res->columns * (++res->crow)) : NULL;
217 }
218
219 static void sql_sqlite3_free_result(t_sql_res *result)
220 {
221 if (result == NULL) {
222 eventlog(eventlog_level_error, __FUNCTION__, "got NULL result");
223 return;
224 }
225
226 p_sqlite3_free_table(((t_sqlite3_res *)result)->results);
227 xfree(result);
228 }
229
230 static unsigned int sql_sqlite3_num_rows(t_sql_res *result)
231 {
232 if (result == NULL) {
233 eventlog(eventlog_level_error, __FUNCTION__, "got NULL result");
234 return 0;
235 }
236
237 return ((t_sqlite3_res *)result)->rows;
238 }
239
240 static unsigned int sql_sqlite3_num_fields(t_sql_res *result)
241 {
242 if (result == NULL) {
243 eventlog(eventlog_level_error, __FUNCTION__, "got NULL result");
244 return 0;
245 }
246
247 return ((t_sqlite3_res *)result)->columns;
248 }
249
250 static unsigned int sql_sqlite3_affected_rows(void)
251 {
252 return p_sqlite3_changes(db);
253 }
254
255 static t_sql_field * sql_sqlite3_fetch_fields(t_sql_res *result)
256 {
257 if (result == NULL) {
258 eventlog(eventlog_level_error, __FUNCTION__, "got NULL result");
259 return NULL;
260 }
261
262 /* the first "row" is actually the field names */
263 return ((t_sqlite3_res*)result)->results;
264 }
265
266 static int sql_sqlite3_free_fields(t_sql_field *fields)
267 {
268 if (fields == NULL) {
269 eventlog(eventlog_level_error, __FUNCTION__, "got NULL fields");
270 return -1;
271 }
272
273 return 0; /* sqlite3_free_table() should free it properly */
274 }
275
276 static void sql_sqlite3_escape_string(char *escape, const char *from, int len)
277 {
278 if (db == NULL) {
279 eventlog(eventlog_level_error, __FUNCTION__, "sqlite3 driver not initilized");
280 return;
281 }
282 p_sqlite3_snprintf(len * 2 + 1, escape, "%q", from);
283 }
284
285 }
286
287 }
288
289 #endif /* WITH_SQL_SQLITE3 */