Subversion Repositories IRC-Archiver

Rev

Rev 11 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9 nishi 1
/* $Id: db.c 13 2024-08-30 07:33:43Z nishi $ */
10 nishi 2
 
3
#include "web_db.h"
13 nishi 4
 
5
#include "ia_db.h"
6
#include "ia_util.h"
7
 
8
#include <stdio.h>
9
#include <stdlib.h>
10
 
11
#include <sqlite3.h>
12
 
13
extern sqlite3* sql;
14
 
15
entry_t** retarr;
16
 
17
int add_info(void* param, int ncol, char** row, char** col) {
18
	entry_t* entry = malloc(sizeof(*entry));
19
	entry->username = ia_strdup(row[0]);
20
	entry->message = ia_strdup(row[2]);
21
	entry->time = strtoull(row[3], (char**)NULL, 10);
22
 
23
	entry_t** oldret = retarr;
24
	int i;
25
	for(i = 0; oldret[i] != NULL; i++)
26
		;
27
	retarr = malloc(sizeof(*retarr) * (i + 2));
28
	for(i = 0; oldret[i] != NULL; i++) {
29
		retarr[i] = oldret[i];
30
	}
31
	retarr[i] = entry;
32
	retarr[i + 1] = NULL;
33
	free(oldret);
34
 
35
	return 0;
36
}
37
 
38
int compare_time(const void* _a, const void* _b) {
39
	entry_t** a = (entry_t**)_a;
40
	entry_t** b = (entry_t**)_b;
41
	return (*a)->time - (*b)->time;
42
}
43
 
44
entry_t** web_db_query(web_range_t range) {
45
	char date[512];
46
	char* esc = ia_escape_sql(range.channel);
47
	char* query = ia_strcat3("select * from log where channel = '", esc, "'");
48
 
49
	retarr = malloc(sizeof(*retarr));
50
	retarr[0] = NULL;
51
 
52
	if(range.from > 0) {
53
		char* tmp = query;
54
		sprintf(date, "%llu", range.from);
55
		query = ia_strcat3(tmp, " and date >= ", date);
56
		free(tmp);
57
	}
58
	if(range.to > 0) {
59
		char* tmp = query;
60
		sprintf(date, "%llu", range.to);
61
		query = ia_strcat3(tmp, " and date <= ", date);
62
		free(tmp);
63
	}
64
 
65
	char* err;
66
	int ret = sqlite3_exec(sql, query, add_info, NULL, &err);
67
	if(ret != SQLITE_OK) {
68
		sqlite3_free(err);
69
	}
70
 
71
	free(query);
72
 
73
	int i;
74
	for(i = 0; retarr[i] != NULL; i++)
75
		;
76
 
77
	qsort(retarr, i, sizeof(entry_t*), compare_time);
78
 
79
	return retarr;
80
}