Subversion Repositories IRCServ

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 nishi 1
/* $Id: db.c 10 2024-08-26 13:37:23Z nishi $ */
2
 
3
#include "is_db.h"
4
 
5
#include "is_log.h"
6
#include "is_util.h"
7
 
8
#include <sqlite3.h>
9
 
10
#include <stdlib.h>
11
 
12
sqlite3* sql;
13
 
14
char* escape_sql(const char* input) {
15
	char* query = malloc(1);
16
	query[0] = 0;
17
	char cbuf[2];
18
	cbuf[1] = 0;
19
	int i;
20
	for(i = 0; input[i] != 0; i++) {
21
		if(input[i] == '\'') {
22
			cbuf[0] = input[i];
23
			char* tmp = query;
24
			query = is_strcat(tmp, cbuf);
25
			free(tmp);
26
			cbuf[0] = input[i];
27
			tmp = query;
28
			query = is_strcat(tmp, cbuf);
29
			free(tmp);
30
		} else {
31
			cbuf[0] = input[i];
32
			char* tmp = query;
33
			query = is_strcat(tmp, cbuf);
34
			free(tmp);
35
		}
36
	}
37
	return query;
38
}
39
 
40
int is_db_init(void) {
41
	int ret;
42
	ret = sqlite3_open(PREFIX "/lib/ircserv/db.sqlite3", &sql);
43
	if(ret != SQLITE_OK) {
44
		is_log("Failed to open the database");
45
		return 1;
46
	}
47
	return 0;
48
}
49
 
50
int is_run_sql(const char* txt) {
51
	char* err;
52
	int ret;
53
	ret = sqlite3_exec(sql, txt, NULL, NULL, &err);
54
	if(ret != SQLITE_OK) {
55
		char* str = is_strcat("SQL error: ", err);
56
		sqlite3_free(err);
57
		is_log(str);
58
		free(str);
59
	}
60
	return ret == SQLITE_OK ? 0 : 1;
61
}