Subversion Repositories RepoView

Rev

Rev 1 | Rev 5 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1 Rev 3
Line 1... Line 1...
1
/* $Id: sqlite.c 1 2024-08-20 19:18:25Z nishi $ */
1
/* $Id: sqlite.c 3 2024-08-20 21:05:24Z nishi $ */
2
 
2
 
3
#include "rv_db.h"
3
#include "rv_db.h"
4
 
4
 
5
#include "../../config.h"
5
#include "../../config.h"
6
 
6
 
Line 12... Line 12...
12
#include <stdlib.h>
12
#include <stdlib.h>
13
#include <stdbool.h>
13
#include <stdbool.h>
14
 
14
 
15
sqlite3* sql;
15
sqlite3* sql;
16
 
16
 
17
void rv_init_db(void){
17
void rv_init_db(void) {
18
	int ret;
18
	int ret;
19
	ret = sqlite3_open(DB_ROOT "/db.sqlite3", &sql);
19
	ret = sqlite3_open(DB_ROOT "/db.sqlite3", &sql);
20
	if(ret != SQLITE_OK){
20
	if(ret != SQLITE_OK) {
21
		rv_error_http();
21
		rv_error_http();
22
		printf("SQLite3 database error\n");
22
		printf("SQLite3 database error\n");
23
		exit(1);
23
		exit(1);
24
	}
24
	}
25
	char* err;
25
	char* err;
26
	ret = sqlite3_exec(
-
 
27
		sql,
-
 
28
		"create table if not exists users(user text, password text)",
26
	ret = sqlite3_exec(sql, "create table if not exists users(user text, password text)", NULL, NULL, &err);
29
		NULL,
-
 
30
		NULL,
-
 
31
		&err
-
 
32
	);
-
 
33
	if(ret != SQLITE_OK){
27
	if(ret != SQLITE_OK) {
34
		sqlite3_free(err);
28
		sqlite3_free(err);
35
		rv_error_http();
29
		rv_error_http();
36
		printf("SQLite3 database error\n");
30
		printf("SQLite3 database error\n");
37
		exit(1);
31
		exit(1);
38
	}
32
	}
39
	ret = sqlite3_exec(
-
 
40
		sql,
-
 
41
		"create table if not exists tokens(user text, token text)",
33
	ret = sqlite3_exec(sql, "create table if not exists tokens(user text, token text)", NULL, NULL, &err);
42
		NULL,
-
 
43
		NULL,
-
 
44
		&err
-
 
45
	);
-
 
46
	if(ret != SQLITE_OK){
34
	if(ret != SQLITE_OK) {
47
		sqlite3_free(err);
35
		sqlite3_free(err);
48
		rv_error_http();
36
		rv_error_http();
49
		printf("SQLite3 database error\n");
37
		printf("SQLite3 database error\n");
50
		exit(1);
38
		exit(1);
51
	}
39
	}
52
}
40
}
53
 
41
 
54
void rv_close_db(void){
42
void rv_close_db(void) { sqlite3_close(sql); }
55
	sqlite3_close(sql);
-
 
56
}
-
 
57
 
43
 
58
int count = 0;
44
int count = 0;
59
 
45
 
60
int sqlcount(void* param, int ncol, char** row, char** col){
46
int sqlcount(void* param, int ncol, char** row, char** col) {
61
	count = ncol;
47
	count = ncol;
62
	fprintf(stderr, "%d\n", ncol);
48
	fprintf(stderr, "%d\n", ncol);
63
	return 0;
49
	return 0;
64
}
50
}
65
 
51
 
66
bool rv_has_user(const char* username){
52
bool rv_has_user(const char* username) {
67
	char* err;
53
	char* err;
68
	char cbuf[2];
54
	char cbuf[2];
69
	cbuf[1] = 0;
55
	cbuf[1] = 0;
70
	char* query = rv_strdup("select * from users where user = '");
56
	char* query = rv_strdup("select * from users where user = '");
71
	int i;
57
	int i;
72
	for(i = 0; username[i] != 0; i++){
58
	for(i = 0; username[i] != 0; i++) {
73
		if(username[i] == '\''){
59
		if(username[i] == '\'') {
74
			cbuf[0] = username[i];
60
			cbuf[0] = username[i];
75
			char* tmp = query;
61
			char* tmp = query;
76
			tmp = rv_strcat(tmp, cbuf);
62
			tmp = rv_strcat(tmp, cbuf);
77
			free(tmp);
63
			free(tmp);
78
			cbuf[0] = username[i];
64
			cbuf[0] = username[i];
79
			tmp = query;
65
			tmp = query;
80
			query = rv_strcat(tmp, cbuf);
66
			query = rv_strcat(tmp, cbuf);
81
			free(tmp);
67
			free(tmp);
82
		}else{
68
		} else {
83
			cbuf[0] = username[i];
69
			cbuf[0] = username[i];
84
			char* tmp = query;
70
			char* tmp = query;
85
			query = rv_strcat(tmp, cbuf);
71
			query = rv_strcat(tmp, cbuf);
86
			free(tmp);
72
			free(tmp);
87
		}
73
		}
Line 89... Line 75...
89
	char* tmp = query;
75
	char* tmp = query;
90
	query = rv_strcat(tmp, "'");
76
	query = rv_strcat(tmp, "'");
91
	free(tmp);
77
	free(tmp);
92
	int ret;
78
	int ret;
93
	fprintf(stderr, "%s\n", query);
79
	fprintf(stderr, "%s\n", query);
94
	ret = sqlite3_exec(
80
	ret = sqlite3_exec(sql, query, sqlcount, NULL, &err);
95
		sql,
-
 
96
		query,
-
 
97
		sqlcount,
-
 
98
		NULL,
-
 
99
		&err
-
 
100
	);
-
 
101
	free(query);
81
	free(query);
102
	if(ret != SQLITE_OK){
82
	if(ret != SQLITE_OK) {
103
		sqlite3_free(err);
83
		sqlite3_free(err);
104
	}
84
	}
105
	return count > 0;
85
	return count > 0;
106
}
86
}