aboutsummaryrefslogtreecommitdiff
path: root/db.c
blob: 653065e4596d2421385e8ce497a64d5e483e93f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <err.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
#include "db.h"

static char * textdup(const unsigned char *);

sqlite3_stmt *
byid(char *table, int id)
{
	char sql[100];
	sqlite3_stmt *stmt;
	
	snprintf(sql, 100, "SELECT oid, * FROM %s WHERE oid = ?", table);
	
	if(sqlite3_prepare(db, sql, -1, &stmt, 0) != SQLITE_OK)
		return NULL;
		
	if(sqlite3_bind_int(stmt, 1, id) != SQLITE_OK)
		return NULL;
	
	return stmt;
}

struct attachment *
getattachment(sqlite3_stmt *stmt, int final)
{
	struct attachment *attachment;
	
	if(!stmt)
		goto err;
	
	if(sqlite3_step(stmt) != SQLITE_ROW)
		goto err;
	
	if(!(attachment = malloc(sizeof(struct attachment))))
		err(1, "malloc");
	
	attachment->id = sqlite3_column_int(stmt, 0);
	attachment->post = sqlite3_column_int(stmt, 1);
	attachment->name = textdup(sqlite3_column_text(stmt, 2));
	attachment->description = textdup(sqlite3_column_text(stmt, 3));
	attachment->mime = textdup(sqlite3_column_text(stmt, 4));
	
	attachment->bytes = sqlite3_column_bytes(stmt, 5);
	if(!(attachment->data = malloc(attachment->bytes)))
		err(1, "malloc");
	memcpy(attachment->data, sqlite3_column_blob(stmt, 5),
	    attachment->bytes);
	
	if(final) sqlite3_finalize(stmt);
	return attachment;
	
err:
	if(final) sqlite3_finalize(stmt);
	return NULL;
}

struct post *
getpost(sqlite3_stmt *stmt, int final)
{
	struct post *post;
	
	if(!stmt)
		goto err;
	
	if(sqlite3_step(stmt) != SQLITE_ROW)
		goto err;
	
	if(!(post = malloc(sizeof(struct post))))
		err(1, "malloc");
	
	post->id = sqlite3_column_int(stmt, 0);
	post->parent = sqlite3_column_int(stmt, 1);
	post->user = sqlite3_column_int(stmt, 2);
	post->created = sqlite3_column_int(stmt, 3);
	post->edited = sqlite3_column_int(stmt, 4);
	post->subject = textdup(sqlite3_column_text(stmt, 5));
	post->text = textdup(sqlite3_column_text(stmt, 6));
	
	if(final) sqlite3_finalize(stmt);
	return post;
	
err:
	if(final) sqlite3_finalize(stmt);
	return NULL;
}

struct user *
getuser(sqlite3_stmt *stmt, int final)
{
	struct user *user;
	
	if(!stmt)
		goto err;
	
	if(sqlite3_step(stmt) != SQLITE_ROW)
		goto err;
	
	if(!(user = malloc(sizeof(struct user))))
		err(1, "malloc");
	
	user->id = sqlite3_column_int(stmt, 0);
	user->name = textdup(sqlite3_column_text(stmt, 1));
	user->full = textdup(sqlite3_column_text(stmt, 2));
	user->hash = textdup(sqlite3_column_text(stmt, 3));
	
	if(final) sqlite3_finalize(stmt);
	return user;
	
err:
	if(final) sqlite3_finalize(stmt);
	return NULL;
}

char *
textdup(const unsigned char *s)
{
	return s? strdup((char *)s): NULL;
}